msmeth Posted June 20, 2006 Posted June 20, 2006 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? Quote
Leaders snarfblam Posted June 20, 2006 Leaders Posted June 20, 2006 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; } Quote [sIGPIC]e[/sIGPIC]
msmeth Posted June 20, 2006 Author Posted June 20, 2006 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; } Quote
Cags Posted June 20, 2006 Posted June 20, 2006 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted June 20, 2006 Leaders Posted June 20, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Talyrond Posted June 28, 2006 Posted June 28, 2006 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.