不要用form的mousemove事件
我要的是鼠标在窗体上的相对坐标 不是鼠标在屏幕上的坐标
就是不管我在按钮上移动或者在窗体上移动都能获得鼠标在窗体上的坐标
2位 看清楚我是说在窗体上的坐标 不是在屏幕上的坐标
VB获取鼠标在窗体上的坐标
答案:3 悬赏:40 手机版
解决时间 2021-02-04 08:56
- 提问者网友:像風在裏
- 2021-02-03 20:04
最佳答案
- 五星知识达人网友:怙棘
- 2021-02-03 20:29
'运行此程序需要再窗体添加控件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
全部回答
- 1楼网友:野味小生
- 2021-02-03 23:13
代码:
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
- 2楼网友:青尢
- 2021-02-03 22:06
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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯