• C# WinForm中的快捷键实现方法


     1. 声明系统API:

       [DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

            [DllImport("user32.dll", EntryPoint = "UnregisterHotKey", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    重载OnLoad,注册热键:

            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                WinApi.RegisterHotKey(this.Handle, 225, 0, (int)Keys.F10);
                WinApi.RegisterHotKey(this.Handle, 226, 0, (int)Keys.F11);
            }

    在FormClosing事件中注销热键:

            private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
            {
                WinApi.UnregisterHotKey(this.Handle, 225);
                WinApi.UnregisterHotKey(this.Handle, 226);
            }

     重载 WndProc函数 处理热键

            protected override void WndProc(ref Message m)
            {
                 base.WndProc(ref m);

                switch (m.Msg)
                {

                    case 0x0312:    //这个是window消息定义的注册的热键消息

                        if (m.WParam.ToString().Equals("225"))  //F11
                        {
                            this.buttonItem_ToolBar_电网监测_Click(null, null);
                        }

                        else if (m.WParam.ToString().Equals("226"))  //F12
                        {
                            this.panel1.Visible = !this.panel1.Visible;
                        }
                        break;
                }
            }

  • 相关阅读:
    Node项目
    Angular模块/服务/MVVM
    Angular介绍1
    Node环境配置及Gulp工具
    Linux及Git介绍
    数据库MySQL
    ReactiveCocoa 监听枚举类型enumerate 或者 NSInteger类型
    ReactiveCocoa 监听布尔(BOOL)类型改变
    python3.7 urlopen请求HTTPS警告'CERTIFICATE_VERIFY_FAILED'解决办法
    Centos yum命令
  • 原文地址:https://www.cnblogs.com/zhaobl/p/2137422.html
Copyright © 2020-2023  润新知