Solved

draw line from center to mouseposition [vb.net]

Posted on 2011-09-13
8
407 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:Mutsop
8 Comments
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
ID: 36529938
0
 
LVL 3

Author Comment

by:Mutsop
ID: 36529973
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36530252
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

0
 
LVL 15

Expert Comment

by:x77
ID: 36530942
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.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36530968
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.
0
 
LVL 3

Author Comment

by:Mutsop
ID: 36534484
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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36535869
DrawReversible line just inverts the pixels on the screen.

Here's an example:
Public Class Test

    Private ptStart As Point
    Private ptEnd As Point

    Private Sub Test_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ptStart = Me.PointToScreen(Me.gameBall.Location)
            ptEnd = Cursor.Position
            ControlPaint.DrawReversibleLine(ptStart, ptEnd, Color.Black)
            Cursor.Clip = Me.RectangleToScreen(Me.ClientRectangle)
        End If
    End Sub

    Private Sub Test_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ControlPaint.DrawReversibleLine(ptStart, ptEnd, Color.Black)
            ptEnd = Cursor.Position
            ControlPaint.DrawReversibleLine(ptStart, ptEnd, Color.Black)
        End If
    End Sub

    Private Sub Test_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ControlPaint.DrawReversibleLine(ptStart, ptEnd, Color.Black)
            Cursor.Clip = Nothing
        End If
    End Sub

End Class

Open in new window

0
 
LVL 3

Author Closing Comment

by:Mutsop
ID: 36560559
This is perfect thanks :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now