Solved

draw line from center to mouseposition [vb.net]

Posted on 2011-09-13
8
420 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 20

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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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