Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is calling DispatchMessage in win32 programs necessary?

Tags:

windows

winapi

Win32 programs generally have a message loop that loops calling GetMessage or PeekMessage, and then calls DispatchMessage to dispatch the message to the window proceedure of the relevant window.

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

Mostly I'm just curious but also it might lead to certain aspects of my code being cleaner too.

like image 663
jcoder Avatar asked Sep 05 '12 13:09

jcoder


People also ask

What does DispatchMessage do?

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.

What is the difference between GetMessage and DispatchMessage function?

GetMessage pulls the WM_LBUTTONDOWN message from the queue and fills in the MSG structure. Your program calls the TranslateMessage and DispatchMessage functions. Inside DispatchMessage, the operating system calls your window procedure. Your window procedure can either respond to the message or ignore it.

What is message loop in windows programming?

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows.

What is TranslateMessage?

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.


1 Answers

You call DispatchMessage to have the message delivered to proper window, to its "window proc". You think you have one window only, but is it really the only one? COM will create helper windows, other subsystems might create helper hidden windows as well, who is going to deliver messages posted to shared message queue and addressed to those windows. Without having to think a lot about these details you have API to dispatch them. And you have to do it because those subsystems are relying on presence of message pump.

Spy++ Windows SDK tool might help you with seeing how many windows you really have.

Still if you indeed have the only window, it does not make much of a difference whether you handler is called from DispatchMessage internals, or directly by your message pump.

like image 137
Roman R. Avatar answered Oct 16 '22 17:10

Roman R.