最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于C#實現(xiàn)熱鍵注冊工具類

 更新時間:2023年12月05日 08:59:30   作者:rjcql  
這篇文章主要為大家詳細(xì)介紹了一個驗證過的熱鍵注冊工具類,使用系統(tǒng)類庫user32.dll中的RegisterHotkey函數(shù)來實現(xiàn)全局熱鍵的注冊,感興趣的小伙伴可以學(xué)習(xí)一下

寫在前面

介紹一個驗證過的熱鍵注冊工具類,使用系統(tǒng)類庫user32.dll中的RegisterHotkey函數(shù)來實現(xiàn)全局熱鍵的注冊。

代碼實現(xiàn)

    [Flags]
    public enum KeyModifiers
    {
        Alt = 1,
        Control = 2,
        Shift = 4,
        Windows = 8,
        NoRepeat = 0x4000
    }
 
    public static class HotKeyHelper
    {
        [DllImport("user32", SetLastError = true)]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
 
        [DllImport("user32", SetLastError = true)]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
 
        private static int _id = 0;
        private static volatile MessageWindow _wnd;
        private static volatile IntPtr _hwnd;
        private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
        static HotKeyHelper()
        {
            Thread messageLoop = new Thread(delegate ()
            {
                Application.Run(new MessageWindow());
            });
            messageLoop.Name = "MessageLoopThread";
            messageLoop.IsBackground = true;
            messageLoop.Start();
        }
 
        public static event EventHandler<HotKeyEventArgs> HotKeyPressed;
 
        public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
        {
            _windowReadyEvent.WaitOne();
            int id = System.Threading.Interlocked.Increment(ref _id);
            _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
            return id;
        }
 
        public static void UnregisterHotKey(int id)
        {
            _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
        }
 
        delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
        delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
 
        private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)
        {
            RegisterHotKey(hwnd, id, modifiers, key);
        }
 
        private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)
        {
            UnregisterHotKey(_hwnd, id);
        }
 
        private static void OnHotKeyPressed(HotKeyEventArgs e)
        {
            if (HotKeyHelper.HotKeyPressed != null)
            {
                HotKeyHelper.HotKeyPressed(null, e);
            }
        }
 
        private class MessageWindow : Form
        {
            public MessageWindow()
            {
                _wnd = this;
                _hwnd = this.Handle;
                _windowReadyEvent.Set();
            }
 
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_HOTKEY)
                {
                    HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
                    HotKeyHelper.OnHotKeyPressed(e);
                }
 
                base.WndProc(ref m);
            }
 
            protected override void SetVisibleCore(bool value)
            {
                // Ensure the window never becomes visible
                base.SetVisibleCore(false);
            }
 
            private const int WM_HOTKEY = 0x312;
        }
    }
 
    public class HotKeyEventArgs : EventArgs
    {
        public readonly Keys Key;
        public readonly KeyModifiers Modifiers;
 
        public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
        {
            this.Key = key;
            this.Modifiers = modifiers;
        }
 
        public HotKeyEventArgs(IntPtr hotKeyParam)
        {
            uint param = (uint)hotKeyParam.ToInt64();
            Key = (Keys)((param & 0xffff0000) >> 16);
            Modifiers = (KeyModifiers)(param & 0x0000ffff);
        }
    }

調(diào)用示例

        void RegisterHotKeyTest()
        {
            HotKeyHelper.RegisterHotKey(Keys.B, KeyModifiers.Alt);
            HotKeyHelper.HotKeyPressed += new EventHandler<HotKeyEventArgs>(OnHotKeyPressed);
        }
 
        void OnHotKeyPressed(object sender, HotKeyEventArgs e)
        {
            MessageBox.Show("Alt + B");
        }

執(zhí)行結(jié)果

如果需要注冊成Windows服務(wù)在后臺運行,需要開啟Service的允許與桌面交互選項。 

到此這篇關(guān)于基于C#實現(xiàn)熱鍵注冊工具類的文章就介紹到這了,更多相關(guān)C#熱鍵注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# Socket粘包處理講解示例

    C# Socket粘包處理講解示例

    這篇文章主要介紹了C# Socket粘包處理講解,大家可以參考使用
    2013-12-12
  • C#入門之checked和unchecked的區(qū)別實例解析

    C#入門之checked和unchecked的區(qū)別實例解析

    這篇文章主要介紹了C#中checked和unchecked的區(qū)別,是學(xué)習(xí)C#必須要牢固掌握的,需要的朋友可以參考下
    2014-08-08
  • C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn)

    C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn)

    本文主要介紹了C# Environment.CurrentDirectory 靜態(tài)屬性的實現(xiàn),它返回當(dāng)前應(yīng)用程序的工作目錄路徑,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • C#表達(dá)式中的動態(tài)查詢詳解【譯】

    C#表達(dá)式中的動態(tài)查詢詳解【譯】

    這篇文章主要給大家介紹了關(guān)于C#表達(dá)式中動態(tài)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 一文搞懂C# 數(shù)據(jù)類型

    一文搞懂C# 數(shù)據(jù)類型

    這篇文章主要介紹C# 數(shù)據(jù)類型的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C# PictureBox圖片控件實現(xiàn)圖片交換

    C# PictureBox圖片控件實現(xiàn)圖片交換

    在c#中可以使用PictureBox控件來呈現(xiàn)圖像,本文主要介紹了C# PictureBox實現(xiàn)圖片交換,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 利用C#實現(xiàn)SSLSocket加密通訊的方法詳解

    利用C#實現(xiàn)SSLSocket加密通訊的方法詳解

    這篇文章主要給大家介紹了關(guān)于如何利用C#實現(xiàn)SSLSocket加密通訊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C#微信公眾號開發(fā)之接收事件推送與消息排重的方法

    C#微信公眾號開發(fā)之接收事件推送與消息排重的方法

    這篇文章主要介紹了C#微信公眾號開發(fā)之接收事件推送與消息排重的方法,詳細(xì)分析了事件推送與消息排重的使用技巧,對微信開發(fā)有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解

    unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解

    這篇文章主要介紹了unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解,實現(xiàn)起來其實并不復(fù)雜,涉及的技術(shù)點有pc端和移動端屏幕拖動事件,二維向量的相關(guān)運算,手勢匹配算法,事件系統(tǒng)設(shè)計模式,需要的朋友可以參考下
    2023-12-12
  • c# 實現(xiàn)觀察者模式

    c# 實現(xiàn)觀察者模式

    這篇文章主要介紹了c# 實現(xiàn)觀察者模式的步驟,幫助大家更好的理解和使用c#實現(xiàn)設(shè)計模式,感興趣的朋友可以了解下
    2021-01-01

最新評論

梧州市| 师宗县| 通化县| 芮城县| 新疆| 长春市| 嫩江县| 墨脱县| 团风县| 济源市| 焉耆| 武宁县| 阿尔山市| 平凉市| 西华县| 丹棱县| 星座| 桐柏县| 锡林郭勒盟| 祥云县| 万荣县| 温州市| 赣榆县| 修水县| 广昌县| 建湖县| 东乌珠穆沁旗| 盘山县| 武川县| 石景山区| 黄浦区| 滦平县| 大关县| 普定县| 郑州市| 常德市| 余江县| 伊宁县| 平江县| 克山县| 万州区|