zoukankan      html  css  js  c++  java
  • 给你的程序增加热键(C#)

    Form1.cs

                private void Form1_Load(object sender, System.EventArgs e)
                {
                      // 设置热键
                      SetHotKey(false, false, false,false,Keys.Space );
                }
               
                private bool key_Ctrl = false;
                private bool key_Shift = false;
                private bool key_Alt = false;
                private bool key_Windows = false;
                private Keys   key_other;

                public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool  bWindows,Keys nowKey)
                {
                      try
                      {
                            this.key_Alt = bAlt;
                            this.key_Ctrl = bCtrl;
                            this.key_Shift = bShift;
                            this.key_Windows = bWindows;
                            this.key_other = nowKey;
               
                            WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None;
               
                            if( this.key_Ctrl )
                                  modifier |= WinHotKey.KeyModifiers.Control;
                            if(this.key_Alt )
                                  modifier |= WinHotKey.KeyModifiers.Alt;
                            if(this.key_Shift)
                                  modifier |= WinHotKey.KeyModifiers.Shift;
                            if(this.key_Windows)
                                  modifier |= WinHotKey.KeyModifiers.Windows;
               
                            WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
                      }
                      catch
                      {
                            MessageBox.Show ("快捷键定义错误!");
                      }
                }
               
                protected override void WndProc(ref Message msg )
                {
                      const int WM_HOTKEY =  0x0312; // 热键消息

                      if (msg.Msg != WM_HOTKEY)
                      {
                            base.WndProc(ref msg);
                      }
                      else
                      {
                            MessageBox.Show("Hotkey pressed"); //激活热键
                            // ProcessHotKey();
                      }
                }

                private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
                {
                      WinHotKey.UnregisterHotKey(Handle, 100);      // 注销热键
                }


    WinHotKey.cs

          public class WinHotKey
          {
                public WinHotKey()
                {
               
                }

                [DllImport("user32.dll",SetLastError=true)]
                public static extern bool RegisterHotKey(
                      IntPtr hWnd,
                      int id,
                      KeyModifiers fsModifiers,
                      Keys vk
                      );

                [DllImport("user32.dll",SetLastError=true)]
                public static extern bool UnregisterHotKey(
                      IntPtr hWnd,
                      int id
                      );

                [Flags()]
                public enum KeyModifiers
                {
                      None = 0,
                      Alt = 1,
                      Control =2,
                      Shift = 4,
                      Windows = 8
                }

          }

    完整源码下载
  • 相关阅读:
    2017.11.22 mysql数据库实现关联表更新sql语句
    2017.11.21 基于JSP+Servlet+JavaBean实现复数运算(二)
    2017.11.20 基于JSP+Servlet+JavaBean实现复数运算(一)
    2017.11.19 如何设计好的数据库
    [专项]链表面试题汇总
    java 的三种代理模式
    我们为什么要使用AOP?
    十张图让你了解阿里公司架构设计的发展变化史(yet)
    Java程序员从阿里、京东、美团面试回来,这些面试题你会吗?(yet)
    [专项]jvm面试题(yet)
  • 原文地址:https://www.cnblogs.com/top5/p/1634948.html
Copyright © 2011-2022 走看看