How to Retrieve Mouse Cursor Coordinates in Visual BasicID: Q114777
|
Applications such as Paint programs use functions from the Windows API (Application Programming Interface) to retrieve Mouse coordinates that in turn help the user design and paint their picture. This article shows by example how to use the Windows API GetCursorPos() function to retrieve the mouse coordinates from Visual Basic.
The GetCursorPos() function returns a structure that contains the current
position of the caret. To call the GetCursorPos() function from Visual
Basic, you have to set up a Type structure. This is shown in the
following example.
Type POINTAPI ' This holds the logical cursor information
x As Integer
y As Integer
End Type
Declare Sub GetCursorPos Lib "User" (lpPoint As POINTAPI)
Sub Form_Load ()
timer1.Interval = 100
End Sub
Sub Timer1_Timer ()
Dim rect As POINTAPI
' Get the current mouse cursor coordinates:
Call GetCursorPos(rect)
FORM1.Cls
' Print out current position on the form:
Print "Current X = " & rect.x
Print "Current Y = " & rect.y
End Sub
"Programming Windows: the Microsoft Guide to Writing Applications for
Windows 3," Charles Petzold, Microsoft Press, 1990
"Microsoft Windows Software Development Kit" Reference Manuals and
on-line help
WINSDK.HLP file shipped with Microsoft Windows 3.0 Software
Development Kit
"Visual Basic Programmers Guide to the Windows API", Daniel Appleman,
Ziff Davis Press, 1993
Additional query words: 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 9, 1999