首页 > 代码库 > 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键

黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键

1、新建一个类 HotkeyHelper 

using System;using System.Runtime.InteropServices;using System.Windows.Forms;using System.Collections;namespace 黄聪{    public delegate void HotkeyEventHandler(int hotKeyID);    public class HotkeyHelper : IMessageFilter    {        public event HotkeyEventHandler OnHotkey;        public enum KeyFlags        {            MOD_ALT = 0x1,            MOD_CONTROL = 0x2,            MOD_SHIFT = 0x4,            MOD_WIN = 0x8        }        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()        {            Application.RemoveMessageFilter(this);            foreach (UInt32 key in keyIDs.Values)            {                NativeMethods.UnregisterHotKey(hWnd, key);                NativeMethods.GlobalDeleteAtom(key);            }        }        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;        }    }}

 

2、在窗口创建(FrmMain_Load)的时候注册快捷键

private void FrmMain_Load(object sender, EventArgs e){    hotkeyHelper = new HotkeyHelper(this.Handle);    int favKey = hotkeyHelper.RegisterHotkey(Keys.F, HotkeyHelper.KeyFlags.MOD_CONTROL); //注册Ctrl+F    int nextKey = hotkeyHelper.RegisterHotkey(Keys.G, HotkeyHelper.KeyFlags.MOD_CONTROL);//注册Ctrl+G    hotkeyHelper.OnHotkey += new HotkeyEventHandler(OnHotkey);}

 

3、热键的响应方法

private void OnHotkey(int hotkeyID){    if (hotkeyID == favKey)    {        //do something    }    else if (hotkeyID == nextKey)    {        //do something else    }}

 

4、当然在窗体关闭的时候,一定要注销掉快捷键

private void FrmMain_FormClosing(object sender, FormClosingEventArgs e){    hotkeyHelper.UnregisterHotkeys();}

 

黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键