c#大漠多线程,热键启动停止窗口
结合大漠插件提供的C#多线程模板使用
定义注册卸载热键的类
public class HotKey { [DllImport("kernel32.dll")] public static extern uint GetLastError(); //如果函数执行成功,返回值不为0。 //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。 [DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey( IntPtr hWnd, //要定义热键的窗口的句柄 int id, //定义热键ID(不能与其它ID重复) int fsModifiers, //辅助键,不设置=0,Alt=1、Ctrl=2、Shift=4、Windows=8 Keys vk //定义的热键 ); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey( IntPtr hWnd, //要取消热键的窗口的句柄 int id //要取消热键的ID ); }
根据窗口坐标获取句柄
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(Point point);
在WndProc通知中判断热键
定义消息相关的常量
// 异步通知(用于线程通知UI) public class AsyncNotify { [DllImport("user32.dll")] public static extern bool PostMessageW(IntPtr hwnd,int message,int wparam,int lparam); // 消息ID public static int WM_NOTIFY_ID = 0x400 + 0x555; // 消息Code public static int Update = 0; public static int Stop = 1; public static int Restart = 2; //定义消息相关的常量 public const int WM_HOTKEY = 0x312; //窗口消息-热键 public const int WM_CREATE = 0x1; //窗口消息-创建 public const int WM_DESTROY = 0x2; //窗口消息-销毁 public const int keyID = 0x3572; //热键ID public const int keyID2 = 0x3573; //热键ID // 主窗口句柄 public static IntPtr m_main_hwnd; public static void Notify(int hwnd,int code) { PostMessageW(m_main_hwnd, WM_NOTIFY_ID, hwnd, code); } }
protected override void WndProc(ref Message msg) { // 异步通知 if (msg.Msg == AsyncNotify.WM_NOTIFY_ID) { int hwnd = msg.WParam.ToInt32(); int notify_code = msg.LParam.ToInt32(); if (notify_code == AsyncNotify.Update) { UpdateUI(hwnd, SyncNotify.Update); } else if (notify_code == AsyncNotify.Stop) { m_multi_thread.ThreadStop(hwnd); } else if (notify_code == AsyncNotify.Restart) { m_multi_thread.ThreadRestart(hwnd); } } // 同步通知 else if (msg.Msg == SyncNotify.WM_NOTIFY_ID) { int hwnd = msg.WParam.ToInt32(); int notify_code = msg.LParam.ToInt32(); UpdateUI(hwnd, notify_code); } else if (msg.Msg == SyncNotify.WM_HOTKEY) { int WParamcode = msg.WParam.ToInt32(); if (WParamcode == AsyncNotify.keyID) { int x = Cursor.Position.X; int y = Cursor.Position.Y; Point p = new Point(x, y); IntPtr formHandle = WindowFromPoint(p); runApp(formHandle.ToString()); } else if (WParamcode == AsyncNotify.keyID2) { int x2 = Cursor.Position.X; int y2 = Cursor.Position.Y; Point p2 = new Point(x2, y2); IntPtr formHandle2 = WindowFromPoint(p2); endApp(formHandle2.ToString()); } } else if (msg.Msg == SyncNotify.WM_CREATE) { HotKey.RegisterHotKey(Handle, SyncNotify.keyID, 0, Keys.Home);//设置Ctrl+alt+m热键 HotKey.RegisterHotKey(Handle, SyncNotify.keyID2, 0, Keys.End);//设置Ctrl+alt+m热键 } base.WndProc(ref msg); }
开始启动
private void runApp(string formHandle) { var LdCmdFree = GetLdCmdFree();//取空闲模拟器序号 ldCmd.Launch(LdCmdFree);//启动 //循环取句柄后启动 var LdHwnd = 0; while (true) { LdHwnd = GetLdCmdHwnd(LdCmdFree); if (LdHwnd > 0) { break; } m_dm.Delays(1000, 1500); } formHandle = LdHwnd.ToString(); string hwnds = formHandle; if (hwnds.Length == 0) { Log.WriteStr("枚举窗口失败"); return; } string[] hwnd_array = hwnds.Split(new char[] { ',' }); //Global.result.Clear(); //for (int i = 0; i < listBox2.Items.Count; i++) //{ // listboxArr listboxArr = new listboxArr(); // listboxArr.itemsName = listBox2.Items[i].ToString(); // Global.result.Add(listboxArr); //} foreach (string i in hwnd_array) { int hwnd = Convert.ToInt32(i); if (m_multi_thread.ThreadStart(hwnd) == false) { m_dm.TerminateProcess(m_dm.GetWindowProcessId(hwnd)); } else { Global.ThreadNumbers += 1; } } }
停止运行
private void endApp(string formHandle) { m_multi_thread.ThreadStop(Convert.ToInt32(formHandle)); }
取雷电空闲序号
/// <summary> /// 取雷电空闲序号 /// </summary> /// <returns></returns> public int GetLdCmdFree() { //雷电返回值 var ListSimulator = ldCmd.ListSimulator(); // 0,雷电模拟器,0,0,0,-1,-1 if (ListSimulator.Count > 0) { for (int i = 0; i < ListSimulator.Count; i++) { var simulatorArr = ListSimulator[i].Split(','); if (Convert.ToInt32(simulatorArr[2]) == 0) { return Convert.ToInt32(simulatorArr[0]); } } } return 0; }
根据序号取雷电句柄
/// <summary> /// 根据序号取雷电句柄 /// </summary> /// <returns></returns> public int GetLdCmdHwnd(int Free) { //雷电返回值 var ListSimulator = ldCmd.ListSimulator(); // 0,雷电模拟器,0,0,0,-1,-1 if (ListSimulator.Count > 0) { for (int i = 0; i < ListSimulator.Count; i++) { var simulatorArr = ListSimulator[Free].Split(','); if (Convert.ToInt32(simulatorArr[2]) > 0) { return Convert.ToInt32(simulatorArr[2]); } } } return 0; }
全局变量
/// <summary> /// 全局变量 /// </summary> public static class Global { /// <summary> /// listbox /// </summary> public static List<listboxArr> result = new List<listboxArr>(); /// <summary> /// 全局线程数量 /// </summary> public static int ThreadNumbers = 0; } public class listboxArr { public string itemsName { get; set; } }