【发布时间】:2012-12-15 04:20:26
【问题描述】:
我有一个用 Directx 编写的游戏(不是我的,它是 mmo 游戏)。游戏窗口未激活(未最小化,只是位于其他窗口的后面,但如果可能,也可以将其最小化)。
我想模拟鼠标点击 x、y 位置。 当我在游戏中点击时,Spy++ 不会在消息中显示任何内容。
现在我就是这样做的:
private void start_Click(object sender, EventArgs e)
{
IntPtr ActualWindow = GetActiveWindow();
ShowWindow(hWnd, ShowWindowCommands.Restore); //show game window
Thread.Sleep(50); //a little slow down
ClickOnPoint(hWnd, new Point(692, 87)); //click on x,y
Thread.Sleep(50); //again a little slow down
ShowWindow(hWnd, ShowWindowCommands.Minimize);//minimize game window
ShowWindow(ActualWindow, ShowWindowCommands.Restore);//restore last active window
//sleep is needed for click, if i will do it too fast game will not detect the click
}
private void ClickOnPoint(IntPtr wndHandle, Point clientPoint)
{
POINT oldPoint;
GetCursorPos(out oldPoint);
ClientToScreen(wndHandle, ref clientPoint);
/// set cursor on coords, and press mouse
SetCursorPos(clientPoint.X, clientPoint.Y);
mouse_event(0x00000002, 0, 0, 0, UIntPtr.Zero); /// left mouse button down
Thread.Sleep(18);
mouse_event(0x00000004, 0, 0, 0, UIntPtr.Zero); /// left mouse button up
Thread.Sleep(15);
/// return mouse
SetCursorPos(oldPoint.X, oldPoint.Y);
}
点击点恢复游戏窗口,最小化游戏窗口。
效果很好,但是当我不移动鼠标时...
我搜索别的东西。我想点击鼠标而不真正移动它。甚至有可能在游戏中做到这一点?我没有想要点击的按钮的句柄,因为它是一个游戏......
附言 对不起我的英语。
【问题讨论】:
-
没有人在这个游戏中玩 :D,我只想点击 PLAY 按钮 :D,所以它不是机器人,我只是想知道我该怎么做以及是否有可能。这场比赛是冠军决斗,我的技能太低(目前)无法编写 AI 或神经网络:P
-
我建议
SendMessage而不是试图控制另一个窗口和鼠标。 -
有点失败 :D 我正在尝试 SendMesseage 但没有效果。 Spy++ 没有显示任何内容,因为我使用的是 64 位版本的 Spy++,32 位可以正常工作。
标签: c# c++ mouse simulation simulate