Solved

draw line from center to mouseposition [vb.net]

Posted on 2011-09-13
8
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
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 86

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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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
 
LVL 86

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 86

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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