C# 如何在Directx游戏中模拟鼠标单击
我有一个用Directx写的游戏(不是我的,它是mmo游戏)。游戏窗口未处于活动状态(未最小化,只是在其他窗口后面,但如果可能,也可以最小化) 我想模拟鼠标点击x,y的位置。 当我在游戏中单击时,Spy++不会在消息中显示任何内容 现在我只是这样做了:C# 如何在Directx游戏中模拟鼠标单击,c#,c++,mouse,simulation,simulate,C#,C++,Mouse,Simulation,Simulate,我有一个用Directx写的游戏(不是我的,它是mmo游戏)。游戏窗口未处于活动状态(未最小化,只是在其他窗口后面,但如果可能,也可以最小化) 我想模拟鼠标点击x,y的位置。 当我在游戏中单击时,Spy++不会在消息中显示任何内容 现在我只是这样做了: private void start_Click(object sender, EventArgs e) { IntPtr ActualWindow = GetActiveWindow(); ShowWi
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);
}
它的恢复游戏窗口点击点和最小化游戏窗口
它的工作原理很好,但就在我不移动鼠标的时候
我搜索别的东西。我想点击鼠标而不移动它。甚至有可能在游戏中这样做?我没有任何按钮手柄,我想点击,因为这是一个游戏
附言
对不起,我的英语不好。我的一些模拟鼠标点击非活动窗口的代码如下所示:
[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);
[DllImport(“user32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部boolpostmessage(int-hWnd、uint-Msg、int-wParam、int-lParam);
//int鼠标
//内特老鼠
//公共静态只读单元WM_LBUTTONUP=0x202;
//公共静态只读uint WM_LBUTTONDOWN=0x201;
int lparam=MouseX&0xFFFF |(MouseY&0xFFFF)我的一些模拟鼠标点击非活动窗口的代码如下所示:
[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);
[DllImport(“user32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部boolpostmessage(int-hWnd、uint-Msg、int-wParam、int-lParam);
//int鼠标
//内特老鼠
//公共静态只读单元WM_LBUTTONUP=0x202;
//公共静态只读uint WM_LBUTTONDOWN=0x201;
int lparam=MouseX&0xFFFF |(MouseY&0xFFFF)您可以尝试使用Sendiput。以下是文件:
以下是我的例子:
包括:
#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);
#包括
#包括
#包括
#包括
#包括
输入;
int x=889;
int y=451;
`//鼠标点击向上
input.type=输入\鼠标;
input.mi.mouseData=0;
input.mi.dx=x*float(65536/GetSystemMetrics(SM_CXSCREEN))//x是以像素为单位的坐标
input.mi.dy=y*float(65536/GetSystemMetrics(SM_CYSCREEN))//y以像素为单位进行协调
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN);
input.mi.time=0;
SendInput(1,&input,sizeof(input))`
`
//鼠标点击
input.type=输入\鼠标;
input.mi.mouseData=0;
input.mi.dx=x*float(65536/GetSystemMetrics(SM_CXSCREEN))//x是以像素为单位的坐标
input.mi.dy=y*float(65536/GetSystemMetrics(SM_CYSCREEN))//y以像素为单位进行协调
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP);
input.mi.time=0;
SendInput(1,&input,sizeof(input))`
您可以这样做以确保您处于聚焦窗口中:
HWND HWND=::FindWindowEx(0,0,“你的游戏-使用Spy++”,0);
DWORD dwThreadID=GetWindowThreadProcessId(hWnd,NULL);
AttachThreadInput(dwThreadID,GetCurrentThreadId(),true);
setforegroundindow(hWnd);
设置活动窗口(hWnd);
设置焦点(hWnd);
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(),NULL),GetCurrentThreadId(),TRUE);
您可以尝试使用Sendiput。以下是文件:
以下是我的例子:
包括:
#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);
#包括
#包括
#包括
#包括
#包括
输入;
int x=889;
int y=451;
`//鼠标点击向上
input.type=输入\鼠标;
input.mi.mouseData=0;
input.mi.dx=x*float(65536/GetSystemMetrics(SM_CXSCREEN))//x是以像素为单位的坐标
input.mi.dy=y*float(65536/GetSystemMetrics(SM_CYSCREEN))//y以像素为单位进行协调
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN);
input.mi.time=0;
SendInput(1,&input,sizeof(input))`
`
//鼠标点击
input.type=输入\鼠标;
input.mi.mouseData=0;
input.mi.dx=x*float(65536/GetSystemMetrics(SM_CXSCREEN))//x是以像素为单位的坐标
input.mi.dy=y*float(65536/GetSystemMetrics(SM_CYSCREEN))//y以像素为单位进行协调
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP);
input.mi.time=0;
SendInput(1,&input,sizeof(input))`
您可以这样做以确保您处于聚焦窗口中:
HWND HWND=::FindWindowEx(0,0,“你的游戏-使用Spy++”,0);
DWORD dwThreadID=GetWindowThreadProcessId(hWnd,NULL);
AttachThreadInput(dwThreadID,GetCurrentThreadId(),true);
setforegroundindow(hWnd);
设置活动窗口(hWnd);
设置焦点(hWnd);
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(),NULL),GetCurrentThreadId(),TRUE);
没有人在这个游戏中玩:D,我只想单击“玩”按钮:D,所以它不是机器人,我只想知道我怎么做,以及这是否可能。这个游戏是冠军决斗,我的技能太低(目前)无法编写AI或神经网络:PI建议SendMessage
,而不是尝试控制另一个窗口和鼠标。有点失败:d我尝试SendMessage,但没有效果。Spy++没有向我显示任何东西,因为我使用的是64位版本的Spy++,32位工作正常。没有人在这个游戏中玩:D,我只想点击播放按钮:D,所以它不是机器人,我只想知道我怎么做,如果可能的话。这个游戏是冠军决斗,我的技能太低(目前)无法编写AI或神经网络:PI建议SendMessage
,而不是尝试控制另一个窗口和鼠标。有点失败:d我尝试SendMessage,但没有效果。Spy++没有向我显示任何东西,只是因为我使用的是64位版本的Spy++,32位可以正常工作。在函数MakeLParam中,我有:MouseX&0xFFFF |(MouseY&0xFFFF)是游戏中的Play
按钮还是在加载器上?另外,说游戏在我的国家(美国)不可用。下载链接:我想尝试单击游戏中的登录按钮。使用process explorer,launcher.ex