winform接收全局的快捷键

public class NativeWIN32
    {
        public NativeWIN32()
        { }
        /* ------- using WIN32 Windows API in a C# application ------- */

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static public extern IntPtr GetForegroundWindow(); //

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct STRINGBUFFER
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string szText;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;

        public delegate bool EnumThreadProc(IntPtr hwnd, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool EnumThreadWindows(int threadId, EnumThreadProc pfnEnum, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle);

        /* ------- using HOTKEYs in a C# application -------

        in form load :
         bool success = RegisterHotKey(Handle, 100,     KeyModifiers.Control | KeyModifiers.Shift, Keys.J);

        in form closing :
         UnregisterHotKey(Handle, 100);

        protected override void WndProc( ref Message m )
        {
         const int WM_HOTKEY = 0x0312;  

         switch(m.Msg)
         {
          case WM_HOTKEY:
           MessageBox.Show("Hotkey pressed");
           break;
         }
         base.WndProc(ref m );
        }

        ------- using HOTKEYs in a C# application ------- */

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(IntPtr hWnd, // handle to window
         int id,            // hot key identifier
         KeyModifiers fsModifiers,  // key-modifier options
         Keys vk            // virtual-key code
         );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(IntPtr hWnd,  // handle to window
         int id      // hot key identifier
         );

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

在Form中Load事件中首先注册热键

        /// <summary>
        /// 注册热键
        /// </summary>
        /// <param name="c">按键</param>
        /// <param name="bCtrl">是否需要ctrl</param>
        /// <param name="bShift">是否需要shift</param>
        /// <param name="bAlt">是否需要alt</param>
        /// <param name="bWindows">是否需要win</param>
        public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
        {
            //先赋到变量
            Keys m_key = c;
            bool m_ctrlhotkey = bCtrl;
            bool m_shifthotkey = bShift;
            bool m_althotkey = bAlt;
            bool m_winhotkey = bWindows;

            //注册系统热键
            NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
            if (bCtrl)
                modifiers |= NativeWIN32.KeyModifiers.Control;
            if (bShift)
                modifiers |= NativeWIN32.KeyModifiers.Shift;
            if (bAlt)
                modifiers |= NativeWIN32.KeyModifiers.Alt;
            if (bWindows)
                modifiers |= NativeWIN32.KeyModifiers.Windows;
            NativeWIN32.RegisterHotKey(Handle, 100, modifiers, c);
        }

然后监听消息,处理事件

        /// <summary>
        /// 重写windows消息响应
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            const int wmHotkey = 0x0312;
            switch (m.Msg)
            {
                case wmHotkey:
                    WindowMax();
                    break;
            }
            base.WndProc(ref m);
        }

winform接收全局的快捷键

时间: 2024-10-10 05:03:25

winform接收全局的快捷键的相关文章

WinForm程序全局捕捉异常处理办法

如何全局捕捉Winform程序异常呢,当然是从程序启动入口的Program类下的Main()方法定义了,下面看下这个类怎么写的吧 static class Program { static string RunFormFullName { get { string setRunFormFullName = CIPACE.Sys.Configuration.RunFormFullName; if (setRunFormFullName == null) setRunFormFullName = D

phpstorm的全局操作快捷键ctrl+shift+f被搜狗占用处理方法

1.找到搜狗软件,右键选择属性设置 2.如图选择系统功能快捷键 3.去掉简繁切换快捷方式,确定后即可使用phpstorm的ctrl+shift+f来进行全局查找 原文地址:https://www.cnblogs.com/zhengchuzhou/p/9929225.html

黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键

1.新建一个类 HotkeyHelper  using System; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Collections; namespace 黄聪 { public delegate void HotkeyEventHandler(int hotKeyID); public class HotkeyHelper : IMessageFilter { public

winform 记录全局异常捕获

/// <summary> /// 应用程序的主入口点. /// </summary> public static ApplicationContext context; [STAThread] private static void Main() { try { //处理未捕获的异常 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Applicatio

WinForm C#全局错误捕捉处理【整理】

1 static class Program 2 { 3 /// <summary> 4 /// 应用程序的主入口点. 5 /// </summary> 6 [STAThread] 7 static void Main() 8 { 9 try 10 { 11 12 //添加事件处理程序未捕获的异常 13 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 14 //添加事件处理U

winform / Dev全局皮肤组件

话不多说先上效果图. 由于这是单独的测试项目, 用于演示Dev控件的皮肤样式, 所以上面只是演示了部分控件的效果. 下面则是一些实际项目中的截图: Dev的控件样式不仅美观丰富, 上面仅皮肤设置就有40多种, 针对各种用户的界面设计. 那么下面,我就讲一下dev皮肤的几个核心代码原理和实现. 1.原理 1. 添加引用Dev的皮肤控件DLL,有两个部分组成, 40多种组合 2.通过已经构造好的类去注册连个皮肤 将所有的皮肤加载到下拉列表当中 3.设置皮肤的样式 4.保存到用户配置至本地文件 定义实

winform捕捉全局异常

/// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { try { //设置应用程序处理异常方式:ThreadException处理 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += new Sys

Winform,Wpf快捷键

基类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.Windows.Input; 7 using WUtilitiesV01.Controls; 8 9 namespace WUtilitiesV01.Services 10 { 11 public abstract

删除 Intel HD Graphics 显卡工具的全局快捷键

Intel的内置显卡工具总是注册全局的快捷键,而且还是常用的(比如 Sublime Text, PyCharm等工具的默认按键绑定),如下: Ctrl + Shift + Up Ctrl + Shift + Down Ctrl + Shift + Left Ctrl + Shift + Right 这些快捷键都是由 igfxHK.exe 注册的,虽然可以在任务栏中点击禁用快捷键,但是由于快捷键是全局注册,其它应用程序仍然不能使用这些快捷键. 虽然使用 taskkill /IM igfxHK.ex