THE STRUCTURE OF A WINDOWS PROGRAM

For a minimal Windows program that just uses the Windows API, you will write two functions. These are a WinMain() function, where execution of the program begins and basic program initialization is carried out, and a WindowProc() function that is called by Windows to pass messages to the application. The WindowProc() part of a Windows program is usually the larger portion because this is where most of the application-specific code is, responding to messages caused by user input of one kind or another.

Although these two functions make up a complete program, they are not directly connected. WinMain() does not call WindowProc(), Windows does. Windows also calls WinMain(). This is illustrated in Figure 11-2.

The WinMain() function communicates with Windows by calling Windows API functions. The same applies to WindowProc(). The integrating factor in your Windows desktop application is Windows itself, which links to both WinMain() and WindowProc(). After looking into what the pieces are that make up WinMain() and WindowProc() you will assemble them into a working example of a simple Windows program.

The WinMain() Function

The WinMain() function is the equivalent of main() in a console program. It’s where execution starts and where the basic initialization for the program is carried out. To allow Windows to pass data to it, WinMain()

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.