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

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

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
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?

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

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

you would need to use DirectX or something for that

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

yes

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

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:)

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.

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
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.

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

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

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

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
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
label1.text = Cursor.Position.X & "X" & Cursor.Position.Y
End Sub

The preceding code gives the coordinate relative to the top-left of the screen (which may be useful), but does not work when the cursor leaves the form and when the cursor is over another control on the form.

Hi,

This thread is already several years old and you should have started a new one.
However, add a label, Timer and some other controls and it shoud work:

Public Class Form1
    Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer

    Dim mousepos As Point
    ' This stores the cordinates of the mouse cursors location
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim R As Long = GetCursorPos(mousepos) ' You'll get your location in mousepos
        Label1.Text = "Mouse Cursor Location : " & mousepos.X & "x" & mousepos.Y

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.