C + + Builder as a RAD tool for program development, its new visual programming environment, component-oriented development model will undoubtedly greatly improve the programming efficiency. It encapsulates the intricacies of Windows messages and APIs, and programmers in most cases ignore the details of Windows messages as long as they focus on the component's event-handling functions. However, after all, the Windows operating system is a message-driven system, running its application, naturally can not out of the system, so master and use message processing, some problems can be handled with a multiplier effect.
Although C + + Builder's VCL controls encapsulate most commonly used messages, the event-handling capabilities provided by C + + Builder are quite complete, but when handling C + + Builder undefined Windows messages or custom messages, master C + + Builder Internal message processing mechanism is still very necessary. The following, starting from the Windows operating system message-driven mechanism, discusses the encapsulation, delivery and processing mechanism of messages in the VCL control of C + + builder, and finally, the application instance of the new message processing process as the verification and practice of the content.
Windows message-driven mechanism
Windows is a message-driven operating system, and Windows messages provide the means to communicate between applications and applications and between applications and Windows systems.
There is a system message queue in Windows, and for each executing Windows application, the system establishes a message queue, an application queue, that holds messages for the various windows that the program might create. The application contains a piece of code called a "message loop" that is used to retrieve the messages from the message queue and distribute them to the appropriate window functions.
The message loop code is a program segment similar to the following in the main function WinMain () in the application:
while(GetMessage(&&msg,NULL,NULL,NULL))
{ //从消息队列中取得消息
TranslateMessage(&&msg);
//检索并生成字符消息WM_CHAR
DispatchMessage(&&msg);
//将消息发送给相应的窗口函数
}
This shows that the so-called "message cycle", is actually a program cycle.