欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

寫在前面

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

代碼實(shí)現(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é)果

如果需要注冊(cè)成Windows服務(wù)在后臺(tái)運(yùn)行,需要開啟Service的允許與桌面交互選項(xiàng)。 

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

相關(guān)文章

  • c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)

    c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)

    c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)...
    2007-08-08
  • C#讀取CSV文件的方法總結(jié)

    C#讀取CSV文件的方法總結(jié)

    CSV文件是一種簡(jiǎn)單的文本文件格式,用于存儲(chǔ)表格數(shù)據(jù),在C#中,有多種方法可以用于讀取CSV文件,本文將介紹幾種常見的讀取CSV文件的方法,包括使用System.IO命名空間中的類、使用CsvHelper庫(kù)以及使用LINQ,需要的朋友可以參考下
    2024-05-05
  • C#串口通信實(shí)現(xiàn)方法

    C#串口通信實(shí)現(xiàn)方法

    這篇文章主要介紹了C#串口通信實(shí)現(xiàn)方法,詳細(xì)講述了C#串口通信所涉及的數(shù)據(jù)接收與發(fā)送方法,以及相關(guān)的線程調(diào)用方法,是非常典型的應(yīng)用,需要的朋友可以參考下
    2014-12-12
  • C#實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型轉(zhuǎn)換方法詳解

    C#實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型轉(zhuǎn)換方法詳解

    這篇文章主要為大家介紹了C#中一維數(shù)組如何快速實(shí)現(xiàn)數(shù)組元素的數(shù)據(jù)類型的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-04-04
  • C#給Excel添加水印實(shí)例詳解

    C#給Excel添加水印實(shí)例詳解

    這篇文章主要介紹了C#給Excel添加水印實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • c#中合并excel表格的方法示例

    c#中合并excel表格的方法示例

    本篇文章主要介紹了c#中合并excel表格的方法,就是將excel表格結(jié)構(gòu)一樣的合并到一起,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • C#實(shí)現(xiàn)簡(jiǎn)單文本編輯器

    C#實(shí)現(xiàn)簡(jiǎn)單文本編輯器

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單文本編輯器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法

    C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法,可實(shí)現(xiàn)系統(tǒng)服務(wù)的啟動(dòng)和停止功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#讀寫INI文件的最簡(jiǎn)方法

    C#讀寫INI文件的最簡(jiǎn)方法

    INI文件,全稱為Initialization File(初始化文件),是一種傳統(tǒng)的文本型配置文件格式,廣泛應(yīng)用于Windows操作系統(tǒng)及早期應(yīng)用程序中,本文將聚焦于C#語(yǔ)言環(huán)境下,介紹如何以最簡(jiǎn)化的方式實(shí)現(xiàn)INI文件的讀寫操,需要的朋友可以參考下
    2025-01-01
  • C#屬性顯示的實(shí)現(xiàn)示例

    C#屬性顯示的實(shí)現(xiàn)示例

    本文主要介紹了C#屬性顯示的實(shí)現(xiàn)示例,顯示對(duì)象的屬性,包括可顯示屬性、可編輯屬性、及不可編輯屬性,下面就具有來(lái)介紹一下,感興趣的可以了解一下
    2024-04-04

最新評(píng)論