木子屋 Dnawo's BLOG

C#设置全局热键

👤 dnawo 📅 2009-08-28 👁 6179 👍 0 💬 2 🔄 来源:cnblogs.com
Hotkey.cs:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public delegate void HotkeyEventHandler(int hotkeyid);

    public class Hotkey : IMessageFilter
    {
        Hashtable keyIDs = new Hashtable();
        IntPtr hWnd;

        public event HotkeyEventHandler OnHotkey;

        public enum KeyFlags
        {
            MOD_ALT = 0x1,
            MOD_CONTROL = 0x2,
            MOD_SHIFT = 0x4,
            MOD_WIN = 0x8
        }

        [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);

        public Hotkey(IntPtr hWnd)
        {
            this.hWnd = hWnd;
            Application.AddMessageFilter(this);
        }

        public int RegisterHotkey(Keys key, KeyFlags keyflags)
        {
            UInt32 hotkeyid = GlobalAddAtom(Guid.NewGuid().ToString());
            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)
            {
                UnregisterHotKey(hWnd, key);
                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;
        }
    }
}

Form1.cs:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Hotkey _hotkey;
        private int _hotkeyid;

        public Form1()
        {
            InitializeComponent();
        }

        public void OnHotkey(int hotkeyid)
        {
            if (hotkeyid == _hotkeyid)
            {
                if (this.Visible == true)
                    this.Visible = false;
                else
                    this.Visible = true;
            }
            else
            {
                this.Visible = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _hotkey = new Hotkey(this.Handle);
            _hotkeyid = _hotkey.RegisterHotkey(Keys.F2, Hotkey.KeyFlags.MOD_CONTROL);   //定义快键(Ctrl + F2)
            _hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _hotkey.UnregisterHotkeys();
        }
    }
}

评论(2)

ztitan 2009-09-06 12:34
你好,请问你能发这个程序的源文件给我吗?我邮箱是199989277@qq.com,打扰你拉~~
dnawo 2009-09-07 18:16
代码都在上边了,测试过的,Copy就能用。
计算题
评论需审核通过后显示
← 上一篇 GIF文件格式 下一篇 → C#键盘按键监视