一、说明

经常会被问到需要点击软件的,主要都是玩游戏的盆友,但是也有其它用途的。所以简单弄了一个,打算每当有时间,有需求,就加一些小功能。

这里主要是要记录一下相关开发工作,也记录一些使用/更新的信息。

二、更新记录

1、版本说明

【2022/08/22】版本v1.0(初始版本,主要包含间隔点击模式、识别图片点击模式)

2、下载地址

基于winform/c#/opencv实现的windows下使用的自动鼠标点击小软件-桌面系统文档类资源-CSDN下载【2022/08/22】版本v1.0【功能描述】目前实现两种点击模式。间隔模式:一句话描述,更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/bashendixie5/86406516

三、功能描述

1、点击模式

目前实现两种点击模式(仅支持鼠标左键单击)。

(1)间隔模式

一句话描述,按设置好的间隔时间鼠标左键单击用户设定的位置。

(2)图片模式

一句话描述,用户截图想要识别的图片,上传到软件,然后软件按设置好的间隔时间识别屏幕是否出现了用户上传的图片,如果识别到了,则鼠标左键单击用户设定的位置。

后面会增加一些点击模式,比如文字识别,颜色识别等。

2、基础设置

主要设置

四、基础源码简述

1、枚举/实体

    internal enum ClickModes{/// <summary>/// 间隔模式/// </summary>ClickByStep,/// <summary>/// 根据图片点击/// </summary>ClickByPictures,/// <summary>/// 根据文字点击/// </summary>ClickByText,/// <summary>/// 根据颜色点击/// </summary>ClickByColor,/// <summary>/// 自定义的点击/// </summary>ClickByCustomize}
public enum SettingModes{NotSettingState,/// <summary>/// 间隔点击模式的点击位置设置/// </summary>ClickByStep_PositionOne,/// <summary>/// 根据图片的点击位置设置/// </summary>ClickByPictures_PositionOne,/// <summary>/// 根据文字的点击位置设置/// </summary>ClickByText_PositionOne,/// <summary>/// 根据颜色的点击位置设置/// </summary>ClickByColor_PositionOne,/// <summary>/// 根据自定义的点击设置/// </summary>ClickByCustomize_PositionOne,ClickByCustomize_PositionTwo,ClickByCustomize_PositionThree,ClickByCustomize_PositionFour,ClickByCustomize_PositionFive,ClickByCustomize_PositionSix,}

2、windows api

internal class Win32Api{[StructLayout(LayoutKind.Sequential)]public class POINT{public int x;public int y;}[StructLayout(LayoutKind.Sequential)]public class MouseHookStruct{public POINT pt;public int hwnd;public int wHitTestCode;public int dwExtraInfo;}public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//安装钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);//卸载钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern bool UnhookWindowsHookEx(int idHook);//调用下一个钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);[DllImport("User32")]public extern static void SetCursorPos(int x, int y);//定义快捷键[DllImport("user32.dll", SetLastError = true)]public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, int vk);[Flags()]public enum KeyModifiers{None = 0,Alt = 1,Ctrl = 2,Shift = 4,WindowsKey = 8}/// <summary>/// 取消注册热键/// </summary>/// <param name="hWnd">要取消热键的窗口的句柄</param>/// <param name="id">要取消热键的ID</param>/// <returns></returns>[DllImport("user32.dll", SetLastError = true)]public static extern bool UnregisterHotKey(IntPtr hWnd, int id);/// <summary>/// 向全局原子表添加一个字符串,并返回这个字符串的唯一标识符,成功则返回值为新创建的原子ID,失败返回0/// </summary>/// <param name="lpString"></param>/// <returns></returns>[DllImport("kernel32", SetLastError = true)]public static extern short GlobalAddAtom(string lpString);/// <summary>/// 热键的对应的消息ID/// </summary>public const int WM_HOTKEY = 0x312;[DllImport("kernel32", SetLastError = true)]public static extern short GlobalDeleteAtom(short nAtom);[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int mouse_event(int dwflags, int dx, int dy, int cbuttons, int dwextrainfo);public const int mouseeventf_move = 0x0001;      //移动鼠标public const int mouseeventf_leftdown = 0x0002; //模拟鼠标左键按下public const int mouseeventf_leftup = 0x0004; //模拟鼠标左键抬起public const int mouseeventf_rightdown = 0x0008; //模拟鼠标右键按下public const int mouseeventf_rightup = 0x0010; //模拟鼠标右键抬起public const int mouseeventf_middledown = 0x0020; //模拟鼠标中键按下public const int mouseeventf_middleup = 0x0040; //模拟鼠标中键抬起public const int mouseeventf_absolute = 0x8000; //标示是否采用绝对坐标}

3、定时器

timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Interval = settingModel.intervals;    //执行间隔时间,单位为毫秒
timer.Elapsed += new System.Timers.ElapsedEventHandler(myTimedTask);

4、快捷键

 int f1 = Win32Api.GlobalAddAtom("F1");int f5 = Win32Api.GlobalAddAtom("F5");int f6 = Win32Api.GlobalAddAtom("F6");
Win32Api.RegisterHotKey(this.Handle, f1, Win32Api.KeyModifiers.None, (int)Keys.F1);
Win32Api.RegisterHotKey(this.Handle, f5, Win32Api.KeyModifiers.None, (int)Keys.F5);
Win32Api.RegisterHotKey(this.Handle, f6, Win32Api.KeyModifiers.None, (int)Keys.F6);
/// <summary>/// 监视Windows消息/// </summary>/// <param name="m"></param>protected override void WndProc(ref Message m){switch (m.Msg){case Win32Api.WM_HOTKEY:ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数break;}base.WndProc(ref m); //将系统消息传递自父类的WndProc}/// <summary>/// 按下设定的键时调用该函数/// </summary>/// <param name="m"></param>private void ProcessHotkey(Message m){IntPtr id = m.WParam;//IntPtr用于表示指针或句柄的平台特定类型int sid = id.ToInt32();if (sid == f1){//do something}else if (sid == f5){//do something}else if (sid == f6){//do something}}

5、鼠标单击

        /// <summary>/// 单击/// </summary>/// <param name="x"></param>/// <param name="y"></param>public static void SingleClick(int x, int y){//鼠标移动Win32Api.SetCursorPos(x, y);//鼠标点击Win32Api.mouse_event(Win32Api.mouseeventf_leftdown, x, y, 0, 0);Win32Api.mouse_event(Win32Api.mouseeventf_leftup, x, y, 0, 0);}

五、不同模式代码简述

1、开始监控

监控开始的时候,首先检查是否正确设置了点击位置,然后开始定时器。

        switch (clickModes){//间隔模式case ClickModes.ClickByStep:int x1, y1;if (!this.control0.ClickPositionFlag(out x1, out y1)){MessageBox.Show("请检查鼠标点击位置设置是否正确。");return;}break;//图片模式case ClickModes.ClickByPictures:if(this.control1.mat == null){MessageBox.Show("请选择待匹配图片。");return;}break;}this.BackColor = Color.AliceBlue;this.startFlag = true;this.timer.Start();

2、定时任务的代码

    public void myTimedTask(object sender, System.Timers.ElapsedEventArgs e){if (startFlag){switch (this.clickModes){//间隔模式case ClickModes.ClickByStep:int x0, y0;if (this.control0.ClickPositionFlag(out x0, out y0)){HelpTools.SingleClick(x0, y0);}break;//图片模式case ClickModes.ClickByPictures:int x2, y2;//检查是否再屏幕内匹配到用户上传的图片if(this.GetShouldClickFlagByPictures(out x2, out y2)){int x1, y1;//如果用户设置了点击位置,则点击设置的位置if(this.control1.ClickPositionFlag(out x1, out y1)){HelpTools.SingleClick(x1, y1);}//如果用户没有设置点击位置,则点击图片中心else{HelpTools.SingleClick(x2, y2);}}break;case ClickModes.ClickByText:break;case ClickModes.ClickByColor:break;case ClickModes.ClickByCustomize:break;default:break;}// 用于统计点击的次数runtimes++;// 如果是点击固定次数,则点击次数达到了的时候,停止运行if(settingModel!=null && settingModel.fixedFlag && runtimes>= settingModel.clickNums){this.Invoke(new Action(() =>this.button5_Click(null, null)));}    }}

3、图片识别

首先全屏截图,然后从全屏截图中使用用户上传的图片进行模板匹配,找到匹配的区域之后再将该区域抠出来和用户上传的图片进行一次结构相似性的比较(故此说这里未必次次都能正确找到,但是一般辨识度高的图片问题不太大)。

        /// <summary>/// 判断图片模式是否需要点击/// </summary>/// <returns></returns>public bool GetShouldClickFlagByPictures(out int x1, out int y1){//全屏截图int width = Screen.PrimaryScreen.Bounds.Width;int height = Screen.PrimaryScreen.Bounds.Height;Rectangle smallRect = new Rectangle(0, 0, width, height);Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);Bitmap tSrcBmp = new Bitmap(width, height); // 用于屏幕原始图片保存Graphics gp = Graphics.FromImage(tSrcBmp);gp.CopyFromScreen(0, 0, 0, 0, smallRect.Size);gp.DrawImage(tSrcBmp, 0, 0, smallRect, GraphicsUnit.Pixel);//全屏截图转matMat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(tSrcBmp);Cv2.CvtColor(mat, mat, ColorConversionCodes.BGRA2BGR);//从全屏截图查找待匹配图像Mat out1 = new Mat();Cv2.MatchTemplate(mat, this.control1.mat, out1, TemplateMatchModes.CCoeffNormed);//寻找匹配到的最大最小值double minVal, maxVal;OpenCvSharp.Point minLoc, maxLoc;Cv2.MinMaxLoc(out1, out minVal, out maxVal, out minLoc, out maxLoc, null);//Cv2.Rectangle(mat, maxLoc, new OpenCvSharp.Point(maxLoc.X + this.control1.mat.Width, maxLoc.Y + this.control1.mat.Height), 255, 2);//获取匹配到的部分图像Mat child = new Mat(mat, new OpenCvSharp.Rect(maxLoc.X, maxLoc.Y, this.control1.mat.Width, this.control1.mat.Height));x1 = maxLoc.X + this.control1.mat.Width / 2;y1 = maxLoc.Y + this.control1.mat.Height / 2;//比较匹配到的和原图的相似度double ssim = ImagesTools.Compare_SSIM(child.Clone(), this.control1.mat.Clone());Console.WriteLine("相似性:" + ssim);if (ssim > 0.9){return true;}System.GC.Collect();return false;}

WinFrom、C# 学习记录五 开发一个鼠标自动点击小软件相关推荐

  1. asp.net c#开发的音悦台自动打卡小软件

    有兴趣的朋友可以下来研究一下 源码下载: AutoSignIn.zip

  2. MySQL学习记录 (五) ----- 存储函数、存储过程和触发器

    相关文章: <MySQL学习记录 (一) ----- 有关数据库的基本概念和MySQL常用命令> <MySQL学习记录 (二) ----- SQL数据查询语句(DQL)> &l ...

  3. Java学习记录五(多线程、网络编程、Lambda表达式和接口组成更新)

    Java学习记录五(多线程.网络编程.Lambda表达式和接口组成更新) Java 25.多线程 25.1实现多线程 25.1.1进程 25.1.2线程 25.1.3多线程的实现 25.1.4设置和获 ...

  4. [Python学习]PycURL简单学习 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …...

    [Python学习]PycURL简单学习 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source - [Python学习] ...

  5. python实现简单小游戏_用python开发一个有趣的猜数字小游戏(实现简单的GUI界面学习)...

    文章目录 用python开发一个有趣的猜数字小游戏 用于字符串的格式化,通过 {} 和 : 来代替以前的 % 1. python的GUI编程 1.1 pythonGUI常用库对比 Python 提供了 ...

  6. 开发一个简单的小学生数学测试软件

    前言 本文适用于未学指针的朋友 本人就是没学到指针的 题目 开发一个简单的小学生数学测试软件,能实现以下功能: (1)保存五个学生的学号及姓名,以及考试软件的登录密码. (2)学生根据登录密码登录到考 ...

  7. VBNet WinForm如何开发一个照片自动排版程序

    想要开发一个照片自动排版程序要熟悉一下几个知识点: System.Drawing.Printing.PrintDocument.Print涉及到的纸张设置是英寸相关,我们常用的长度单位是毫米,那么先需 ...

  8. python自动点击脚本_用Python实现鼠标自动点击

    前言: 最近玩某页游时遇到一个重复任务,需要不停的接/交任务道具,数量巨大又十分麻烦,想去网上下脚本又找不到合适的,于是整了一个可以实现鼠标自动点击的小玩意.因为我之前没有任何编程基础,这两天自学又走 ...

  9. python 鼠标 自动点击_鼠标隔段时间自动点击屏幕

    知识点:Python库及简单定时器的使用 1. 鼠标自动点击屏幕代码 (1). 首先 pip install pymouse (2). 运行代码出现:ModuleNotFoundError: No m ...

最新文章

  1. Python入门100题 | 第004题
  2. C语言-第8课 - 注释符号
  3. 华为p10plus能用鸿蒙吗,华为P10/P10 Plus对比评测:自家兄弟大对决
  4. 修复IE下相对定位子元素溢出Bug
  5. MySQL(16)--- DELETE 语句
  6. js怎么获取ueditor值_UEditor获取内容
  7. WindowsServer2003虚拟机 安装SQL2005失败 终于找到原因了
  8. SVM——支持向量回归(SVR)
  9. 转换小写金额为大写金额
  10. java3d室外场景构建_3dmax里怎么渲染室外场景
  11. 明争暗斗 京东阿里智能家居大战一触即发
  12. 基于STM32cubemx的stm32f107vct6代码生成教程,实验一led闪烁
  13. 基于单片机节水定时智能控制器设计-毕设课设资料
  14. Altium Designer--如何添加泪滴
  15. solidworks的openGL选项是灰色的处理方法
  16. Android5.1 -Recents分析
  17. Fortran编程:(三)数据类型
  18. 如何删除win7系统桌面ie图标
  19. Windows操作系统类毕业论文文献有哪些?
  20. 谷氨酰胺合成酶(Glutamine synthetase,GS)试剂盒说明书

热门文章

  1. 怎么把照片变成铅笔画草图效果
  2. smalldatetime mysql_「smalldatetime」datetime与smalldatetime之间的区别小结 - seo实验室
  3. 微博实名制的背后 利益推动还是社交需求?
  4. Win2003下移动硬盘无法识别
  5. windows 使用dos命令,按照映像名称查询进程
  6. 【iMessage苹果推】准入苹果相册推苹果家庭推Apple Texter 无线平台
  7. 动态功能脑网络状态中的EEG特征
  8. 远航 Hello world.
  9. 计算机d盘被拒绝访问了怎么办,Win10系统D盘打不开提示拒绝访问如何解决
  10. 第十九节 HTTP 协议