using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Threading; namespace AutoSendMessages { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region AIP //如果函数执行成功,返回值不为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 } //获取鼠标当前位置 [DllImport("user32.dll")] private static extern bool GetCursorPos(out Point p); //设置鼠标位置 [DllImport("User32")] public extern static void SetCursorPos(int x, int y); //模拟鼠标 [DllImport("User32")] public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo); public enum MouseEventFlags { Move = 0x0001, //移动鼠标 LeftDown = 0x0002,//模拟鼠标左键按下 LeftUp = 0x0004,//模拟鼠标左键抬起 RightDown = 0x0008,//鼠标右键按下 RightUp = 0x0010,//鼠标右键抬起 MiddleDown = 0x0020,//鼠标中键按下 MiddleUp = 0x0040,//中键抬起 Wheel = 0x0800, Absolute = 0x8000//标示是否采用绝对坐标 } #endregion private void Form1_Load(object sender, EventArgs e) { RegisterHotKey(Handle, 100, KeyModifiers.None, Keys.F7);//F7 锁定消息框位置 RegisterHotKey(Handle, 101, KeyModifiers.None, Keys.F8);//F8 开始 / 暂停 RegisterHotKey(Handle, 102, KeyModifiers.None, Keys.F9);//F9 隐藏 / 显示 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; if (MessageBox.Show("你是否要退出?","退出提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) { e.Cancel = false; } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { UnregisterHotKey(Handle, 100); UnregisterHotKey(Handle, 101); UnregisterHotKey(Handle, 102); } protected override void WndProc(ref Message m) { const int WM_HOTKEY = 0x0312; //按快捷键 switch (m.Msg) { case WM_HOTKEY: switch (m.WParam.ToInt32()) { case 100: // F7 锁定位置 FindPoint(); break; case 101: //F8 开始 / 暂停 MakeStart(); break; case 102: //F9 隐藏 / 显示 MakeShow(); break; } break; } base.WndProc(ref m); } Point CP = new Point(); //寻找位置 private void FindPoint() { GetCursorPos(out CP); label5.Text = "(" + CP.X + " , "+ CP.Y + ")"; } //开始/暂停 private void MakeStart() { int t_interval = 0; int.TryParse(textBox2.Text.ToString(), out t_interval); if (t_interval < 100) { t_interval = 100; } timer1.Interval = t_interval; timer1.Enabled = !timer1.Enabled; } //隐藏/显示 private void MakeShow() { this.Visible = !this.Visible; } int indexs = 0; private void timer1_Tick(object sender, EventArgs e) { indexs++; SendMessage(); } //发送消息 [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)] public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo); public const int WM_KEYDOWN = 256; public const int WM_KEYUP = 257; private void SendMessage() { if (textBox1.Text.ToString() != "") { MoveMouse(); DoubleMouse(); //Thread.Sleep(100); keybd_event(Keys.ControlKey, 0, 0, 0); keybd_event(Keys.A, 0, 0, 0); keybd_event(Keys.A, 0, 0x2, 0); keybd_event(Keys.ControlKey, 0, 0x2, 0); Thread.Sleep(10); SendKeys.SendWait(textBox1.Text.ToString()); SendKeys.SendWait("{ENTER}"); } } //移动鼠标 private void MoveMouse() { SetCursorPos(CP.X, CP.Y); } //双击鼠标 private void DoubleMouse() { mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); //mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); //mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero); } } }