C#注册全局热键的方法

在form里加入以下代码即可

 

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

            switch (m.Msg)
            {
                case WM_HOTKEY:
                    this.Close();
                    break;
            }
            base.WndProc(ref   m);
        }
       

        public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
        {
            NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
            if (bCtrl)
            {
                modifiers = NativeWIN32.KeyModifiers.Control;
            }
            else if (bShift)
            {
                modifiers = NativeWIN32.KeyModifiers.Shift;
            }
            else if (bAlt)
            {
                modifiers = NativeWIN32.KeyModifiers.Alt;
            }
            else if (bWindows)
            {
                modifiers = NativeWIN32.KeyModifiers.Windows;
            }
            NativeWIN32.RegisterHotKey(Handle, 100, modifiers, c);
        }

        public class NativeWIN32
        {
            public NativeWIN32()
            { }

            [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);


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


        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值