Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

draw line from center to mouseposition [vb.net]

Posted on 2011-09-13
8
Medium Priority
?
431 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

927 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