C# Winform 글로벌 단축키 추가(보스 키)

10016 단어 WinForm
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace drmaple
{
    class HotKey
    {
        //        ,     0。
        //        ,    0。         ,  GetLastError。
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
                        IntPtr hWnd,                //           
            int id,                     //    ID(     ID  )
            KeyModifiers fsModifiers,   //        Alt、Ctrl、Shift、Windows       
            Keys vk                     //       
            );
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                //           
            int id                      //      ID
            );
        //         (             ,              )
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }
    }
}

////“public static extern bool RegisterHotKey()”          。          user32.dll          ,  
//user32.dll      ,              ,     “DllImport”         。           
//“[DllImport("user32.dll", SetLastError = true)]”    。
//“public static extern bool UnregisterHotKey()”          ,      DllImport  user32.dll     。
//“public enum KeyModifiers{}”       ,                ,     。                
////(2)   FormA  ,  HotKey    
// FormA Activate       ,     Shift+S,Ctrl+Z,Alt+D     。   Id      ,        。

private void Form_Activated(object sender, EventArgs e)
{
    //    Shift+S,Id  100。HotKey.KeyModifiers.Shift         4   。
    HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S); 
    //    Ctrl+B,Id  101。HotKey.KeyModifiers.Ctrl         2   。
    HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);
    //    Alt+D,Id  102。HotKey.KeyModifiers.Alt         1   。
    HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);
}
// FormA Leave       。
private void FrmSale_Leave(object sender, EventArgs e)
{
    //  Id  100     
    HotKey.UnregisterHotKey(Handle, 100);
    //  Id  101     
    HotKey.UnregisterHotKey(Handle, 101);// http://ike.126.com
    //  Id  102     
    HotKey.UnregisterHotKey(Handle, 102);
}

  FromA  WndProc  
/// 
///   Windows  
///   WndProc  ,        
/// 
/// 
protected override void WndProc(ref Message m)
{

    const int WM_HOTKEY = 0x0312;
    //     
    switch (m.Msg)
    {
        case WM_HOTKEY:
            switch (m.WParam.ToInt32())
            {
                case 100:    //    Shift+S
                    //            
                    break;
                case 101:    //    Ctrl+B
                    //           
                    break;
                case 102:    //    Alt+D
                    //           
                    break;
            }
        break;
    }
    base.WndProc(ref m);
}

좋은 웹페이지 즐겨찾기