HomeC&C++WainTutorialsSamplesTip&TrickTools


First we must create a simple windows dialog application. It is a dialog with a button and a text showing a number, each time you push the button, the number will be increased.
First our main function, which for Windows applications is called WinMain.
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE,
                   LPSTR lpCmdLine,
                   INT nCmdShow)
{
   InstanceHandle = hInstance;
   // Most of the code is entered here ...
   return 0;
}
First we must create a wnd-class, which is used to setup some of the attributes for the window. All windows has an wnd-class, but several windows kan share the same wnd-class. To initialize the wnd-class:
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = DialogProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "WhateverClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if(!RegisterClass(&wc))
   return FALSE;
Notice that a wnd-class is not a C++ class, it's a structure.
Here the string "WhateverClass" is important, we will use it later to identify our wnd-class.
DialogProc is the name of the applications dispatcher function, we will come back to that later.

Now we can create the window:
HWND WindowHandle =
   CreateWindow("WhateverClass",
                "Whatever",
                 WS_MINIMIZEBOX | WS_VISIBLE |
                    WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
                    WS_MAXIMIZEBOX | WS_CAPTION |
                    WS_BORDER | WS_SYSMENU,
                 100, 100, 150, 100,
                 NULL,
                 NULL,
                 InstanceHandle,
                 0);
The first parameter is the name of the wnd-class, from above.
The second parameter is the title of the dialog-box.
The WS_xxx is the style of the window.
The next four is the position.

Now we must enter a message-loop, which for a dialog could look like this:
MSG Msg;
while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
{
   if(!IsDialogMessage(WindowHandle, &Msg))
   {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }
}
This loop will send the windows messages to the right destination, handle some of the messages, and call our dispatcher function, which in this case could be:
LRESULT CALLBACK DialogProc(HWND hwndDlg,
                            UINT msg,
                            WPARAM wParam,
                            LPARAM lParam)
{
   switch(msg)
   {
   case WM_CREATE:
      CreateWindow("STATIC",
                   "0",
                   WS_CHILD | WS_VISIBLE | SS_LEFT,
                   5, 5, 100, 20,
                   hwndDlg,
                   (HMENU)(IDC_STATIC_1),
                   InstanceHandle,
                   0);

      CreateWindow("BUTTON",
                   "Push Me",
                   WS_CHILD | WS_VISIBLE,
                   5, 30, 100, 30,
                   hwndDlg,
                   (HMENU)(IDC_BUTTON1),
                   InstanceHandle,
                   0);
      break;
   case WM_COMMAND:
      if(HIWORD(wParam) == BN_CLICKED &&
         LOWORD(wParam) == IDC_BUTTON1)
      {
         NumPush++;
         SetDlgItemInt(hwndDlg, IDC_STATIC_1, NumPush, TRUE);
      }
      break;
   }
   return DefWindowProc(hwndDlg, msg, wParam, lParam);
}
This function will be called for most dialog messages, we just handle two and let DefWindowProc handle the rest.

We handle WM_CREATE which is an event generated by windows, when the window is created, but before the dialog is shown, we use this event to create the two controls, the text and the button.

The other event we capture is the event generated when the user push the button. It is send thru a WM_COMMAND message, as a lot of other messages, so we must use the parameters to know that evet is received because of a button push, and that it is our button.
If this is the case, we update the text on the label.

The only thing we need now is a #include, some defines, and global variables:
#include <windows.h>

#define IDC_BUTTON1  1200
#define IDC_STATIC_1 1201

HINSTANCE InstanceHandle;
int NumPush = 0;
The complete code for the application can be found here