Link to home
Start Free TrialLog in
Avatar of Mutsop
MutsopFlag for Belgium

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.

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

Open in new window


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?
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Avatar of Mutsop

ASKER

Thanks, I already found another solution.
I've set the e As System.Windows.Forms.MouseEventArgs 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.
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

Open in new window

While You paint only Lines, you can use ControlPaint.DrawReversibleLine

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.
Avatar of Mutsop

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.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mutsop

ASKER

This is perfect thanks :)