C#系統熱鍵類

來源:互聯網
上載者:User
C#系統熱鍵類using System;
using System.Runtime.InteropServices;

namespace SystemHotKey
{
    public delegate void HotkeyEventHandler(int HotKeyID);

    public class Hotkey : System.Windows.Forms.IMessageFilter
    {
        System.Collections.Hashtable keyIDs = new System.Collections.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;
            System.Windows.Forms.Application.AddMessageFilter(this);
        }

        public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
        {
            UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
            RegisterHotKey( (IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
            keyIDs.Add(hotkeyid, hotkeyid);
            return (int)hotkeyid;
        }

        public void UnregisterHotkeys()
        {
            System.Windows.Forms.Application.RemoveMessageFilter(this);
            foreach (UInt32 key in keyIDs.Values)
            {
                UnregisterHotKey(hWnd, key); 
                GlobalDeleteAtom(key);
            }
        }

        public bool PreFilterMessage(ref System.Windows.Forms.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;
        }
    }
}

該類的使用方法:

在表單的類中聲明一個變數private int Hotkey1

在表單的Load事件中加入如下代碼

Hotkey hotkey;
hotkey = new Hotkey(this.Handle);
Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F1, Hotkey.KeyFlags.MOD_CONTROL);//定義快鍵(Ctrl + F1)
hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);

添加快鍵調用函數

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.