One-day Windows API training (13) TranslateMessage Function

Source: Internet
Author: User

TranslateMessage is used to convert a virtual key message to a character message. Because Windows uses the definition of virtual keys for all keyboard encodings, when the keys are pressed, messages that do not have character messages need to be converted to character mappings.
The TranslateMessage function is used to convert a virtual key message to a character message. The character message is delivered to the Message Queue of the calling thread, and is taken out when the current GetMessage function is called. When we press a character key on the keyboard, the system will generate the WM_KEYDOWN and WM_KEYUP messages. The additional parameters (wParam and lParam) of the two messages contain information such as the virtual key code and scan code, and we usually need to obtain the ASCII code of a specific character in the program, the TranslateMessage function converts the combination of WM_KEYDOWN and WM _ KEYUP messages to a WM_CHAR message (the wParam additional parameter of the message contains the ASCII code of the characters ), and deliver the converted new message to the Message Queue of the calling thread. Note: The TranslateMessage function does not modify the original message. It only generates new messages and delivers them to the message queue.
That is to say, the TranslateMessage will find whether there is a message with a character key in the message. If there is a message with a character key, the WM_CHAR message will be generated. If not, the message will be generated.
 
The function TranslateMessage declaration is as follows:
WINUSERAPI
BOOL
WINAPI
TranslateMessage (
_ In const msg * lpMsg );
LpMsg is the message to be converted.
 
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 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
 
Row 3 calls the TranslateMessage function for message conversion.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/07/23/1702420.aspx

 

 

Function Description: converts a virtual key message to a character message. The character message is sent to the Message Queue of the calling thread and is read when the next thread calls the GetMessage or PeekMessage function.

. Function prototype:
BOOL TranslateMessage (const msg * lpMsg);. parameters:
LpMsg
A pointer to a MSG structure that uses the GetMessage or PeekMessage function to obtain message information from the message queue of the calling thread .. Return Value:
If the message is converted (that is, the character message is sent to the Message Queue of the thread), a non-zero value is returned.
If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, a non-zero value is returned, regardless of conversion.
If the message is not converted (that is, the character message is not sent to the Message Queue of the thread), the return value is zero .. Note:
The TranslateMessage function does not modify the message directed by the lpMsg parameter.
The message WM_KEYDOWN and WM_KEYUP combine to generate a WM_CHAR or WM_DEADCHAR message. Message WM_SYSKEYDOWN and WM_SYSKEYUP combine to generate a WM_SYSCHAR or WM_SYSDEADCHAR message.
TtanslateMessage only generates WM_CHAR messages for keys mapped to ASCII characters by the keyboard drive.
If the application processes a virtual key message for other purposes, the TranslateMessage function should not be called. For example, if the TranslateAccelerator function returns a non-zero value, the application will not call the TranslateMessage function.
Windows CE: Windows CE does not support scan code or extended key flag. Therefore, it does not support the value of lKeyData (lParam) 16-24 in the WM_CHAR message generated by the TranslateMessage function.
The TranslateMessage function can only be used to convert messages received by the GetMessage or PeekMessage function.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 1632241

 

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.