zoukankan      html  css  js  c++  java
  • 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;
                }
            }

  • 相关阅读:
    spring服务器的校验
    数据库中文到页面乱码 添加一个过滤器
    springmvc的form标签
    EasyUI触发方法、触发事件、创建对象的格式??
    JQuery如何获取按键的unicode编码?
    StringBuilder的append、StringBuffer的append和String str = "a"+"b"的区别?
    简述什么是ajax、javascript、json、Jquery?
    oracle分页
    Github的基本功能:
    Springmvc和struts2的区别?
  • 原文地址:https://www.cnblogs.com/zhaobl/p/2137422.html
Copyright © 2011-2022 走看看