using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Collections; namespace Dll.HotKeys { public delegate void HotkeyEventHandler(int hotKeyID); public class HotkeyHelper : IMessageFilter { public event HotkeyEventHandler OnHotkey; public enum KeyFlags { Alt = 0x1, Ctrl = 0x2, Ctrl_Alt = 0x3, Alt_Shift = 0x5, Shift = 0x4, Ctrl_Shift = 0x6, Win = 0x8, Ctrl_Alt_Shift = 0x7 } class NativeMethods { private NativeMethods() { } #region WIN32 API [DllImport("user32.dll")] public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); [DllImport("user32.dll")] public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id); [DllImport("kernel32.dll")] public static extern UInt32 GlobalAddAtom(String lpString); [DllImport("kernel32.dll")] public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); #endregion } Hashtable keyIDs = new Hashtable(); IntPtr hWnd; public HotkeyHelper(IntPtr hWnd) { this.hWnd = hWnd; Application.AddMessageFilter(this); } public int RegisterHotkey(Keys Key, KeyFlags keyflags) { UInt32 hotkeyid = NativeMethods.GlobalAddAtom(System.Guid.NewGuid().ToString()); NativeMethods.RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key); keyIDs.Add(hotkeyid, hotkeyid); return (int)hotkeyid; } public void UnregisterHotkeys(object sender, EventArgs e) { Application.RemoveMessageFilter(this); foreach (UInt32 key in keyIDs.Values) { NativeMethods.UnregisterHotKey(hWnd, key); NativeMethods.GlobalDeleteAtom(key); } } public void UnregisterHotkeys() { UnregisterHotkeys(null, null); } public bool PreFilterMessage(ref Message m) { if (m.Msg == 0x312) { if (OnHotkey != null) { foreach (UInt32 key in keyIDs.Values) { if ((UInt32)m.WParam == key) { OnHotkey((int)m.WParam); return true; } } } } return false; } } } //注册热键 Dll.HotKeys.HotkeyHelper hotkeyHelper = new Dll.HotKeys.HotkeyHelper(this.Handle); HotKeyIds.FormClose = hotkeyHelper.RegisterHotkey(Keys.End, Dll.HotKeys.HotkeyHelper.KeyFlags.Ctrl_Alt); HotKeyIds.FormShow = hotkeyHelper.RegisterHotkey(Keys.Space, Dll.HotKeys.HotkeyHelper.KeyFlags.Ctrl_Alt); HotKeyIds.War3Kill = hotkeyHelper.RegisterHotkey(Keys.F12, Dll.HotKeys.HotkeyHelper.KeyFlags.Ctrl_Alt); hotkeyHelper.OnHotkey += new Dll.HotKeys.HotkeyEventHandler(OnHotkey); //注销热键 this.Disposed += new EventHandler(hotkeyHelper.UnregisterHotkeys); /// <summary> /// 定义热键对应ID /// </summary> private struct HotKeyIds { //显示窗体 public static int FormShow; //退出程序 public static int FormClose; //结束魔兽 public static int War3Kill; } /// <summary> /// 响应热键 /// </summary> /// <param name="hotkeyID">热键Id</param> private void OnHotkey(int hotkeyID) { if (hotkeyID == HotKeyIds.FormClose) { this.FormClose(); } if (hotkeyID == HotKeyIds.FormShow) { this.Ni_MouseDoubleClick(null, null); } if (hotkeyID == HotKeyIds.War3Kill) { Dll.Processes.Action.KillProcessByProcessName("war3"); if (Dll.Processes.Action.strErr != "") { MessageBox.Show(Dll.Processes.Action.strErr); } } }