954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can I get Mouse Position?

How can I get the Mouse Position from the mousemove event inthe VB.net?

I can't find out how to do this.. :rolleyes:

vbpro
Newbie Poster
2 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

How can I get the Mouse Position from the mousemove event inthe VB.net?

I can't find out how to do this.. :rolleyes:

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

        Label1.Text = "X." & e.X & vbCrLf & "Y." & e.Y
        
    End Sub
Mr.Stupid
Newbie Poster
2 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

        Label1.Text = "X." & e.X & vbCrLf & "Y." & e.Y
        
    End Sub


hi,
but these code above only works inside the form.

how to get the mouse coordinates / position by using VB2005.NET in anywhere?

Benuz
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

you cant get the coordinates of the pointer outside the form..

if you wish to.. customize windows!! :lol:

iTaChi
Newbie Poster
21 posts since Mar 2007
Reputation Points: 29
Solved Threads: 2
 

you would need to use DirectX or something for that

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 600
 

do you want to trap mouse co-ordinates any where on the screen ?

debasisdas
Posting Genius
6,870 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

yes

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 600
 
Dim MyPointAPI As POINTAPI
Private Type POINTAPI
  X As Long
  Y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Public Sub Timer1_Timer()
  l = GetCursorPos(MyPointAPI)
  Label1.Caption = CStr(MyPointAPI.X) & ", " & CStr(MyPointAPI.Y)
End Sub
Skullmagic
Newbie Poster
1 post since Jul 2007
Reputation Points: 14
Solved Threads: 0
 

I have not been using VB for very long but this is how I have been getting the mouse postion on the screen.

Dim MousePosition As Point
        MousePosition = Cursor.Position


I hope this helps:)

blondie.simon
Light Poster
27 posts since Jul 2007
Reputation Points: 34
Solved Threads: 0
 

I know this thread is old, but I'm confused as to why so many people think this impossible, at least without using an API.

blondie.simon was on the right tracks.

Here's how:

dim mousePos as point = me.mouseposition

dim mousePosX as integer = form1.mouseposition.x
dim mousePosY as integer = form1.mouseposition.y


Simples see!

And to capture the location when the mouse is moving:

Private Sub mouseMove_Capture(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _ 
MyBase.MouseMove
        Dim mousePos As point= Me.MousePosition
End Sub

Note, that the above only functions when the mouse is not over any other form object, but the form itself. It is possible to only capture the mouse movement within a single object too, like a label or picture box for example.

WarMacre
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Better still, use:

dim mouseLoc as point = myControl.pointToClient(myControl.Cursor)


This gets the location relative to the container Form, not the screen.

Function:

Private Function getMouseLoc() As Point
        getMouseLoc = Me.PointToClient(Me.Cursor.Position)
        Return getMouseLoc
End Function

dim mouseLoc as point = getMouseLoc()


Or

Dim mouseLocX as integer = getMouseLoc.X

Dim mouseLocY as integer = getMouseLoc.Y
WarMacre
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

        Label1.Text = "X." & e.X & vbCrLf & "Y." & e.Y
        
    End Sub

So I' a re-beginner familiar with older BASIC stuff but not a VISUAL expert. If the MouseMove stuff is gone from the 2010 version toolbox or whatever, how do you do it now? Thanks.

DOT208
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
Structure PointAPI
        Public x As Int32 ' Integer if you prefer
        Public y As Int32 ' Integer if you prefer
    End Structure

Public Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As PointAPI) As Boolean

    Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Int32, ByRef lpPoint As PointAPI) As Int32

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim lpPoint As PointAPI

        GetCursorPos(lpPoint)
        Label1.Text = "ΣΥΝΤΕΤΑΓΜΕΝΗ Χ είναι:  " & CStr(lpPoint.x)
        Label2.Text = "ΣΥΝΤΕΤΑΓΜΕΝΗ Υ είναι:  " & CStr(lpPoint.y)
        Timer1.Enabled = False
    End Sub
ergreco
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Its not that difficult at all, i found it out just a few days ago having the same problem, my method even get the coordinates outside your form, anywhere. First add 2 textboxes, on for the x and one for the y coordinate. Then Add a timer. Make sure its enabled and the interval is 1. Then double click on the timer and write:
TextBox1.Text = MousePosition.X
TextBox2.Text = MousePosition.Y

shekoasinger
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

How do you get mouse click coordinates inside a picture box?

computbill
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Tmr.Tick
        Dim mspt = New Point(Cursor.Position)
        Me.Text = "x:" & mspt.X & "y: " & mspt.Y
End Sub
Daleap
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You