How to create rubberband effect when drawing line?

  • Thread starter Thread starter Anand Ganesh
  • Start date Start date
A

Anand Ganesh

Hi All,

In my application I am allowing the user to draw a line. But when the user
clicks the start point and starts moving the mouse there is a series of line
generated. When the mouse is up the final line is drawn.

How to avoid the series of lines being generated during mouse move event?
How to get the rubberband effect when the user moves the mouse?

I tried using the ControlPaint.Reversible line but this does not help
because the mouse cursor and the drawn line does not lie in the same
co-ordinates. There is always an offset. I even tried PointtoScreen and
there is still an offset.

Is there any solution for this?

Can anybody please help. Thanks for your time.

Thanks
Anand Ganesj
 
I am relatively new at the C# thing but in C++ what you would do is;

1) Create a memory bitmap that hold the portion of the screen from the
first point to the second point. This bitmap(a) will later be used to
erase the line.
2) Make a copy of the bitmap(a) into another bitmap(b)
3) draw the line into the bitmap
4) copy the bitmap onto the screen.

When the mouse moves, get the new start and end points.
1) Create a new bitmap(b) that holds the screen image for the new points
2) copy the bitmap(a) from above into the new one. - This erases the
first line
3) Make a copy of bitmap(b) into bitamp(a) - to be used to erase
4) draw the line in bitmap(b)
5) copy the bitmap to the screen.

It sounds complicated but it is really about that many lines of code.
The thing is to keep erasing the old line and then draw the new line.
Do it all in memory bitmaps there is no flicker.

In C# you would use the Bitmap class. Everything else would be the
same.
 
Back
Top