suppose that I have a class derived from CWnd that have the event handler functions OnPaint
,OnCreate
and OnSize
. As you know all of them are occured when the window starts but I want to see what is the order between them.
when I set a breakpoint in one of them, after ending the function, the control is not passed to another and goes to one of the MFC's built-in .cpp files for example wincore.cpp
?
How can I understand the order? any links or teach me a way to prevent the control from going to MFC built-in classes?
问题:
回答1:
The order in which messages arrive is not fully defined and documented. However, some messages are ordered with respect to others. The MSDN explicitly states that WM_NCCREATE
is sent prior to WM_CREATE
. The documentation for WM_CREATE
has a few more hints as far as the order is concerned (emphasis mine):
Sent when an application requests that a window be created by calling the
CreateWindowEx
orCreateWindow
function. (The message is sent before the function returns.) The window procedure of the new window receives this message after the window is created, but before the window becomes visible.
It should be immediately clear, that a window has to exist to receive additional messages. Consequently, the first WM_SIZE
will be sent after WM_CREATE
. As for WM_PAINT
it requires that a window is visible. Plus, it is a low priority message that is only generated if the message queue is empty.
During window creation the order is WM_CREATE
, WM_SIZE
, WM_PAINT
.
If you are only interested in observing messages you can use a tool like Spy++ (spyxx.exe) that comes with Visual Studio. It can log arbitrary messages and gives you detailed information. If this is for educational purposes (as opposed to deducing behavior) it is a very helpful tool.
Note to downvoters: If you feel this answer is inaccurate or needs improvement please leave a note to help future visitors.