Introduction to MFC - Windows and Event Handling
(Page 2 of 5 )
If your to answer the question of how Windows manages to control all the applications at once is "event handling," then you are on the right path and surely deserve the gold medal. However, there is another good response: message cycles. Windows monitors all user activities like the mouse, keyboard, and so forth. When a message (event) occurs that affects one of the programs, a message is sent to it. For example, if you push down the left click, then Windows notifies the application by dispatching a WM_LBUTTONDOWN message towards it. You get the picture, right?
Once this is completed, it is up to the software to decide what it should do with the received message. The decision is made in accordance with the message cycle in its interior. There are two options: first it may decide that an interior handling is necessary and call for the function assigned to take care of the respective message. Then the function executes all the code that we previously wrote in it.
The second option is to ignore the message, as in the case of the absence of a function assigned to it. Either way, a result will be sent back to Windows and the OS can take the next step. It will take care of future trouble according its own message map.
The power of MFC lies in the encapsulation of the message handling. An MFC application does the same thing that a Win32 (32-bit API for modern versions of Windows) application does. The distinction is that, while coding under Win32 demands its own message handling (and all the trouble that this implies), the developer under MFC doesn’t implicate itself in this; s/he can sit back and concentrate on pure code writing. All s/he needs to do is declare a message map through which informing the MFC system is possible, describing the way it should allocate the messages. The interior system of the MFC will take care of the rest.
This is indeed a great step forward, but wait… it’s even simpler. And some truly powerful extra instruments were created under Visual Studio that tend to make programming under MFC effortless. These are the AppWizard (for creating a new application) and the Properties Window (in the case of Visual Studio prior to the .NET platform, it is known as the ClassWizard – for handling the message map).
Next: The First MFC Program and the Basics >>
More BrainDump Articles
More By Gabor Bernat