用 API 函数 GetCarePos,给个示例做参考(窗体上放置一个 Timer、一个 Label):
Option Explicit
Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim a As POINTAPI, b As Long
Private Sub Timer1_Timer()
GetCaretPos a
Label1.Caption = a.x, a.y
End Sub
Private Sub Form_Load()
Timer1.Enable = True
Timer1.Interval = 100
End Sub
'将代码写在窗体的“鼠标移动”(MouseMove)事件中,只要鼠标移动,就触发代码
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Top = Y '获取鼠标距窗体顶部的距离 Y,使文本框距窗体顶部的距离和鼠标相同
Text1.Left = X '获取鼠标距窗体左侧的距离 X,使文本框距窗体左侧的距离和鼠标相同
End Sub
使用用 API 函数 GetCarePos
Label1.Caption = a.x, a.y 出错,是你要 在 窗体上有个label1这个标签 控件, 你要先创建一个这样的控件来显示坐标的当前位置