The Mystery of the Modal Dialog Box Message Loop

2010年7月27日
When you’re programming a Windows application, you use messages. When you launch a modal dialog box when you do such programming, in a Window procedure, like below:
 
WM_COMMAND:
DialogBox(…);
 
After popping up the dialog box, you can still process messages in your original Window procedure, such as WM_TIMER. Don’t you find it strange? The Windows UI thread is only a single thread, so the window messages and dialog messages are processed in the same thread. Then, how does the system do it?
 
The mystery lies in the DialogBox function. The answer is it has a message loop of its own. In the message loop, it checks IsDialogMessage() of the message and if it is, the message is dispatched directly by IsDialogMessage() to the dialog and if not, the message is dispatched back to the window. The stack is not cleared until the the dialog box is closed and destroyed.
 
This, is the mystery of the modal dialog box.

留下您的评论