In creating 开发者_如何学编程a window:
1) Why should we register the window class and how does CreateWindow
understand our desired class?
WNDCLASS wc;
RegisterClass(&wc)
2) Why should we use this LOOP:
MSG msg;
While(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispathMessage(&msg);
}
At start there is no messsage, so we won’t enter the loop and we will exit from the WinMain
function.
You register the window class because that's how the system works. Think of it as defining a type. GetMessage blocks until a message arrives in the queue.
You really need to read Charles Petzold's book, Programming Windows
In principle when your program starts up you tell Windows that your window/program is ready to accept messages - that is registering it. The message loop thereafter is where the interaction with the user (and system) happens. Every time some interaction is needed it is communicated to the application via a message (or event). In your message loop you define behavior how to react on the different messages.
Besides the UI messages there are other messages like timer event or messages system related messages. These can be handled as well.
You are making a wrong assumption about GetMessage
. You state that if there's no message, "we won’t enter the loop". That means you assume GetMessage
returns false if there's no message.
In fact, GetMessage
returns false only when there's a WM_QUIT
message. If there is no message, GetMessage
does not return. It just waits until a message does arrive. This makes a lot of sense for many programs. If there's no input, there's nothing to do and your program doesn't need CPU time.
Some programs periodically need some CPU time. That's why they use WM_TIMER
: so that GetMessage
returns control to your code.
精彩评论