Mutsop
asked on
draw line from center to mouseposition [vb.net]
Hi,
I'm trying to make a little golf game in vb.net.
Now I seem to misunderstand the position (coordinates) relative or absolute to the form.
it seems for some reason that "Cursor.Position.X" uses the cursor location of the screen and not of the form. Any idea how to fix this?
Also how do I dispose of this line if it was created at runtime?
I'm trying to make a little golf game in vb.net.
Now I seem to misunderstand the position (coordinates) relative or absolute to the form.
Public Class Test
Private Sub drawLine()
Dim formGraphics As Drawing.Graphics
formGraphics = Me.CreateGraphics
formGraphics.DrawLine(Pens.Black, gameBall.Location.X, gameBall.Location.Y,
Cursor.Position.X, Cursor.Position.Y)
End Sub
Private Sub Test_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
drawLine()
End Sub
Private Sub Test_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
drawLine()
End Sub
End Class
it seems for some reason that "Cursor.Position.X" uses the cursor location of the screen and not of the form. Any idea how to fix this?
Also how do I dispose of this line if it was created at runtime?
ASKER
Thanks, I already found another solution.
I've set the e As System.Windows.Forms.Mouse EventArgs as parameter in drawLine and used "e.X, e.Y".
Now the only problem I have is how to dispose of this line each time my mouse moves.
I've set the e As System.Windows.Forms.Mouse
Now the only problem I have is how to dispose of this line each time my mouse moves.
Use the Paint() event...something like:
Public Class Test
Private pt As Point
Private Sub Test_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
pt = New Point(e.X, e.Y)
Me.Refresh()
End Sub
Private Sub Test_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawLine(Pens.Black, gameBall.Location.X, gameBall.Location.Y, pt.X, pt.Y)
End Sub
End Class
While You paint only Lines, you can use ControlPaint.DrawReversibl eLine
Note that this use screen coordinates (use PointtoScreen to get screen coordinates)
When you draw a line, You see the line.
When You Draw same line, you erase the line.
Note that Refresh or Invalidate can cause flicker.
Note that this use screen coordinates (use PointtoScreen to get screen coordinates)
When you draw a line, You see the line.
When You Draw same line, you erase the line.
Note that Refresh or Invalidate can cause flicker.
That'll work to...though it can cause artifacts if another window partially obscures yours or if you visit another window and come back. I like DrawReversibleLine() while the mouse is being held DOWN.
ASKER
k so for DrawReversibleLine I need to use the end point of my just created line as Start and start with end? But wouldn't this cause a memory increase? as all lines I created still exists...
I mean as I read, DrawReversibleLine just draws another line on the form.
I mean as I read, DrawReversibleLine just draws another line on the form.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is perfect thanks :)
http://www.blackwasp.co.uk/GetMousePosition.aspx
and
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient.aspx