VB获取鼠标在窗体上的坐标
不要用form的mousemove事件我要的是鼠标在窗体上的相对坐标不是鼠标在屏幕上的坐标就是不管我在按钮上移动或者在窗体上移动都能获得鼠标在窗体上的坐标2位看清楚我是说...
不要用form的mousemove事件
我要的是鼠标在窗体上的相对坐标 不是鼠标在屏幕上的坐标
就是不管我在按钮上移动或者在窗体上移动都能获得鼠标在窗体上的坐标
2位 看清楚我是说在窗体上的坐标 不是在屏幕上的坐标 展开
我要的是鼠标在窗体上的相对坐标 不是鼠标在屏幕上的坐标
就是不管我在按钮上移动或者在窗体上移动都能获得鼠标在窗体上的坐标
2位 看清楚我是说在窗体上的坐标 不是在屏幕上的坐标 展开
5个回答
展开全部
'运行此程序需要再窗体添加控件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 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
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
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
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询