① Mouse messages:
Wm_mousemove Mouse Move position
Wm_lbuttondown the left mouse button press
Wm_lbuttonup the left mouse button to pop up
Wm_lbuttondblclk left mouse button double-click
Wm_rbuttondown right mouse button press
Wm_rbuttonup right mouse button bouncing up
Wm_rbuttondblclk right mouse button double-click
Wm_mbuttondown Middle mouse Button Press
Wm_mbuttonup mouse button Bouncing up
WM_MBUTTONDBLCLK middle mouse button double click
WM_MouseWheel Mouse wheel scrolling
Responding to mouse messages
Add the message response function of the object to the class and add the code you want to implement in the function.
② Keyboard Message:
Wm_keydown Keyboard Button Press
WM_CHAR can print characters
Wm_keyup keyboard keys pop up
Responding to keyboard messages
MFC dialog box program needs to overload the PreTranslateMessage function for key processing, add the relevant code as follows:
BOOL Cmfctestdlg::P retranslatemessage (msg* pMsg) { //Todo:add your specialized code here and/or call the base class
if (Pmsg->message = = wm_keydown) { switch (pmsg->wparam) {case Vk_up:messagebox (_t ("You pressed the UP arrow! ")); Break;case VK_DOWN:default:break; } } Return CDialog::P retranslatemessage (PMSG);}
Virtual Keyboard Code: MSDN Query [virtual-key Codes]
WM_KEYDOWN messages send Pmsg->wparam characters are uppercase, if you want to determine whether the pressed characters are uppercase or lowercase, you can use ASCII code in the WM_CHAR message to determine the Pmsg->wparam,
Or with the help of functions: Ischarlower, ischarupper to judge!
③ key combination Message response:
Use the Getkeystate function to get the pressed, bouncing state of a button
use:: Getkeystate () returns a short type, short is a 16-bit signed data type, if the key to be queried is pressed and the highest bit of the return value is set to 1, then this number represents negative numbers. So you can use <0 or >0 to judge.
0x8000 is a 16 binary number, represented as 1000 0000 0000 with 2 binary 0000, & is bitwise and &NBSP;&NBSP;&NBSP;
Similarly, if the key is pressed and the return value is the highest bit 1, the 1xxx xxxx xxxx xxxx & 1000 0000 0000 0000 results are 1, otherwise 0, you can also determine the highest bit value.
Note that: getkeystate () can only be used in a keyboard message handler because it only reports the state of the queried key when the thread reads the keyboard message from the message queue. If you need to query the key state outside the keyboard message handler, you need to use:: Getasynckeystate instead of
#define ISKEYPRESSED (Nvirtkey) ((Getkeystate (Nvirtkey) & (1<< (sizeof (short) *8-1))! = 0) #define Iskeytoggled (Nvirtkey) ((Getkeystate (Nvirtkey) & 1)! = 0)
Use the PreTranslateMessage method to determine whether the ctrl+q is pressed at the same time:
if (pmsg->message = = Wm_keydown && Pmsg->wparam = = ' Q ' && iskeypressed (Vk_control)) {
AfxMessageBox ("Ctrl + Q key combination is pressed");
return TRUE;
}
Add shortcut key resource, establish the method of shortcut key and message response way to realize the key combination message response.
1. Add a Hotkey table:
2. Add a hotkey combination:
3. Add the response function declaration in the format: afx_msg void Onctrlw ();
4. Add a message map in the format: on_bn_clicked (ID_ACC, ONCTRLW) or on_bn_clicked (ID_ACC, &CMFCTESTDLG::ONCTRLW)
5. Add the implementation of the response function:
void Cmfctestdlg::onctrlw () { MessageBox (_t ("You pressed the CTRL+W key! "));}
6. Initialize the shortcut key resource:
HACC = Loadaccelerators (AfxGetApp ()->m_hinstance, Makeintresource (Idr_accelerator1));
7. Intercept processing in the PreTranslateMessage function:
Switch (pmsg->message) {case wm_keydown: Case Wm_syskeydown://Alt, F10 key if (TranslateAccelerator (m_ HWnd, HACC, PMSG))
Differences between the two methods
The first method is more direct, easy to use, but generally only used in the program with fewer shortcut keys;
The second method is more practical, although the implementation of the code is slightly larger, but it is suitable for more shortcut keys in the program;
Defects
It is only valid when the program is currently active, and is not valid when the window is minimized or inactive!
④ Global Hotkey Registration
1. Call the RegisterHotKey function to register the global system hotkey;
BOOL RegisterHotKey ( hwnd hwnd, int ID, uint fsmodifiers, uint VK); HWND : Specifies the window handle to which the hotkey should be sounded; ID : The identification of the hotkey, a bit similar to the identity in the SetTimer, the scope is line range; Fsmodifiers: Specifies a combination of keys, which can consist of one or more of Mod_alt, Mod_control, Mod_shift, Mod_win; VK: Specifies the virtual keyboard code of the hotkey, such as: ' A ' NOTE: Here the ctrl+alt+a is the scope of the key combination;
Example: RegisterHotKey (M_hwnd, mod_control| Mod_alt, ' Q ');
2. Wm_hotkey message in response to the target window;
3. Call the Unregisterhotkey Uninstall Hotkey:
BOOL Unregisterhotkey (HWND hwnd, int ID); HWND : The window handle associated with the hotkey; ID : The identification of the hotkey;
It can be called in the OnDestroy () function of the dialog class so that both modal and non modal dialogs can be successfully unloaded;
Mouse Keyboard message response and System Hotkey Registration