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;
                }
            }

  • 相关阅读:
    Today is 5.20,her birthday...
    哭有时,笑有时,悲伤有时,欢乐有时。
    母亲节……
    性格决定命运...
    男人一生只会最爱一个女人?男人的爱一辈子只会付出一次?
    利物浦输了……You'll never walk alone!
    Liverpool,give me power again.Control myself.
    Python编程题27合并两个有序列表
    Python编程题25最长回文串的长度
    Python编程题26爬楼梯
  • 原文地址:https://www.cnblogs.com/zhaobl/p/2137422.html
Copyright © 2011-2022 走看看