vb获取相对坐标
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
获取窗口相对坐标
示例一:无论某窗口如何改变,单击窗体某相对位置
实现:一窗体,两按钮,单击按钮一时,光标移动到按钮二上并单击(并非诸如call command2_click()之类);
重点在于坐标转换。
'两CommamdButton
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Type POINT
x As Long
y As Long
End Type
Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Const MOUSEEVENTF_LEFTUP As Integer = &H4
Private Sub Command1_Click() '测试键
Dim re As RECT, pt As POINT, x As Long, y As Long GetWindowRect Me.hwnd, re '获取窗口矩形信息
x = re.left + (Command2.left + Command2.Width / 2) / Screen.TwipsPerPixelX
y = re.top + (Command2.top + Me.Height - Me.ScaleHeight + Command2.Height / 2) / Screen.TwipsPerPixelY SetCursorPos x, y '移动光标至(x,y)
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub
'目标按钮
Private Sub Command2_Click()
MsgBox "You have clicked me !"
End Sub
示例二:坐标三步转换,窗体上有图像框,图像框上有坐标系,显示鼠标在坐标系位置
注:坐标系为数学常用坐标系,原点在左下角
实现:获取光标位置,转换成窗体位置,转换成图像框位置,转换为坐标系位置。
'一PictureBox
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
Dim pt As POINTAPI
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
GetCursorPos pt '获取鼠标屏幕位置
ScreenToClient Me.hwnd, pt '转换成窗口位置
If pt.x > Picture1.left / Screen.TwipsPerPixelX And _
pt.x < (Picture1.left + Picture1.Width) / Screen.TwipsPerPixelX _ And pt.y > Picture1.top / Screen.TwipsPerPixelY And _
pt.y < (Picture1.top + Picture1.Height) / Screen.TwipsPerPixelY Then Picture1.Cls
Picture1.Print pt.x - Picture1.left / Screen.TwipsPerPixelX & "," & _
(Picture1.top + Picture1.Height) / Screen.TwipsPerPixelY - pt.y End If
End Sub
作者:西门吹雪
玉树临风个人工作室:。