Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I need to draw lines similar to drawing lines using a mouse (click to start the line and click to end the line. There is no holding down like dragging a mouse). How do I use invalidate so the entire screen isn't being redrawn but the line shows continously?
  • Leaders
Posted

No need to invalidate. Just draw.

int lastX = 0;
int lastY = 0;

// Demonstrate drawing given a pair of coordinates
// You can adapt this for whatever input device you may be using
public void OnThisMouseMove(Object sender, MouseEventArgs e){
   this.CreateGraphics.DrawLine(Pens.Black, e.X, e.Y, lastX, lastY);
   lastX = e.X;
   lastY = e.Y;
}

 

If you are using a bitmap as a backbuffer, it might be more efficient to draw twice, once to the buffer and once to the screen, instead of drawing to the buffer and then invalidating the screen to reflect the changes.

// Adapted for backbuffer
public void OnThisMouseMove(Object sender, MouseEventArgs e){
   gBackBuffer.DrawLine(Pens.Black, e.X, e.Y, lastX, lastY);
   this.CreateGraphics.DrawLine(Pens.Black, e.X, e.Y, lastX, lastY);
   lastX = e.X;
   lastY = e.Y;
}

[sIGPIC]e[/sIGPIC]
Posted

I have to actually choose a colour from a menu to draw the line. Then the line begins drawing from the menu. The menu is always being redrawn (I know it sounds odd but I can't get into explaining all that this thing does). So, this solution you showed me isn't working. It's causing a discontinuous line to be drawn....

 

No need to invalidate. Just draw.

int lastX = 0;
int lastY = 0;

// Demonstrate drawing given a pair of coordinates
// You can adapt this for whatever input device you may be using
public void OnThisMouseMove(Object sender, MouseEventArgs e){
   this.CreateGraphics.DrawLine(Pens.Black, e.X, e.Y, lastX, lastY);
   lastX = e.X;
   lastY = e.Y;
}

 

If you are using a bitmap as a backbuffer, it might be more efficient to draw twice, once to the buffer and once to the screen, instead of drawing to the buffer and then invalidating the screen to reflect the changes.

// Adapted for backbuffer
public void OnThisMouseMove(Object sender, MouseEventArgs e){
   gBackBuffer.DrawLine(Pens.Black, e.X, e.Y, lastX, lastY);
   this.CreateGraphics.DrawLine(Pens.Black, e.X, e.Y, lastX, lastY);
   lastX = e.X;
   lastY = e.Y;
}

Posted
Unless you post (at least some) code, it's going to be very difficult for us to help you with this matter. With drawing there are so many different variables, us guessing exactly whats causing your problem could take forever.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

Why not double buffer the graphics. Either manually create a System.Drawing.Bitmap to back the control's image and use code like that form my second example, drawing to both the screen and the buffer so you never lose the graphics and still draw them efficiently.

 

Short of that, I can't think of anything besides maintaining an array of all the points in the line and redrawing the entire line when the control is redrawn.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...