When the application Program When running, you press the shortcut key to generate a key message. In Windows, how does one convert it into a message that responds with the shortcut key? This requires the translateaccelerator function. The main function of the translateaccelerator function is to compare the keys defined in the message and shortcut keys table. If any shortcut keys are found, the key message is converted to the wm_command or wm_syscommand message and sent to the message processing function in the window.
The function translateaccelerator declaration is as follows:
Winuserapi
Int
Winapi
Translateacceleratora (
_ In hwnd,
_ In haccel hacctable,
_ In lpmsg );
Winuserapi
Int
Winapi
Translateacceleratorw (
_ In hwnd,
_ In haccel hacctable,
_ In lpmsg );
# Ifdef Unicode
# Define translateaccelerator translateacceleratorw
# Else
# Define translateaccelerator translateacceleratora
# Endif //! Unicode
Hwnd is the window handle.
Hacctable is a shortcut key definition table.
Lpmsg is the current message.
If the function call is successful, a non-zero value is returned. If it fails, 0 is returned.
An example of calling this function is as follows:
#001 // main program entry
#002 //
#003 // Cai junsheng 2007/07/19
#004 // QQ: 9073204.
#005 //
#006 int apientry _ twinmain (hinstance,
#007 hinstance hprevinstance,
#008 lptstr lpcmdline,
#009 int ncmdshow)
#010 {
#011 unreferenced_parameter (hprevinstance );
#012 unreferenced_parameter (lpcmdline );
#013
#014 //
#015 MSG;
#016 haccel hacceltable;
#017
#018 // load the global string.
#019 loadstring (hinstance, ids_app_title, sztitle, max_loadstring );
#020 loadstring (hinstance, idc_testwin, szwindowclass, max_loadstring );
#021 myregisterclass (hinstance );
#022
#023 // application initialization:
#024 if (! Initinstance (hinstance, ncmdshow ))
#025 {
#026 return false;
#027}
#028
#029 hacceltable = loadaccelerators (hinstance, makeintresource (idc_testwin ));
#030
#031 // message loop:
#032 bool Bret;
#033 while (Bret = getmessage (& MSG, null, 0, 0 ))! = 0)
#034 {
#035 if (Bret =-1)
#036 {
#037 // handling error.
#038
#039}
#040 else if (! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
#041 {
#042 translatemessage (& MSG );
#043 dispatchmessage (& MSG );
#044}
#045}
#046
#047 return (INT) msg. wparam;
#048}
#049
Line 2 calls the translateaccelerator function to process the shortcut key and convert it to the Command Message for sending.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/07/21/1701151.aspx
While ( 1 )
{
If (Peekmessage ( & MSG, null, 0 , 0 , Pm_remove ))
{
If ( ! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
{
Translatemessage ( & MSG );
Dispatchmessage ( & MSG );