如何在Directx游戏中模拟鼠标点击
How to simulate mouse click in a Directx game
我有一个用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);
}
这是恢复游戏窗口点击点和最小化游戏窗口。
它的工作很好,但只是当我不移动鼠标…
我搜索别的东西。我想在不移动的情况下点击鼠标。甚至可以在游戏中做到这一点?我没有任何处理按钮,我想要点击,因为这是一个游戏…
p。年代对不起,我的英语不好。
我的一些模拟鼠标点击非活动窗口的代码如下:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
// int MouseX
// int MouseY
// public static readonly uint WM_LBUTTONUP = 0x202;
// public static readonly uint WM_LBUTTONDOWN = 0x201;
int lparam = MouseX & 0xFFFF | (MouseY & 0xFFFF) << 16;
int wparam = 0;
PostMessage(windowHandle, WM_LBUTTONDOWN, wparam, lparam);
Thread.Sleep(10);
PostMessage(windowHandle, WM_LBUTTONUP, wparam, lparam);
您可以尝试使用sendput。这里是文档:https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
下面是我的例子:
包括:
#include <stdio.h>
#include <cstdlib>
#include <windows.h>
#include <winuser.h>
#include <conio.h>
INPUT input;
int x = 889;
int y = 451;
`// Mouse click up
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = x * float(65536 / GetSystemMetrics(SM_CXSCREEN)); //x being coord in pixels
input.mi.dy = y * float(65536 / GetSystemMetrics(SM_CYSCREEN)); //y being coord in pixels
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN);
input.mi.time = 0;
SendInput(1, &input, sizeof(input));`
`
//Mouse click down
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = x * float(65536 / GetSystemMetrics(SM_CXSCREEN)); //x being coord in pixels
input.mi.dy = y * float(65536 / GetSystemMetrics(SM_CYSCREEN)); //y being coord in pixels
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP);
input.mi.time = 0;
SendInput(1, &input, sizeof(input));`
And You can do something like this to make sure you are in the focused window:
HWND hWnd = ::FindWindowEx(0, 0, "YourGame - Use Spy++", 0);
DWORD dwThreadID = GetWindowThreadProcessId(hWnd, NULL);
AttachThreadInput(dwThreadID, GetCurrentThreadId(), true);
SetForegroundWindow(hWnd);
SetActiveWindow(hWnd);
SetFocus(hWnd);
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(), NULL),GetCurrentThreadId(), TRUE);
相关文章:
- 如何在C++中模拟鼠标光标的移动
- 如何在后台模拟鼠标单击到 DirectX 应用程序?
- .EXE窗口中的鼠标模拟
- 处理 iOS 触摸以模拟鼠标以C++输入库
- 模拟鼠标按钮按QT 5.3仅第一次工作
- 在使用发送输入 API 模拟鼠标单击时是否需要引入延迟
- C++模拟鼠标左键单击最小化程序
- 如何用Qt模拟鼠标事件
- 有没有一种方法可以模拟从鼠标右键菜单发送到控制台的调用
- 无线键盘/鼠标应用程序:模拟击键
- 如何模拟当前打开窗口的鼠标滚动
- 在Qt中生成(模拟)假鼠标事件
- 以C++模拟鼠标和键盘输入
- 模拟鼠标单击而不移动光标
- 如何将控制器模拟棒映射到鼠标
- c++鼠标点击模拟代码
- 如何在Linux和Windows上的Qt中模拟所有鼠标和键盘事件
- 是否有可能模拟鼠标移动和鼠标点击隐藏窗口
- 如何模拟鼠标移动
- 如何模拟鼠标同时在多个窗口中移动