Why are “TranslateMessage” and “DispatchMessage” separate calls?

前端 未结 4 1505
孤城傲影
孤城傲影 2020-12-15 04:52

Most of the Win32 main loops I\'ve seen are all structured like:

while (GetMessage(&message, NULL, 0, 0) > 0) {
  TranslateMessage(&message);
  Di         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-15 05:23

    They are different beasts.

    For TranslateMessage function

    Translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function. [...] The TranslateMessage function does not modify the message pointed to by the lpMsg parameter.

    DispatchMessage, on the other hand, dispatches a message to a window procedure.

    So DispatchMessage does the actual work of processing the message. TranslateMessage MAY or MAY NOT post a new message to the thread queue. If the message is translated then a character message is posted to the thread's message queue.

    The TranslateMessage function does not modify the message pointed to by the lpMsg parameter.

    They are separate calls so you, the programmer, can have a chance to avoid the message translation provided by TranslateMessage.

提交回复
热议问题