假如我想实现用Alt+D实现隐藏窗口,然后用Alt+H显示   请问怎么实现。

解决方案 »

  1.   

    使用RegisterHotKey和UnregisterHotKey这两个WINAPI
      

  2.   

    RegisterHotKey注册全局热键http://blog.sina.com.cn/s/blog_49bf414d0100c9d8.html
      

  3.   

    // 定义常量和方法
    private const int HIDE_ID = 0x1111;
    private const int SHOW_ID = 0x1112;[DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);// 窗体构造函数添加代码
    const uint MOD_ALT = 0x0001;
    RegisterHotKey(this.Handle, HIDE_ID, MOD_ALT, (uint)Keys.D);
    RegisterHotKey(this.Handle, SHOW_ID, MOD_ALT, (uint)Keys.H);// 重写窗体 WndProc 方法
    protected override void WndProc(ref Message m)
    {
        const int WM_HOTKEY = 0x0312;
        switch (m.Msg)
        {
            case WM_HOTKEY:
                {
                    switch (m.WParam.ToInt32())
                    {
                        case HIDE_ID:
                            this.Visible = false;
                            break;
                        case SHOW_ID:
                            this.Visible = true;
                            break;
                        default:
                            break;
                    }
                    break;
                }
            default:
                break;
        }
        base.WndProc(ref m);
    }