Is DispatchMessage required in win32 programs?

Win32 programs typically have a message loop that involves calling GetMessage or PeekMessage , and then calling DispatchMessage to send a message to the window. corresponding window.

But is there a need for this? Can I instead just look in the MSG object directly in the message loop and perform the necessary actions there without calling DispatchMessage ? I am talking about cases where I have one window without any other window controls, for example, if the window is used only as a direct3d display window, so messages will always be sent to a single window.

Mostly I'm just curious, but it can also lead to some aspects of my code being cleaner.

+7
source share
1 answer

You call DispatchMessage so that the message is delivered to the appropriate window, in its "proc window". You think that you have only one window, but is it really the only one? COM will create auxiliary windows, other subsystems can also create auxiliary hidden windows that will deliver messages sent to the general message queue and addressed to these windows. Without thinking about these details, you have an API to send them. And you have to do this because these subsystems rely on the presence of messages.

Spy++ The Windows SDK tool will help you find out how many windows you have.

However, if you really have a single window, it doesn’t really matter if the handler is called from internal DispatchMessage services or directly using your message pump.

+12
source

All Articles