[摘]给窗体添加快捷键

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

 

posted @ 2013-07-31 10:13  Micmacs  阅读(208)  评论(0编辑  收藏  举报