浅谈游戏中的模拟点击程序

游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用c#调用windows api函数实现鼠标模拟操作的功能。首先通过结合findwindow和findwindowex寻找到窗体的按钮,在通过setcursorpos或mouse_event函数操作鼠标,同时涉及到通过spy++工具获取窗体消息的信息。
一。 windows api函数介绍
.net没有提供改变鼠标指针位置、模拟单机操作的函数,但是可以通过调用windows api函数实现。
[dllimport(“user32.dll”)]
static extern bool setcursorpos(int x,int y);
该函数用于设置鼠标的位置,其中x和y是相对于屏幕左上角的绝对位置。
[dllimport(“user32.dll”)]
static extern void mouse_event(mouseeventflag flags,int dx,int dy,uint data,uintptr extrainfo);
该函数不仅可以设置鼠标指针绝对位置,而且可以以相对坐标来设置位置。
其中flags标志位集,指定点击按钮和鼠标动作的多种情况.dx指鼠标沿x轴绝对位置或上次鼠标事件位置产生以来移动的数量.dy指沿y轴的绝对位置或从上次鼠标事件以来移动的数量.data如果flags为mouse_wheel则该值指鼠标轮移动的数量(否则为0),正值向前转动.extrainfo指定与鼠标事件相关的附加32位值。
[dllimport(“user32.dll”)]
static extern intptr findwindow(string strclass, string strwindow);
该函数根据类名和窗口名来得到窗口句柄,但是这个函数不能查找子窗口,也不区分大小写。如果要从一个窗口的子窗口查找需要使用findwindowex函数。
[dllimport(“user32.dll”)]
static extern intptr findwindowex(intptr hwndparent, intptr hwndchildafter,
string strclass, string strwindow);
该函数获取一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配,该函数查找子窗口时从排在给定的子窗口后面的下一个子窗口开始。其中参数
hwnparent为要查找子窗口的父窗口句柄,若该值为null则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。
hwndchildafter子窗口句柄,查找从在z序中的下一个子窗口开始,子窗口必须为hwnparent直接子窗口而非后代窗口,若hwnchildafter为null,查找从父窗口的第一个子窗口开始。
strclass指向一个指定类名的空结束字符串或一个标识类名字符串的成员的指针。
strwindow指向一个指定窗口名(窗口标题)的空结束字符串。若为null则所有窗体全匹配。
返回值:如果函数成功,返回值为具有指定类名和窗口名的窗口句柄,如果函数失败,返回值为null.
二。 鼠标自动点击按钮和查看鼠标运行轨迹
首先创建一个c#工程,设计的窗体如下图所示,同时添加timer时间器控件:
然后添加的如下代码,即可实现鼠标模拟技术及自动操作鼠标:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
//引用新命名空间
using system.runtime.interopservices; //structlayout
namespace mouseaction
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
//结构体布局 本机位置
[structlayout(layoutkind.sequential)]
struct nativerect
{
public int left;
public int top;
public int right;
public int bottom;
}
//将枚举作为位域处理
[flags]
enum mouseeventflag : uint //设置鼠标动作的键值
{
move = 0x0001, //发生移动
leftdown = 0x0002, //鼠标按下左键
leftup = 0x0004, //鼠标松开左键
rightdown = 0x0008, //鼠标按下右键
rightup = 0x0010, //鼠标松开右键
middledown = 0x0020, //鼠标按下中键
middleup = 0x0040, //鼠标松开中键
xdown = 0x0080,
xup = 0x0100,
wheel = 0x0800, //鼠标轮被移动
virtualdesk = 0x4000, //虚拟桌面
absolute = 0x8000
}
//设置鼠标位置
[dllimport(“user32.dll”)]
static extern bool setcursorpos(int x, int y);
//设置鼠标按键和动作
[dllimport(“user32.dll”)]
static extern void mouse_event(mouseeventflag flags, int dx, int dy,
uint data, uintptr extrainfo); //uintptr指针多句柄类型
[dllimport(“user32.dll”)]
static extern intptr findwindow(string strclass, string strwindow);
//该函数获取一个窗口句柄,该窗口雷鸣和窗口名与给定字符串匹配 hwnparent=null从桌面窗口查找
[dllimport(“user32.dll”)]
static extern intptr findwindowex(intptr hwndparent, intptr hwndchildafter,
string strclass, string strwindow);
[dllimport(“user32.dll”)]
static extern bool getwindowrect(handleref hwnd, out nativerect rect);
//定义变量
const int animationcount = 80;
private point endposition;
private int count;
private void button1_click(object sender, eventargs e)
{
nativerect rect;
//获取主窗体句柄
intptr ptrtaskbar = findwindow(“windowsforms10.window.8.app.0.2bf8098_r11_ad1”, null);
if (ptrtaskbar == intptr.zero)
{
messagebox.show(“no windows found!”);
return;
}
//获取窗体中“button1”按钮
intptr ptrstartbtn = findwindowex(ptrtaskbar, intptr.zero, null, “button1”);
if (ptrstartbtn == intptr.zero)
{
messagebox.show(“no button found!”);
return;
}
//获取窗体大小
getwindowrect(new handleref(this, ptrstartbtn), out rect);
endposition.x = (rect.left + rect.right) / 2;
endposition.y = (rect.top + rect.bottom) / 2;
//判断点击按钮
if (checkbox1.checked)
{
//选择“查看鼠标运行的轨迹”
this.count = animationcount;
movementtimer.start();
}
else
{
setcursorpos(endposition.x, endposition.y);
mouse_event(mouseeventflag.leftdown, 0, 0, 0, uintptr.zero);
mouse_event(mouseeventflag.leftup, 0, 0, 0, uintptr.zero);
textbox1.text = string.format(“{0},{1}”, mouseposition.x, mouseposition.y);
}
}
//tick:定时器,每当经过多少时间发生函数
private void movementtimer_tick(object sender, eventargs e)
{
int stepx = (endposition.x - mouseposition.x) / count;
int stepy = (endposition.y - mouseposition.y) / count;
count--;
if (count == 0)
{
movementtimer.stop();
mouse_event(mouseeventflag.leftdown, 0, 0, 0, uintptr.zero);
mouse_event(mouseeventflag.leftup, 0, 0, 0, uintptr.zero);
}
textbox1.text = string.format(“{0},{1}”, mouseposition.x, mouseposition.y);
mouse_event(mouseeventflag.move, stepx, stepy, 0, uintptr.zero);
}
}
}
同时自定义一个对话框,增加一个button按钮,其运行结果如下图所示:
可以看到当运行程序勾选“查看鼠标运行的轨迹”并点击“开始”按钮后,会通过findwindow和findwindowex函数查找窗体“form1”的“button1”按钮,并通过mouse_event移动鼠标和点击鼠标。其中函数原型为:
intptr findwindowex(
intptr hwndparent, // handle to parent window [父窗体句柄]
intptr hwndchildafter, // handle to child window [子窗体句柄]
string strclass, // class name [窗体类名]
string strwindow // window name [窗体名]
);
但是怎样找到窗体类名和按钮的类名呢?由于初学,很多窗体我都没有实现如qq,它需要用到一个叫spy++的工具。
ps:第一次制作gif格式动态图片,参照博客 http://blog.csdn.net/tangcheng_ok/article/details/8246792
三。 使用spy++工具获取窗体信息
如果修改代码为:
//获取任务栏句柄
intptr ptrtaskbar = findwindow(“shell_traywnd”,null);
//托盘通知句柄
intptr ptrstartbtn = findwindowex(ptrtaskbar, intptr.zero, “traynotifywnd”, null);
可以获取电脑底部任务栏的托盘通知句柄,其中通过spy++工具(vs中“工具”中自带)查找如下图所示:
同样,我通过spy++工具获取txt句柄,首先打开spy++工具,同时点击“查找窗口”按钮(望远镜),再点击“查找程序工具”中按钮拖拽至要查看的窗体中,点击“确定”按钮
这样就会显示这个txt的信息,同时可以右击“属性”显示窗体的类名、窗体题目、句柄等信息。
最后通过下面代码可以获取hello.txt的句柄:
//获取记事本句柄
intptr ptrtaskbar = findwindow(“notepad”, null);
intptr ptrstartbtn = findwindowex(ptrtaskbar, intptr.zero, “edit”, null);
再通过mouse_event操作鼠标,同时可以通过sendmessage将指定的消息发送到一个或多个窗口,postmessage将一个消息寄送到一个线程的消息队列后就立即返回。实现消息传递等功能,学习ing~
四。 总结
该篇文章主要讲述c#如何操作鼠标的事件,在制作游戏外挂或自动运行程序时非常实用,但遗憾的是在上面通过窗体名称“form1”获取窗体时总是失败,需要通过spy++获取它的类名来实现.why?同时如果想学习键盘模拟技术的可以研究setwindowshookex(安装钩子)、callnexthookex(下一个钩子)、unhookwindowshookex(卸载钩子)和鼠标hook实现很多技术。
希望文章对大家有所帮助,如果有错误或不足之处,请见谅~
(by:eastmount 2014年10月13日 晚上8点 http://blog.csdn.net/eastmount/)
参考资料-在线笔记:
本文主要参考书籍《c#网络变成高级篇之网页游戏辅助程序设计》张慧斌 王小峰著
1.c#获取qq聊天输入框中内容 http://www.csharpwin.com/csharpspace/9133r5654.shtml
2.c#查找窗口,findwindow用法(by-lybwwp)http://blog.csdn.net/lybwwp/article/details/8168553
3.findwindowex用法(by-coolszy) http://blog.csdn.net/coolszy/article/details/5523784
4.c# 隐藏任务栏开始按钮关闭shell(by-sshhbb)http://blog.csdn.net/sshhbb/article/details/6605976
5.任务栏句柄 http://blog.csdn.net/wangjieest/article/details/6943241
6.c#如何在外部程序的密码框内自动输入密码 http://biancheng.dnbcw.info/c/117849.html
7.c#实现对外部程序的调用操作 http://www.blue1000.com/bkhtml/c17/2012-11/70993.htm
8.百度知道 c# api函数findwindowex返回子窗体的值为零
9.百度知道 用c#操作api实现填写桌面窗体内的textbox并点击窗体按钮

OPPO千元新机官宣 将于9月16日亮相
诺基亚手机官网
海信发布U8E、U7E两款ULED超画质电视新品,3月底正式上市销售
固态继电器的基本工作原理是什么?
江苏省司法厅:引入区块链等先进技术 推进精准法律援助
浅谈游戏中的模拟点击程序
三星Galaxy S21 Ultra曝光:无缘屏下摄像头技术
莱迪思携手Helion推出开箱即用的ISP解决方案,加速嵌入式视觉应用设计
IC封装中的导电银胶和非导电胶有什么区别?
人工智能背景下的教育会有什么改变
亚马逊计划在送货车辆装上视频摄像头
润和软件与华为昇腾AI完成兼容性测试认证
软通动力人工智能与数据创新中心项目签约贵安新区
汇川技术反应釜系统解决方案推动现代化生物农药生产
无刷直流电机的优缺点
Timer测试方案 Timer测试平台实现 测试平台debug注意事项
任正非回应鸿蒙系统问题
我国工业互联网市场达3万亿
RS485与Modbus通信原理解读
EMC设计的3大规律及要素分析