目录
- 前言
- 一、工具的基本原理
- 二、实现代码
- 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、1
if (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)); //点击
}
}
#endregion
bool 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;
#endregion
string 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;
}
}
}
}
总结
其实这个还是挺简单的,难点就在于全局快捷键那一块,这边源码贴的不是很全,要是想要源码或者是有哪里不懂的 欢迎来私信我~ 可以一起讨论一下其他的问题