VB后台点击,执行窗口指定位置点击.就是相对坐标.问题

PrivateDeclareFunctionFindWindowLib"user32"Alias"FindWindowA"(ByVallpClassNameAsStrin... Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type

Dim game As Long

Private Sub Command1_Click()
Dim a As Long, b As Long
Dim point As POINTAPI
a = 400
b = 300
X = point.X
Y = point.Y

game = FindWindow(vbNullString, "xxx执行窗口")
ClientToScreen game, point.X, point.Y

mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '模拟鼠标的左键单击!
End Sub

哪里错了。谢谢高手解答。或者给一个好的代码。完整一点。
我是用api的 ClientToScreen函数。如果其他可以的话。请告诉我一下。谢谢
展开
 我来答
joforn
2008-04-08 · TA获得超过112个赞
知道小有建树答主
回答量:169
采纳率:0%
帮助的人:236万
展开全部
Option Explicit

'在VB窗体上添加两个CommandButton,使用默认名称,即可观察测试效果
Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) 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 GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command1_Click()
Dim Point0 As POINTAPI
Dim Point1 As POINTAPI

GetCursorPos Point0 '获取当前鼠标坐标

Point1.X = 2 '指定偏移坐标X值,实际应用时视目标窗体而定
Point1.Y = 2 '指定偏移坐标Y值,实际应用时视目标窗体而定

'将偏移Command2的偏移坐标转换成屏幕坐标
'实际应用时使用指定窗口的句柄
ClientToScreen Command2.hWnd, Point1

'将鼠标移动到转换后的Point1坐标,即指定窗口的偏移坐标
SetCursorPos Point1.X, Point1.Y

'模拟鼠标左键Click动作
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&

'恢复原鼠标坐标
SetCursorPos Point0.X, Point0.Y
End Sub

Private Sub Command2_Click()
Debug.Print "Click"
End Sub
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式