[摘]给窗体添加快捷键

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


        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;

            if (WM_HOTKEY == m.Msg)
            {
                if (m.WParam.ToInt32() == 101)
                { 
                    hidWindows(); 
                }
            }
            base.WndProc(ref m);
        }


     //显示 or 隐藏
     private void hidWindows()
        {
            if (this.Visible == false)
            {
                this.Visible = true;
                this.TopMost = true;
            }
            else
            {
                this.Visible = false;
            }
        }

注册快捷键:
/*        None = 0,   
            Alt = 1,   
            Ctrl = 2,   
            Shift = 4,   
            WindowsKey = 8  */
       RegisterHotKey(Handle, 101, 2, Keys.Q);
原文地址:https://www.cnblogs.com/qq1223558/p/3227128.html