[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

Drawing a line with mouse drag?

John Parrish

1/14/2005 4:06:00 PM

Can anyone point me to an example of drawing a line in real time as a
user drags the mouse. I have tried several ways to get around the
flickering caused by invalidate when the line is drug across other
elements, to no avail.

I have the book on GDI+ with .NET but it does not show an example of
doing this, instead it lets the user drag the mouse and when release the
line is drawn.

Please end my misery!! Thanks
5 Answers

John Parrish

1/15/2005 10:31:00 PM

0

I assume 1 of 2 things here... either this is something that is so easy
it's not worth anyones time, or this is something that is difficult to
achieve and an explanation of how would be too in depth.

I would appreciate at least one reply to let me know which of the two it
is ;) Thanks!

Mick Doherty

1/16/2005 12:18:00 AM

0

It's too easy, but I spent 2 mins writing this for you anyway. ;-)
There are several ways to achieve this, but this is how I would do it.

\\Private Lines As New Collection

Private Structure Line
Public StartPos As Point
Public EndPos As Point
Public Sub New(ByVal pt1 As Point, ByVal pt2 As Point)
StartPos = pt1
EndPos = pt2
End Sub
End Structure

Private StartPoint, EndPoint As Point

Private Sub MainForm_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
If e.Button = MouseButtons.Left Then
StartPoint = New Point(e.X, e.Y)
End If
End Sub

Private Sub MainForm_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp
If e.Button = MouseButtons.Left Then
EndPoint = New Point(e.X, e.Y)
Lines.Add(New Line(StartPoint, EndPoint))
End If
End Sub

Private Sub MainForm_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
For Each Item As Line In Lines
e.Graphics.DrawLine(Pens.Blue, Item.StartPos, Item.EndPos)
Next
If Me.MouseButtons = MouseButtons.Left Then
e.Graphics.DrawLine(Pens.Blue, _
StartPoint, Me.PointToClient(Cursor.Position))
End If
End Sub

Private Sub MainForm_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove
Invalidate()
End Sub
///

--
Mick Doherty
http://dotnetrix.co.uk/no...


"John Parrish" <please@ask.me.com> wrote in message
news:O3iZFH1%23EHA.1400@TK2MSFTNGP11.phx.gbl...
>I assume 1 of 2 things here... either this is something that is so easy
>it's not worth anyones time, or this is something that is difficult to
>achieve and an explanation of how would be too in depth.
>
> I would appreciate at least one reply to let me know which of the two it
> is ;) Thanks!


Bob Powell

1/16/2005 12:19:00 AM

0

Hello John,
The process is simple.

#1 on mouse down, store the start point, set a boolean flag saying that
dragging is occurring.

#2 On mouse move AND the dragging flag set, store the current mouse position
and invalidate the window.

#3 On paint, do all the painting for the static stuff and if the mouse
dragging flag is set, draw from start position to end position

#4 On mouse up, save the start and end positions of the line in whatever
format you wish and reset the dragging flag

To eliminate flickering, use double buffering. You'll find an article for
simple or complex versions of this in Windows Forms Tips and Tricks.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"John Parrish" <pleaseask@me.com> wrote in message
news:uGJYmLl%23EHA.1392@tk2msftngp13.phx.gbl...
> Can anyone point me to an example of drawing a line in real time as a user
> drags the mouse. I have tried several ways to get around the flickering
> caused by invalidate when the line is drug across other elements, to no
> avail.
>
> I have the book on GDI+ with .NET but it does not show an example of doing
> this, instead it lets the user drag the mouse and when release the line is
> drawn.
>
> Please end my misery!! Thanks


John Parrish

1/16/2005 5:52:00 AM

0

Bob,

Thanks for the reply. I was afraid that the answer would be more towards
implementation, I will post some code in a few minutes to show what it
is I am seeing. I understand the mechanics of animating the line
drawing, I also understand how to enable double buffering. Maybe when I
post the code you can point out what I am doing wrong to get such nasty
results. Thanks.. code to follow shortly

John Parrish

1/16/2005 6:47:00 AM

0

John Parrish wrote:
> Bob,
>
> Thanks for the reply. I was afraid that the answer would be more towards
> implementation, I will post some code in a few minutes to show what it
> is I am seeing. I understand the mechanics of animating the line
> drawing, I also understand how to enable double buffering. Maybe when I
> post the code you can point out what I am doing wrong to get such nasty
> results. Thanks.. code to follow shortly

haha.. I'm going to bow out of this for now.. at work I had what I
thought was the same implementation, but when I just re-wrote it Im not
having the same issues. Now I'll just sit and stare at the screen >:O