drawing line without invalidating whole screen

msmeth

Member
Joined
Jun 17, 2006
Messages
6
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 isnt being redrawn but the line shows continously?
 
No need to invalidate. Just draw.
C#:
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.
C#:
// 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;
}
 
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 cant get into explaining all that this thing does). So, this solution you showed me isnt working. Its causing a discontinuous line to be drawn....

marble_eater said:
No need to invalidate. Just draw.
C#:
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.
C#:
// 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;
}
 
Unless you post (at least some) code, its 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.
 
Why not double buffer the graphics. Either manually create a System.Drawing.Bitmap to back the controls 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 cant think of anything besides maintaining an array of all the points in the line and redrawing the entire line when the control is redrawn.
 
You may want to take a look at this thread on another forum:

http://www.vbcity.com/forums/topic.asp?tid=95435

Download the last Rubberband project I did.

As marble_eater suggested unless you are drawing more than 3-4 thousand lines I would redraw all the lines every time. Always avoid using CreateGraphics, you will never get flicker free graphics.
 
Back
Top