vb实现获取鼠标在屏幕上的坐标并显示文本框1中

2025-05-23 09:27:15
推荐回答(2个)
回答(1):

添加一个窗口,一个文本框,一个TIMER控件,代码如下:

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

Private Type POINTAPI '定义点(Point)结构
X As Long '点在X坐标(横坐标)上的坐标值
Y As Long '点在Y坐标(纵坐标)上的坐标值
End Type

Sub PrintCursorPos()
Dim dl As Long
Dim MyPoint As POINTAPI
dl& = GetCursorPos(MyPoint) '调用函数,获取屏幕鼠标坐标
'Debug.Print "X=" & Str(MyPoint.X) & " and " & "Y=" & Str(MyPoint.Y)
text1 = "当前横坐标:" & Str(MyPoint.X) & " 当前纵坐标:" & Str(MyPoint.Y)
End Sub

Private Sub Command1_Click() '开始检测
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
PrintCursorPos
End Sub

回答(2):

可以通过调用系统API实现,具体方法问渡娘,资料很多的.