Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Draw a Rectangle?

How to Draw a Rectangle?

Scheduled Pinned Locked Moved C#
graphicstutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sduhd
    wrote on last edited by
    #1

    I want to draw a rectangle while moving the mouse. But the event handlers below would preserve all the intermediate rectangles in the window. How to erase the intermediate rectangles and only show the last rectangle? Thanks. private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { firstpoint = new Point(e.X,e.Y); } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Graphics g = this.CreateGraphics(); Pen dotPen = new Pen(Color.Black, 1);dotPen.DashStyle=DashStyle.Dot; g.DrawRectangle(dotPen,firstpoint.X,firstpoint.Y,e.X-firstpoint.X,e.Y-firstpoint.Y); } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { secondpoint=new Point(e.X,e.Y); Graphics g = this.CreateGraphics(); Pen dotPen = new Pen(Color.Black, 1);dotPen.DashStyle=DashStyle.Dot; g.DrawRectangle(dotPen,firstpoint.X,firstpoint.Y,e.X-firstpoint.X,e.Y-firstpoint.Y); }

    G 1 Reply Last reply
    0
    • S sduhd

      I want to draw a rectangle while moving the mouse. But the event handlers below would preserve all the intermediate rectangles in the window. How to erase the intermediate rectangles and only show the last rectangle? Thanks. private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { firstpoint = new Point(e.X,e.Y); } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Graphics g = this.CreateGraphics(); Pen dotPen = new Pen(Color.Black, 1);dotPen.DashStyle=DashStyle.Dot; g.DrawRectangle(dotPen,firstpoint.X,firstpoint.Y,e.X-firstpoint.X,e.Y-firstpoint.Y); } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { secondpoint=new Point(e.X,e.Y); Graphics g = this.CreateGraphics(); Pen dotPen = new Pen(Color.Black, 1);dotPen.DashStyle=DashStyle.Dot; g.DrawRectangle(dotPen,firstpoint.X,firstpoint.Y,e.X-firstpoint.X,e.Y-firstpoint.Y); }

      G Offline
      G Offline
      Gulfraz Khan
      wrote on last edited by
      #2

      I think you should draw your rectangle in the Form's Paint event As per my knowledge using the CreateGraphics() draws a drawing permanently -- modified at 8:18 Thursday 13th October, 2005

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups