如何在Directx游戏中模拟鼠标点击

如何在Directx游戏中模拟鼠标点击

本文介绍了如何在Directx游戏中模拟鼠标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Directx写的游戏(不是我的游戏)。游戏窗口不是活动的(不是最小化,只是它在其他窗口后面,如果可能,它也可以最小化)。



我想模拟鼠标点击x ,y位置。
当我在游戏中点击时,Spy ++在邮件中不显示任何内容。



现在我只是这样:

  private void start_Click(object sender,EventArgs e)
{
IntPtr ActualWindow = GetActiveWindow

ShowWindow(hWnd,ShowWindowCommands.Restore); //显示游戏窗口
Thread.Sleep(50); //有点慢下来
ClickOnPoint(hWnd,new Point(692,87)); //点击x,y
Thread.Sleep(50); //再次减慢
ShowWindow(hWnd,ShowWindowCommands.Minimize); //最小化游戏窗口
ShowWindow(ActualWindow,ShowWindowCommands.Restore); //恢复最后一个活动窗口
//如果我会做太快游戏不会检测到点击
}

private void ClickOnPoint(IntPtr wndHandle,Point clientPoint)
{
POINT oldPoint;
GetCursorPos(out oldPoint);

ClientToScreen(wndHandle,ref clientPoint);

///将光标置于坐标上,然后按鼠标
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);

///返回鼠标
SetCursorPos(oldPoint.X,oldPoint.Y);
}

这是还原游戏窗口点击点和最小化游戏窗口。

这很好,但只是当我不移动鼠标...



我搜索别的东西。我想点击鼠标,而不移动它的真实。它甚至可以在游戏中做吗?我没有任何处理按钮我想点击,因为它是一个游戏...



PS
对不起我的英语。

解决方案

我的一些模拟鼠标点击非活动窗口的代码如下:

  [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);


I have a game written in Directx (not mine it's mmo game). The game window isn't active (not minimized, just it's behind other windows but if is possible it can be minimized also).

I want to simulate mouse click on x, y position.Spy++ doesn't show anything in message when i'm clicking in game.

For now i did just that:

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);
    }

It's restore game window click on point and minimize game window.

It's works good, but just when i'm not moving mouse...

I search something else. I want to click mouse without moving it for real. It's even possible do it in game? I don't have any handle for button i want to click because it's a game...

P.SSorry for my english.

解决方案

Some of my code for simulating a mouse click on a non-active window looks like:

[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);

这篇关于如何在Directx游戏中模拟鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:42