Winform multi-combination boss key Alt_Ctrl_Shift

Source: Internet
Author: User

One day, in order to add a boss key function to Weibo fans, I began to look for it from the Internet in a conventional way: the keywords are similar to "C # Boss key, however, the Code is basically the same: Normally, the boss key is usually indispensable: Alt + Ctrl + Shift + XX multi-combination method. However, all kinds of code is not directly described, and there is no prompt. It seems that it is intentionally hidden. Finally, I found some unknown hidden properties:Next, let's take a look at the common code for self-modified networks: public delegate void HotkeyEventHandler (int HotKeyID );

Public class SystemHotKey: System. Windows. Forms. IMessageFilter
{
List <UInt32> keyIDs = new List <UInt32> ();
IntPtr hWnd;

Public event HotkeyEventHandler OnHotkey;

Public enum KeyFlags
{
Alt = 0x1,
Ctrl = 0x2,
Shift = 0x4,
Win = 0x8,
// The key combination equals the sum of Values
Alt_Ctrl = 0x3,
Alt_Shift = 0x5,
Ctrl_Shift = 0x6,
Alt_Ctrl_Shift = 0x7
}
[DllImport ("user32.dll")]
Public static extern UInt32 uregisterhotkey (IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk );

[DllImport ("user32.dll")]
Public static extern UInt32 uunregisterhotkey (IntPtr hWnd, UInt32 id );

[DllImport ("kernel32.dll")]
Public static extern UInt32 GlobalAddAtom (String lpString );

[DllImport ("kernel32.dll")]
Public static extern UInt32 uglobaldeleteatom (UInt32 nAtom );

Public SystemHotKey (IntPtr hWnd)
{
This. hWnd = hWnd;
}

Public int RegisterHotkey (KeyFlags keyflags, System. Windows. Forms. Keys Key)
{
System. Windows. Forms. Application. AddMessageFilter (this );
UInt32 hotkeyid = GlobalAddAtom (System. Guid. NewGuid (). ToString ());
RegisterHotKey (IntPtr) hWnd, hotkeyid, (UInt32) keyflags, (UInt32) Key );
KeyIDs. Add (hotkeyid );
Return (int) hotkeyid;
}

Public void UnregisterHotkeys ()
{
If (keyIDs. Count> 0)
{

System. Windows. Forms. Application. RemoveMessageFilter (this );
Foreach (UInt32 key in keyIDs)
{
UnregisterHotKey (hWnd, key );
GlobalDeleteAtom (key );
}
KeyIDs. Clear ();
}
}

Public bool PreFilterMessage (ref System. Windows. Forms. Message m)
{
If (m. Msg = 0x312)
{
If (OnHotkey! = Null)
{
Foreach (UInt32 key in keyIDs)
{
If (UInt32) m. WParam = key)
{
OnHotkey (int) m. WParam );
Return true;
}
}
}
}
Return false;
}
}The following are some key points:1: System. Windows. Forms. Application. AddMessageFilter (this); this statement must correspond to System. Windows. Forms. Application. RemoveMessageFilter (this). Remember to cancel it when you use it. Because the original program is added only to the constructor, the setting will become invalid after cancellation. Here, it is added directly at registration and removed when cancellation, pay attention to this effect. 2: hotkey combination: // The combination key equals to the value addition Alt_Ctrl = 0x3, Alt_Shift = 0x5, Ctrl_Shift = 0x6, alt_Ctrl_Shift = 0x7 This is a result of casual thinking. I did not mention any code on the Internet. It is estimated that there are too many people to turn around and I will not write anything I know. 3: Change Hastable to List <Unint32>. I 've had a lot of things recently and it's easy to write articles. Sorry.

This article from the "passing by autumn" blog, please be sure to keep this source http://cyq1162.blog.51cto.com/2127378/818498

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.