Reference namespace
Using system. runtime. interopservices;
Reference Methods in a class
[Dllimport ("user32.dll", setlasterror = true)]
Public static extern bool registerhotkey (intptr hwnd, int ID, uint control, keys VK );
Private void form2_load1 (Object sender, system. eventargs E)
{
Registerhotkey (this. Handle, 888,2, keys. s );
// Handle: handle 888 of the window: The serial ID 2: crtl serial S: s serial
}
Private void form2_load1 (Object sender, system. eventargs E)
{
Unregisterhotkey (this. Handle, 888 );
// Handle: handle 888 of the window body: the parameter ID of the preceding Parameter
}
Protected override void wndproc (ref message m)
{
Switch (M. msg)
{
Case 0x0312: // This is a delayed response message of the fixed window message.
If (M. wparam. tostring (). Equals ("888") // if it is the region of our region
MessageBox. Show ("You press Ctrl + ");
Break;
}
Base. wndproc (ref m );
}
This is the definition of "variable". Alt + crtl is 3 and can be directly added.
If sendmessagea is used
Sendmessage (this. Handle, wm_sethotkey, 0x45a, 0)
// Wm_sethotkey is 0x32
// 0x45a the High-character is contrl character, the low-character is a B c d, so 5A is Z's ASCII 4 is ALT, that is, ALT + z
Demo
Http://files.cnblogs.com/5tao/work day .rar
Refer:
You are Program You need to register the global hotkey yourself,
APIs used
Registerhotkey
Unregisterhotkey
First, create a winhotkey class, as shown below:
Public class winhotkey
{
[Dllimport ("user32.dll", setlasterror = true)]
Public static extern bool registerhotkey (
Intptr hwnd, // window handle
Int ID,
Keymodifiers fsmodifiers,
Keys VK
);
[Dllimport ("user32.dll", setlasterror = true)]
Public static extern bool unregisterhotkey (
Intptr hwnd,
Int ID
);
[Flags ()]
Public Enum keymodifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
Public winhotkey ()
{
//
// Todo: add the constructor logic here
//
}
}
Then, call
// Shortcut key definition
Private bool key_ctrl = false;
Private bool key_shift = false;
Private bool key_alt = false;
Private bool key_windows = false;
Private Keys key_other;
Public void sethotkey (bool bctrl, bool bshift, bool Balt, bool
Bwindows, keys nowkey)
{
Try
{
This. key_alt = Balt;
This. key_ctrl = bctrl;
This. key_shift = bshift;
This. key_windows = bwindows;
This. key_other = nowkey;
Winhotkey. keymodifiers modifier = winhotkey. keymodifiers. None;
If (this. key_ctrl)
Modifier | = winhotkey. keymodifiers. Control;
If (this. key_alt)
Modifier | = winhotkey. keymodifiers. ALT;
If (this. key_shift)
Modifier | = winhotkey. keymodifiers. shift;
If (this. key_windows)
Modifier | = winhotkey. keymodifiers. windows;
Winhotkey. registerhotkey (handle, 100, modifier, nowkey );
}
Catch
{
// Login. showmessage ("the shortcut key definition is incorrect! ");
}
}
// Activate the hotkey
Protected override void wndproc (ref message m)
{
Const int wm_hotkey = 0x0312;
Switch (M. msg)
{
Case wm_hotkey:
{
// If a new message exists, a message is displayed.
If (incluenewmessage = true)
{
For (INT I = 0; I {
Maninfor searchman = (maninfor) This. maninforlist [I];
If (searchman. maninforid. Equals (getfriendid ))
{
Searchman. clicked = true;
Searchman. p2pshow ();
Break;
}
}
}
Else
{
This. Show ();
This. topmost = true;
This. panel_main.refresh ();
This. windowstate = system. Windows. Forms. formwindowstate. normal;
}
}
Break;
}
Base. wndproc (ref m );
}
Study by yourself
Void regallkey (hwnd winhand)
{
Registerhotkey (winhand, 96, mod_alt, 96); // 0
Registerhotkey (winhand, 97, mod_alt, 97); // 1
Registerhotkey (winhand, 98, mod_alt, 98); // 2
Registerhotkey (winhand, 99, mod_alt, 99); // 3
Registerhotkey (winhand, 100, mod_alt, 100); // 4
Registerhotkey (winhand, 101, mod_alt, 101); // 5
Registerhotkey (winhand, 102, mod_alt, 102); // 6
Registerhotkey (winhand, 103, mod_alt, 103); // 7
Registerhotkey (winhand, 104, mod_alt, 104); // 8
Registerhotkey (winhand, 105, mod_alt, 105); // 9
Registerhotkey (winhand, 106, mod_alt, 107); // Password
Registerhotkey (winhand, 107, mod_alt, 109); // query
Registerhotkey (winhand, 108, mod_alt, 106); // return
Registerhotkey (winhand, 109, mod_alt, 111); // confirm
Registerhotkey (winhand, 110, mod_shift, 111); // left
Registerhotkey (winhand, 111, mod_shift, 106); // right
Registerhotkey (winhand, 960, mod_alt, 45); // 0
Registerhotkey (winhand, 970, mod_alt, 35); // 1
Registerhotkey (winhand, 980, mod_alt, 40); // 2
Registerhotkey (winhand, 990, mod_alt, 34); // 3
Registerhotkey (winhand, 1000, mod_alt, 37); // 4
Registerhotkey (winhand, 1010, mod_alt, 12); // 5
Registerhotkey (winhand, 1020, mod_alt, 39); // 6
Registerhotkey (winhand, 1030, mod_alt, 36); // 7
Registerhotkey (winhand, 1040, mod_alt, 38); // 8
Registerhotkey (winhand, 1050, mod_alt, 33); // 9
}
Void unregallkey (hwnd winhand)
{
For (INT I = 96; I <122; I ++)
Unregisterhotkey (winhand, I); // 0
For (I = 96; I <106; I ++)
Unregisterhotkey (winhand, I * 10); // 0
}
The numeric key is Alt + num0 ~ Num9 combination
The password key is the plus sign of ALT + keypad
The query key is Alt + small key.
The Return key is Alt + keypad Asterisk
The confirmation key is Alt + keypad/Number
Shift + keypad/Number
The right arrow key is SHIFT + keypad Asterisk
Window movement:
Using system. runtime. interopservices;
[Dllimport ("user32.dll")]
Public static extern int movewindow (intptr hwnd, int X, int y, int M, int N, bool repaint );
+ Movewindow (myfrmshowlogin. Handle, 1024,0, 392,240, true );