How does the message queue work in Win32?

后端 未结 3 1772
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 21:49

I read some stuff on Win32 and how the message loop works, and there\'s something that is still unclear to me: What exactly is stored in the message queue? The integer value

3条回答
  •  时光说笑
    2021-01-02 22:28

    To answer your question narrowly, each message in the queue stores, at the least,

    • a window handle to which the message is directed,
    • the message code, wParam and lParam, as you already correctly noted,
    • the time when the message was posted, that you retrieve with GetMessageTime(),
    • for UI messages, the position of the cursor when the message was posted (see GetMessagePos()).

    Note that not all messages are actually stored in the queue. Messages that are sent with SendMessage() to a window from the thread that owns the window are never stored; instead, a receiver window's message function is called directly. Messages sent from other threads are stored until processed, and the sending thread blocks until the message is replied to, either by exiting the window function or explicitly with a call to ReplyMessage(). The API function InSendMessage() helps figure out whether the windows function is processing a message sent from another thread.

    Messages that you or the system post are stored in the queue, with some exceptions.

    • WM_TIMER messages are never actually stored; instead, GetMessage() constructs a timer message if there are no other messages in the queue and a timer has matured. This means that, first, the timer messages have the lowest dequeuing priority, and, second, that multiple messages from a short period timer would never overflow queue, even if GetMessage() is not called for a while. As a result, a single WM_TIMER message is sent for the given timer, even if the timer has elapsed multiple times since the last WM_TIMER message from that timer has been processed.

    • Similarly, WM_QUIT is also not stored, and rather only flagged. GetMessage() pretends to have retrieved the WM_QUIT after the queue has been exhausted, and this is the last message it retrieves.

    • Another example is the WM_PAINT message (hat tip to @cody-gray for reminding about this). This message is also simulated when any part of the window¹ is marked as "dirty" and needs repainting. This is also a low-priority message, made so that multiple invalidated regions in a window are repainted all at once when the queue becomes empty, to reduce responsiveness of the GUI and reduce flicker. You can force an immediate repaint by calling UpdateWindow(). This function acts like SendMessage(), in the sense that it does not return until the exposed part of the window is redrawn. This function does not send a WM_PAINT to the window if the invalid region of that window is empty, as an obvious optimization.

    Probably, there are other exceptions and internal optimizations.

    Messages posted with PostMessage() end up in the queue of a thread that owns the window to which the message is posted.

    In what form the messages are stored internally, we do not know, and we do not care. Windows API abstracts that completely. The MSG structure is filled in memory you pass to GetMessage() or PeekMessage(). You do not need to know or to worry about the details of internal implementation beyond those documented in Windows SDK guides.


    ¹ I do not know how exactly WM_PAINT and WM_TIMER are prioritized relative to each other. I assume WM_PAINT has a lower priority, but I may be wrong.

提交回复
热议问题