C#中調用API函數RegisterHotKey註冊多個系統熱鍵

來源:互聯網
上載者:User

要設定快速鍵必須使用user32.dll下面的兩個方法。
BOOL RegisterHotKey( //註冊系統熱鍵的API函數
 HWND hWnd,
 int id,
 UINT fsModifiers,
 UINT vk
);
 
BOOL UnregisterHotKey( //刪除系統熱鍵的API函數
 HWND hWnd,
 int id
);
 
在C#中引用命名空間System.Runtime.InteropServices;來載入非託管類user32.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace HotKey
{
 
    public enum KeyModifiers //按鍵組合枚舉
    {
        None = 0,
        Alt = 1,
        Control = 2,
        Shift = 4,
        Windows = 8
    }
 
    public partial class Form1 : Form
    {
        /*
         * RegisterHotKey函數原型及說明:
         * BOOL RegisterHotKey(
         * HWND hWnd,         // window to receive hot-key notification
         * int id,            // identifier of hot key
         * UINT fsModifiers, // key-modifier flags
         * UINT vk            // virtual-key code);
         * 參數 id為你自己定義的一個ID值
         * 對一個線程來講其值必需在0x0000 - 0xBFFF範圍之內,十進位為0~49151
         * 對DLL來講其值必需在0xC000 - 0xFFFF 範圍之內,十進位為49152~65535
         * 在同一進程內該值必須唯一參數 fsModifiers指明與熱鍵聯合使用按鍵
         * 可取值為:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT參數,或數字0為無,1為Alt,2為Control,4為Shift,8為Windows
         * vk指明熱鍵的虛擬鍵碼
         */
 
        [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數
        public static extern bool RegisterHotKey(
         IntPtr hWnd, // handle to window
         int id, // hot key identifier
         uint fsModifiers, // key-modifier options
         Keys vk // virtual-key code
        );
        [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函數
        public static extern bool UnregisterHotKey(
         IntPtr hWnd, // handle to window
         int id // hot key identifier
        );
        public Form1()
        {
            InitializeComponent();
        }
        private void ProcessHotkey(Message m) //按下設定的鍵時調用該函數
        {
            IntPtr id = m.WParam; //IntPtr用於表示指標或控制代碼的平台特定類型
            //MessageBox.Show(id.ToString());
            string sid = id.ToString();
            switch (sid)
            {
                case "100":
                    MessageBox.Show("調用A函數");
                    break;
                case "200":
                    MessageBox.Show("調用B函數");
                    break;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //Handle為當前視窗的控制代碼,繼續自Control.Handle,Control為定義控制項的基類
            //RegisterHotKey(Handle, 100, 0, Keys.A); //註冊快速鍵,熱鍵為A
            //RegisterHotKey(Handle, 100, KeyModifiers.Alt | KeyModifiers.Control, Keys.B);//這時熱鍵為Alt+CTRL+B
            //RegisterHotKey(Handle, 100, 1, Keys.B); //1為Alt鍵,熱鍵為Alt+B
            RegisterHotKey(Handle, 100, 2,Keys.A); //定義熱鍵為Alt+Tab,這裡實現了螢幕系統Alt+Tab鍵
            RegisterHotKey(Handle, 200, 2, Keys.B); //註冊2個熱鍵,根據id值100,200來判斷需要執行哪個函數
        }
 
        private void button1_Click(object sender, EventArgs e) //重新設定熱鍵
        {
            UnregisterHotKey(Handle, 100);//卸載快速鍵
            RegisterHotKey(Handle, 100, 2, Keys.C); //註冊新的快速鍵,參數0表示無按鍵組合
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) //退出程式時缷載熱鍵
        {
            UnregisterHotKey(Handle, 100);//卸載第1個快速鍵
            UnregisterHotKey(Handle, 200); //缷載第2個快速鍵
        }
 
        //重寫WndProc()方法,通過監視系統訊息,來調用過程
        protected override void WndProc(ref Message m)//監視Windows訊息
        {
            const int WM_HOTKEY = 0x0312;//如果m.Msg的值為0x0312那麼表示使用者按下了熱鍵
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
                    break;
            }
            base.WndProc(ref m); //將系統訊息傳遞自父類的WndProc
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.