目录

  • 前言
  • 一、工具的基本原理
  • 二、实现代码
    • 1.MainForm页面布局
    • 2.MainForm后台代码
    • 3.KeyBord类
    • 4.ConfigTools类
  • 总结

前言

之前有需要用到鼠标连点器的场景,百度上的软件都不太喜欢,要么是太繁琐,要么就是要收费的,于是产生了自己写一个的想法,写完发现蛮好用的,于是过来分享一下


一、工具的基本原理

利用Winform里Timer工具,进行设置Invteral属性来达到每秒连点。

二、实现代码

1.MainForm页面布局

2.MainForm后台代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using drmaple;namespace MouseLinkerDemo
{public partial class MainForm : Skin_Color{public MainForm(){InitializeComponent();}#region 获取鼠标操作/// <summary>/// 鼠标操作标志位集合/// </summary>[Flags]enum MouseEventFlag : uint{/// <summary>/// 鼠标移动事件/// </summary>Move = 0x0001,/// <summary>/// 鼠标左键按下事件/// </summary>LeftDown = 0x0002,LeftUp = 0x0004,RightDown = 0x0008,RightUp = 0x0010,MiddleDown = 0x0020,MiddleUp = 0x0040,XDown = 0x0080,XUp = 0x0100,Wheel = 0x0800,VirtualDesk = 0x4000,/// <summary>/// 设置鼠标坐标为绝对位置(dx,dy),否则为距离最后一次事件触发的相对位置/// </summary>Absolute = 0x8000}/// <summary>/// 鼠标事件/// </summary>/// <param name="flags">事件类型</param>/// <param name="dx">x坐标值(0~65535)</param>/// <param name="dy">y坐标值(0~65535)</param>/// <param name="data">滚动值(120一个单位)</param>/// <param name="extraInfo">不支持</param>[DllImport("user32.dll")]static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);/// <summary>/// 触发鼠标事件/// </summary>/// <param name="x"></param>/// <param name="y"></param>private static void DoMouseClick(int x, int y, string Mouse){int dx = (int)((double)x / Screen.PrimaryScreen.Bounds.Width * 65535); //屏幕分辨率映射到0~65535(0xffff,即16位)之间int dy = (int)((double)y / Screen.PrimaryScreen.Bounds.Height * 0xffff); //转换为double类型运算,否则值为0、1if (Mouse == "左键"){mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击}else if (Mouse == "右键"){mouse_event(MouseEventFlag.Move | MouseEventFlag.RightDown | MouseEventFlag.RightUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击}else if (Mouse == "中键"){mouse_event(MouseEventFlag.Move | MouseEventFlag.MiddleDown | MouseEventFlag.MiddleUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击}else{mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击mouse_event(MouseEventFlag.Move | MouseEventFlag.RightDown | MouseEventFlag.RightUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击}}#endregionbool ISstart = false;Dictionary<string, int> valuePairs = new Dictionary<string, int>();//获取按键的key和值string keyboards = "F5";//快捷键设置private void Form1_Load(object sender, EventArgs e){#region 键盘快捷键绑定List<string> vs_Keys = new List<string>();int num = 112;for (int i = 0; i < 124 - 112; i++){num = 112 + i;string emnuName = Enum.GetName(typeof(Keys), num);vs_Keys.Add(emnuName);valuePairs.Add(emnuName, num);}this.comboBox3.DataSource = vs_Keys;#endregion#region 连点按键绑定List<string> Mouses = new List<string>() { "左键", "右键", "中键", "左右互点" };this.comboBox1.DataSource = Mouses;#endregionstring keyboard;string Mouse;string Speed;string start;string close;if (ConfigurationManager.AppSettings["KeyBord"].ToString() != "" && ConfigurationManager.AppSettings["Mouse"].ToString() != "" && ConfigurationManager.AppSettings["Speed"].ToString() != ""){keyboard = ConfigurationManager.AppSettings["KeyBord"].ToString();Mouse = ConfigurationManager.AppSettings["Mouse"].ToString();Speed = ConfigurationManager.AppSettings["Speed"].ToString();start = ConfigurationManager.AppSettings["cb_Start"].ToString();close = ConfigurationManager.AppSettings["cb_Close"].ToString();if (vs_Keys.Contains(keyboard)){int vs_i = vs_Keys.IndexOf(keyboard);comboBox3.SelectedIndex = vs_i;}if (Mouses.Contains(Mouse)){int vs_i = Mouses.IndexOf(Mouse);comboBox1.SelectedIndex = vs_i;}textBox1.Text = Speed + "";if (start == "True"){cb_Start.Checked = true;}else if (close == "True"){cb_Close.Checked = true;}}else{comboBox1.SelectedIndex = 0;int i = vs_Keys.IndexOf(keyboards);comboBox3.SelectedIndex = i;textBox1.Text = 1000 + "";cb_Start.Checked = true;}}private void Form1_Activated(object sender, EventArgs e){}private void Form1_Leave(object sender, EventArgs e){//注销Id号为100的热键设定HotKey.UnregisterHotKey(Handle, 100);}protected override void WndProc(ref Message m){const int WM_HOTKEY = 0x0312;//按快捷键 switch (m.Msg){case WM_HOTKEY:switch (m.WParam.ToInt32()){case 100:if (ISstart == true){ISstart = false;timer1.Stop();Lbl_isstart.Text = "未启动";Lbl_isstart.ForeColor = Color.Red;label7.Visible = false;count = 0;label7.Text = "0秒";timer2.Dispose();timer2.Stop();}else if (ISstart == false){ISstart = true;Lbl_isstart.Text = "已启动";Lbl_isstart.ForeColor = Color.Green;label7.Visible = true;timer1.Enabled = true;timer1.Start();timer1.Interval = Convert.ToInt32(textBox1.Text);timer2.Enabled = true;timer2.Start();}if (ISstart == false){if (cb_Start.Checked == true){SoundPlayer player = new SoundPlayer();player.SoundLocation = Application.StartupPath + "\\" + "Sound" + "\\" + "9676.wav";//关闭音效player.Load();player.Play();}}if (ISstart){if (cb_Start.Checked == true){SoundPlayer player = new SoundPlayer();player.SoundLocation = Application.StartupPath + "\\" + "Sound" + "\\" + "9675.wav";//开启音效player.Load();player.Play();}}break;}break;}base.WndProc(ref m);}private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){}private void timer1_Tick(object sender, EventArgs e){Point p1 = MousePosition;DoMouseClick(p1.X + 1, p1.Y + 1, comboBox1.Text);}private void comboBox3_SelectedIndexChanged(object sender, EventArgs e){if (comboBox3.SelectedValue != null){HotKey.UnregisterHotKey(Handle, 100);string Isselect = comboBox3.SelectedValue.ToString();HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, (Keys)Enum.Parse(typeof(Keys), Isselect));}}private void Form1_FormClosed(object sender, FormClosedEventArgs e){ConfigSetting();}private void ConfigSetting(){string MouseAJ = comboBox1.Text;//鼠标按键string KeyBord = comboBox3.Text;//全局按键string Speed = textBox1.Text;//连点速度string start = cb_Start.Checked.ToString();string close = cb_Close.Checked.ToString();//保存用户设置bool flag1 = ConfigTools.SetAppConfigValue("Mouse", MouseAJ);bool flag2 = ConfigTools.SetAppConfigValue("KeyBord", KeyBord);bool flag3 = ConfigTools.SetAppConfigValue("Speed", Speed);bool flag4 = ConfigTools.SetAppConfigValue("cb_Start", start);bool flag5 = ConfigTools.SetAppConfigValue("cb_Close", close);}int count = 0;private void timer2_Tick(object sender, EventArgs e){count++;label7.Text = count + "秒";}}
}

3.KeyBord类

该处是对于键盘的一些操作,用于实现全局快捷键功能

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace drmaple
{/// <summary>/// 全局键盘函数/// </summary>class HotKey{//如果函数执行成功,返回值不为0。//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。[DllImport("user32.dll", SetLastError = true)]public static extern bool RegisterHotKey(IntPtr hWnd,                //要定义热键的窗口的句柄int id,                     //定义热键ID(不能与其它ID重复)KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效Keys vk                     //定义热键的内容);[DllImport("user32.dll", SetLastError = true)]public static extern bool UnregisterHotKey(IntPtr hWnd,                //要取消热键的窗口的句柄int id                      //要取消热键的ID);//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)[Flags()]public enum KeyModifiers{None = 0,Alt = 1,Ctrl = 2,Shift = 4,WindowsKey = 8}}
}

4.ConfigTools类

该类是保存用户配置到Appconfig 方便下次读取用户配置。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;namespace MouseLinkerDemo
{public  class ConfigTools{/// 设置配置文件值/// </summary>/// <param name="key"></param>/// <param name="value"></param>/// <returns></returns>public static bool SetConConfigValue(string key, string value){XmlDocument xDoc = new XmlDocument();//获取可执行文件的路径和名称 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");XmlNode xNode;XmlElement xElem1;XmlElement xElem2;xNode = xDoc.SelectSingleNode("//connectionStrings");xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@name='" + key + "']");if (xElem1 != null) xElem1.SetAttribute("connectionString", value);else{xElem2 = xDoc.CreateElement("add");xElem2.SetAttribute("name", key);xElem2.SetAttribute("connectionString", value);xNode.AppendChild(xElem2);}try{xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");return true;}catch{return false;}}/// 设置配置文件值/// </summary>/// <param name="key"></param>/// <param name="value"></param>/// <returns></returns>public static bool SetAppConfigValue(string key, string value){XmlDocument xDoc = new XmlDocument();//获取可执行文件的路径和名称 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");XmlNode xNode;XmlElement xElem1;XmlElement xElem2;xNode = xDoc.SelectSingleNode("//appSettings");xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");if (xElem1 != null) xElem1.SetAttribute("value", value);else{xElem2 = xDoc.CreateElement("add");xElem2.SetAttribute("key", key);xElem2.SetAttribute("value", value);xNode.AppendChild(xElem2);}try{xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");return true;}catch{return false;}}}
}

总结

其实这个还是挺简单的,难点就在于全局快捷键那一块,这边源码贴的不是很全,要是想要源码或者是有哪里不懂的 欢迎来私信我~ 可以一起讨论一下其他的问题

C# WinForm开发鼠标连点器相关推荐

  1. C# WinForm开发系列 - Open-Source Controls

    整理了一些在WinForm开发中可以使用的开源组件.(文章及相关代码搜集自网络,仅供学习参考,版权属于原作者! ). 1. 仿office 2007的Ribbon风格面板(该控件模仿office 20 ...

  2. 跟我做WinForm开发(2)-后台逻辑操作

    上一篇中,我简单了介绍了实现自定义UI的步骤和其中一些需要注意的点:详见:跟我做WinForm开发(1)-自定义UI,下面,我就继续完成上篇没完成的逻辑操作: 获取声音 这是一个发音器,声音的来源是G ...

  3. 使用 .NET WinForm 开发所见即所得的 IDE 开发环境,实现不写代码直接生成应用程序

    SailingEase WinForm Framework WinForm开发框架开发手册:http://docs.shengxunwei.com/Home/Browser/sewinformfw/ ...

  4. WinForm开发中针对TreeView控件改变当前选择节点的字体与颜色

    WinForm开发中针对TreeView控件改变当前选择节点的字体与颜色  在B/S开发中,对TreeView控件要改变当前选中节点的颜色比较方便,其有相应的SelectedNodeChanged事件 ...

  5. c# Winform 开发分屏显示应用程序

    c# Winform 开发分屏显示应用程序 原文 http://blog.csdn.net/soft2buy/article/details/7082668 分屏显示即可把一台主机内运行的多个程序分别 ...

  6. .Net Winform 开发笔记(四) 透过现象看本质

    写在前面: 从一个窗体的创建显示,再到与用户的交互,最后窗体关闭,这中间经历过了一系列复杂的过程,本文将从Winform应用程序中的Program.cs文件的第一行代码开始,逐步分析一个Winform ...

  7. C语言和win32绿色鼠标连点器,带代码(Windows)

    前言: 项目名称:绿色鼠标连点器. 系统平台:Windows. 因为使用Win32api. 使用语言:C. 编译器/IDE:gcc,Dev cpp,Visual Studio. 项目前言: 本项目也是 ...

  8. 鼠标点击器20+版本使用教程系列(7)鼠标轨迹记录点击器1.1使用教程

    鼠标点击器是我们经常使用的一款windows软件(其实手机端也很有需求),因此网上有很多功能不一点击软件,但是很多不能满足需求,今天带领大家了解我们开发系列的鼠标点击软件,多达20多个版本.今天为大家 ...

  9. 鼠标点击器20+版本使用教程系列(2)多位置循环点击器

    鼠标点击器使用我们经常使用的一款软件,因此网上有很多功能不一点击软件,但是很多不能满足需求,今天带领大家了解我们开发系列的鼠标点击软件,多达20多个版本.今天为大家带来第2款软件-鼠标多位置循环点击器 ...

  10. VS2017 winform开发知识点汇总

    VS2017 winform开发知识点汇总 1 快捷键 2 控键缩写 2.1 标准控件 2.2 容器控件 2.3 菜单和工具栏 2.4 数据 2.5 对话框 2.6 组件 2.7 印刷 2.8 水晶报 ...

最新文章

  1. 完整的python项目流程
  2. flex布局一行三个_CSS Flex布局
  3. 【中间件】消息队列-RabbitMQ
  4. ActiveDirectory之不常用操作
  5. jar文件与云服务器断开,把jar包放到云服务器
  6. 中文分词工具jieba中的词性类型(转载)
  7. 十分钟了解Kubernetes
  8. Origin绘制双Y或双X折线图(不同坐标)
  9. Tiktok培训可以去学习吗?
  10. php之将用户信息写入数据库
  11. 中国大陆IP过滤器-Java实现
  12. hp 服务器 阵列卡信息导入,HP Proliant系列服务器 配置阵列卡过程.doc
  13. 蓝牙耳机按键事件linux,【记录】Android监听蓝牙耳机的按键事件
  14. switchport nonegotiate
  15. Linux中的ko怎么玩?
  16. echarts x 起始_echarts中如何在dataZoom 最左侧和最右侧始终显示起始和结束的值
  17. javaWeb开发的放推特项目之推特发布
  18. css clip-path和drop-shadow生成自定义图形、阴影
  19. 玩转X-CTR100 l STM32F4 l ADC 模拟数字转换
  20. [NOIP 模拟]疫情延迟 二分+Spfa

热门文章

  1. win10自带录屏工具
  2. html入门圣思园视频,Java Web学习历程-(基于圣思园视频教程)
  3. 黑金花大理石_不同产地的黑金花大理石有哪些特点?
  4. 揭秘百度新治理结构:马东敏的谣言与李彦宏的用人观
  5. 5S现场管理之车间现场标准化管理的六大执行要点
  6. 共同富裕指数集:31省份共同富裕-富裕度、共同度两大维度数据(2000-2021年)
  7. OTSU算法实现二值化
  8. 阿里云盘秒传使用方法
  9. 阿里云oss文件分片、断点续传上传
  10. Visual Studio 2015 中文社区版下载