The Win32 WinMain Function

Writing programs for Win32, using WinMain as the entry point.

© Guy Lecky-Thompson

Every Windows program has to have a WinMain function, but you will probably only ever need one. Each Win32 project can use a cut'n'pasted version of this skeleton.

Introduction

Before reading this article, the reader should use the following article as a starting point - The Win32 Programming Paradigm. Once the paradigm is understood, it makes the programming that much easier.

The WinMain Function

The WinMain function is the entry point, and is similar to the main function used in regular C programming. Unlike console (command line) programming, it has to explicity register the applications look and feel with Windows, and provide a message loop for processing incoming messages.

If this sounds a little daunting, then it is worth remembering that Windows process much of the specifics itself - the WinMain function simply provides a starting point. As such, the following skeleton can be re-used as many times as necessary, only adjusting, where necessary, some of the values from project to project.

  int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpszCmdLine, int nCmdShow)
   {
     static char szAppName [] = "APP_NAME" ;
     MSG msg;
     HWND hwnd ;
     WNDCLASS wndclass ;
     if (!hPrevInstance)
     {
       wndclass.style = CS_HREDRAW | CS_VREDRAW ;
       wndclass.lpfnWndProc = WndProc ;
       wndclass.cbClsExtra = 0 ;
       wndclass.cbWndExtra = 0 ;
       wndclass.hInstance = hInstance ;
       wndclass.hIcon = LoadIcon (hInstance, IDI_APPLICATION) ;
       wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
       wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
       wndclass.lpszMenuName = "AppMenu";
       wndclass.lpszClassName = szAppName ;
       RegisterClass (&wndclass) ;
     }
     hwnd = CreateWindow (szAppName, "App Title",
       WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, CW_USEDEFAULT,
       CW_USEDEFAULT, CW_USEDEFAULT,
       NULL, NULL, hInstance, NULL) ;
     ShowWindow (hwnd, nCmdShow) ;
     UpdateWindow (hwnd);
     while (GetMessage (&msg, NULL, 0, 0))
     {
       TranslateMessage (&msg) ;
       DispatchMessage (&msg) ;
     }
     return msg.wParam ;
   }

Most of the customisation will take place in the wndclass variable. This gives the various attributes associated with the application, from the cursor and icon, through to the color used to paint the background. After all the parameters (which should be failry self-explanatory from the names) have been set, the class is registered with a call to RegisterClass.

This enables Windows to re-use the definition if another copy of the class appears in the system. To run several instances at the same time in this way, and keep them logically seperate, the hInstance variable is used. The WndProc referred to is the function that actually processes the users interaction with the application. This needs to be provided by the programmer as well, and will be covered in a later article.

CreateWindow and the Message Loop

All that the CreateWindow function does is set up the window, in the correct place and at the correct size, and give it a title in the title bar. Every piece of the Windows GUI (including buttons, text boxes, etc.) will be created with a call to CreateWindow. Once it is set up, the messages associated with it have to be processed.

This is done in the message loop. The while loop simply takes messages from the queue, and returns out of the function when there are nonen left. TranslateMessage turns key codes into real virtual keys, and DispatchMessage sends the message to WndProc for processing.

Summary

Feel free to cut and paste the above WinMain function in its entirety. Do not forget, however, to change the szAppName and szAppTitle variables, otherwise the result will not be quite as intended. Other values are also open to customisation, including:

Also, if a menu is required, the lpszMenuName member of the WNDCLASS must be set to the same value as that used in the resource file that accompanies the application code. If a resource file is not included in the project, then the Borland compiler will simply ignore the lpszMenuName member, and build a menu-less (resource-less) application.

For more details on building an application with the free Borland compiler and tools, please refer to the Borland Win32 Compiler Guide.


The copyright of the article The Win32 WinMain Function in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish The Win32 WinMain Function must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo