winform接收全域的快速鍵

來源:互聯網
上載者:User

標籤:winform   style   class   blog   code   ext   

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.