zoukankan      html  css  js  c++  java
  • 黄聪: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 event HotkeyEventHandler OnHotkey;
    
            public enum KeyFlags
            {
                MOD_ALT = 0x1,
                MOD_CONTROL = 0x2,
                MOD_SHIFT = 0x4,
                MOD_WIN = 0x8
            }
    
            class NativeMethods
            {
                private NativeMethods() { }
    
                #region WIN32 API
    
                [DllImport("user32.dll")]
                public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
    
                [DllImport("user32.dll")]
                public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
    
                [DllImport("kernel32.dll")]
                public static extern UInt32 GlobalAddAtom(String lpString);
    
                [DllImport("kernel32.dll")]
                public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
    
                #endregion
            }
    
            Hashtable keyIDs = new Hashtable();
            IntPtr hWnd;
    
            public HotkeyHelper(IntPtr hWnd)
            {
                this.hWnd = hWnd;
                Application.AddMessageFilter(this);
            }
    
            public int RegisterHotkey(Keys Key, KeyFlags keyflags)
            {
                UInt32 hotkeyid = NativeMethods.GlobalAddAtom(System.Guid.NewGuid().ToString());
                NativeMethods.RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
                keyIDs.Add(hotkeyid, hotkeyid);
                return (int)hotkeyid;
            }
    
            public void UnregisterHotkeys()
            {
                Application.RemoveMessageFilter(this);
                foreach (UInt32 key in keyIDs.Values)
                {
                    NativeMethods.UnregisterHotKey(hWnd, key);
                    NativeMethods.GlobalDeleteAtom(key);
                }
            }
    
            public bool PreFilterMessage(ref Message m)
            {
                if (m.Msg == 0x312)
                {
                    if (OnHotkey != null)
                    {
                        foreach (UInt32 key in keyIDs.Values)
                        {
                            if ((UInt32)m.WParam == key)
                            {
                                OnHotkey((int)m.WParam);
                                return true;
                            }
                        }
                    }
                }
    
                return false;
            }
        }
    }

    2、在窗口创建(FrmMain_Load)的时候注册快捷键

    private void FrmMain_Load(object sender, EventArgs e)
    {
        hotkeyHelper = new HotkeyHelper(this.Handle);
        int favKey = hotkeyHelper.RegisterHotkey(Keys.F, HotkeyHelper.KeyFlags.MOD_CONTROL); //注册Ctrl+F
        int nextKey = hotkeyHelper.RegisterHotkey(Keys.G, HotkeyHelper.KeyFlags.MOD_CONTROL);//注册Ctrl+G
        hotkeyHelper.OnHotkey += new HotkeyEventHandler(OnHotkey);
    }

    3、热键的响应方法

    private void OnHotkey(int hotkeyID)
    {
        if (hotkeyID == favKey)
        {
            //do something
        }
        else if (hotkeyID == nextKey)
        {
            //do something else
        }
    }

    4、当然在窗体关闭的时候,一定要注销掉快捷键

    private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        hotkeyHelper.UnregisterHotkeys();
    }
  • 相关阅读:
    html_dom类读取
    PHPExcel读取Excel文件的实现代码
    PHP中的mb_convert_encoding与iconv函数介绍
    BZOJ 3160 万径人踪灭 解题报告
    BZOJ 4036 [HAOI2015] Set 解题报告
    BZOJ 3288 Mato矩阵 解题报告
    BZOJ 3173 [Tjoi2013] 最长上升子序列 解题报告
    BZOJ 4123 [Baltic2015] Hacker 解题报告
    BZOJ 4127 Abs 解题报告
    BZOJ 4145 [AMPPZ2014] The Prices 解题报告
  • 原文地址:https://www.cnblogs.com/huangcong/p/4047346.html
Copyright © 2011-2022 走看看