Yongq Wong

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
注册快捷键--显示和隐藏窗口

   //注册热键的API

       [DllImport("user32")]

        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);

       //解除注册热键的API
        [DllImport("user32")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        private void Form1_Load(object sender, System.EventArgs e)
        {
            RegisterHotKey(this.Handle,11, 2, Keys.Q);     //注册热键
            RegisterHotKey(this.Handle, 22, 2, Keys.A); 
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(this.Handle, 11);                   // 撤消注册热键
            UnregisterHotKey(this.Handle, 22);
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0312:       //热键消息
                    if (m.WParam.ToString() == "11")   // 按下CTRL+Q 隐藏窗口
                    {
                        this.Hide();
                    }
                    else if (m.WParam.ToString() == "22")   //按下CTRL+A显示窗口
                    {
                        this.Visible = true;
                    }
                    break;
            }
            base.WndProc(ref m);
        }

posted on 2009-04-24 12:01  Yongq wong  阅读(302)  评论(0编辑  收藏  举报