黄聪

论SEO对人类的重要性,请看我的博客:hcsem.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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();
}

 

posted on 2014-10-24 08:35  黄聪  阅读(1493)  评论(0编辑  收藏  举报