C#之鼠标模拟技术
游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C#调用Windows API函数实现鼠标模拟操作的功能.首先通过结合FindWindow和FindWindowEx寻找到窗体的按钮,在通过SetCursorPos或mouse_event函数操作鼠标,同时涉及到通过spy++工具获取窗体消息的信息.
鼠标自动点击按钮和查看鼠标运行轨迹:
首先创建一个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; //StructLayoutnamespace 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); } }}
C#之鼠标模拟技术的更多相关文章
- [转]C# 系统应用之鼠标模拟技术及自动操作鼠标
原文网址: C# 系统应用之鼠标模拟技术及自动操作鼠标 游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C# ...
- C# 系统应用之鼠标模拟技术及自动操作鼠标
游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C#调用Windows API函数实现鼠标模拟操作的功能.首先通过结合Fi ...
- C# 调用windows api 操作鼠标、键盘、窗体合集...更新中
鼠标操作window窗体合集...更新中 1.根据句柄查找窗体 引自http://www.2cto.com/kf/201410/343342.html 使用SPY++工具获取窗体 首先打开spy+ ...
- [转]鼠标和键盘模拟API
几乎所有的游戏中都使用了鼠标来改变角色的位置和方向,玩家仅用一个小小的鼠标,就可以使角色畅游天下. 那么,我们如何实现在没有玩家的参与下角色也可以自动行走呢.其实实现这个并不难,仅仅几个Windows ...
- Winserver下的Hyper-v “未在远程桌面会话中捕获到鼠标”
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题
1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...
- 自定义鼠标光标cursor
通过css属性 Cursor:url()自定义鼠标光标. {cursor:url('图标路径'),default;} url是自定义鼠标图标路径 default指的是定义默认的光标(通常是一个箭头), ...
- JS鼠标事件大全 推荐收藏
一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4 Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDb ...
- CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果
CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...
随机推荐
- Linux ssh的使用
1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...
- IOS开发代码分享之获取启动画面图片的string
http://www.jb51.net/article/55309.htm 本代码支持 iPhone 6 以下. 支持 iPhone 及 iPad ? 1 2 3 4 5 6 7 8 9 10 11 ...
- Linux环境下通过ODBC访问MSSql Server
为了解决Linux系统连接MSSql Server的问题,微软为Linux系统提供了连接MSSql Server的ODBC官方驱动.通过官方驱动,Linux程序可以方便地对MSSql Server进行 ...
- td中嵌套table,让table完全填充父元素td
<table width="100% " cellspacing=0 cellpadding=0 border=1 > <tr> <td style= ...
- BZOJ 3098
: Hash Killer II 时间限制: Sec 内存限制: MBSec Special Judge 提交: 解决: [提交][][] 题目描述 这天天气不错,hzhwcmhf神犇给VFleaKi ...
- C# axWindowsMediaPlayer制作播放器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【转】docker之Dockerfile实践
转自:https://www.cnblogs.com/jsonhc/p/7767669.html 上一篇介绍了Dockerfile中使用的指令,现在开始进行指令实践 先查看下本地的镜像,选一个作为ba ...
- python数据转换工具Into
转:http://python.jobbole.com/81564/ 再次介绍Into包:整洁地数据迁移 2015/03/12 · 基础知识 · into, 数据迁移 分享到:3 本文由 伯乐在线 ...
- (20)python pycharm
使用GitHub 一·登录GitHub 1. 2. 3. 4. 二. 登录成功后再配置git 1 2.创建项目到github 3.下载github
- python3类方法,实例方法和静态方法
今天简单总结下python的类方法,实例方法,静态方法. python默认都是实例方法,也就是说,只能实例对象才能调用这个方法. 那是不是说类方法也只能被类对象本身来调用呢,当然,不是.类方法既可以被 ...