如何获得vb中鼠标在窗口的坐标

来源:百度知道 编辑:UC知道 时间:2024/06/05 17:07:42
当鼠标在窗口中某一点单击时,显示鼠标坐标数值!

方法一: 直接用 MouseDown事件
举例:(这种方法获取的是点的位置在窗体中,却该对象有MouseDown事件才行,其坐标值为所在容器的相对坐标值)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Cls
Print "X:"; X, "Y:"; Y
End If
End Sub

方法二:用API函数GetCursorPos来获取位置,用GetAsyncKeyState来获取是否按下左键
这种方法在任何时候都可以获取,哪怕鼠标不在应用程序内也行
在窗体上添加一个计时器,设置Interval属性为10
获取的位置的数字,是屏幕中的位置(不是相对窗体的)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Sub Timer1_Timer()
Dim P As POINTAPI
X = GetAsyncKeyState(1)
If X = -32767 Then 'x返回的是16位整数,最高位为1,表明按下
Cls
Print "左键按下",
GetCursorPos P