How can I get the Mouse Position from the mousemove event inthe VB.net?
I can't find out how to do this.. :rolleyes:
Two common needs in this thread are: getting coordinates inside a control (like a PictureBox) and tracking the mouse beyond your form.
For image work, do not use the control coordinates directly if the PictureBox uses SizeMode.Zoom. You must map from control space to image pixels. Example for a click inside a zoomed PictureBox:
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
Dim pb = DirectCast(sender, PictureBox)
Dim img = pb.Image
If img Is Nothing OrElse pb.SizeMode <> PictureBoxSizeMode.Zoom Then Return
Dim ratioX As Double = pb.ClientSize.Width / CDbl(img.Width)
Dim ratioY As Double = pb.ClientSize.Height / CDbl(img.Height)
Dim scale As Double = Math.Min(ratioX, ratioY)
Dim scaledW As Double = img.Width * scale
Dim scaledH As Double = img.Height * scale
Dim offsetX As Double = (pb.ClientSize.Width - scaledW) / 2.0
Dim offsetY As Double = (pb.ClientSize.Height - scaledH) / 2.0
Dim px = e.X - offsetX, py = e.Y - offsetY
If px < 0 OrElse py < 0 OrElse px > scaledW OrElse py > scaledH Then Return ' outside image
Dim imgX As Integer = CInt(px / scale), imgY As Integer = CInt(py / scale)
' use imgX, imgY (image pixel coordinates)
End Sub See PictureBox.SizeMode.
To observe mouse moves across your whole WinForms app (not the entire OS), implement an IMessageFilter and register it with Application.AddMessageFilter. For truly global tracking outside your process, install a low-level mouse hook via SetWindowsHookEx(WH_MOUSE_LL); be mindful of 32/64-bit, performance, and unhooking.
For ASP.NET/web pages, you must capture the mouse in JavaScript, e.g.:
document.addEventListener('mousemove', e => { /* e.clientX, e.clientY */ }); Reference: MDN MouseEvent.clientX/clientY.
Jump to Post— iTaChi 19you cant get the coordinates of the pointer outside the form..
if you wish to.. customize windows!! :lol:
Jump to Post— jbennet 1,618you would need to use DirectX or something for that
Jump to Post— debasisdas 580do you want to trap mouse co-ordinates any where on the screen ?
Jump to Post— jbennet 1,618yes
Jump to Post— blondie.simon 15I 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.PositionI hope this helps:)
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.