'运行此程序需要再窗体添加控件Timer
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
Timer1.Interval = 10 '设置获取鼠标坐标间隔
End Sub
Private Sub Timer1_Timer()
Dim P As POINTAPI
GetCursorPos P '获取鼠标在屏幕中的位置
ScreenToClient Me.hwnd, P '转换为本窗体的坐标
Dim t As Boolean
t = P.x >= 0 And P.y >= 0 And P.x < Me.Width / Screen.TwipsPerPixelX And P.y <= Me.Height / Screen.TwipsPerPixelY
If t Then Me.Caption = "x=" & P.x & "y=" & P.y '按像素显示坐标
'If t Then Me.Caption = "x=" & P.x * Screen.TwipsPerPixelX & "y=" & P.y * Screen.TwipsPerPixelY '按缇显示坐标
End Sub
'运行此程序需要再窗体添加控件Timer
Option
Explicit
Private
Declare
Function
GetCursorPos
Lib
"user32"
(lpPoint
As
POINTAPI)
As
Long
Private
Declare
Function
ScreenToClient
Lib
"user32"
(ByVal
hwnd
As
Long,
lpPoint
As
POINTAPI)
As
Long
Private
Type
POINTAPI
x
As
Long
y
As
Long
End
Type
Private
Sub
Form_Load()
Timer1.Interval
=
10
'设置获取鼠标坐标间隔
End
Sub
Private
Sub
Timer1_Timer()
Dim
P
As
POINTAPI
GetCursorPos
P
'获取鼠标在屏幕中的位置
ScreenToClient
Me.hwnd,
P
'转换为本窗体的坐标
Dim
t
As
Boolean
t
=
P.x
>=
0
And
P.y
>=
0
And
P.x
<
Me.Width
/
Screen.TwipsPerPixelX
And
P.y
<=
Me.Height
/
Screen.TwipsPerPixelY
If
t
Then
Me.Caption
=
"x="
&
P.x
&
"y="
&
P.y
'按像素显示坐标
'If
t
Then
Me.Caption
=
"x="
&
P.x
*
Screen.TwipsPerPixelX
&
"y="
&
P.y
*
Screen.TwipsPerPixelY
'按缇显示坐标
End
Sub
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim mp As POINTAPI
Private Sub Timer1_Timer()
GetCursorPos mp
Me.Caption = "X:" & Str(mp.x) & "Y:" & Str(mp.y)
End Sub
代码:
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim mp As POINTAPI
Private Sub Timer1_Timer()
GetCursorPos mp
Me.Caption = "X:" & Str(mp.x) & "Y:" & Str(mp.y)
End Sub