Win32: Setting the Properties of a Window

Setting the Properties of a Window

In this Win32 C++ video tutorial, we look at the parts of our original Win32 lesson 1 code and discuss the portions of the program that are used set the properties of the window. Basically, the window class is set inside of the MyRegisterClass() function where it is also registered for later use. In the InitInstance() function the only window in the application is created with a call to CreateWindow().

Below, we have the code for the function MyRegisterClass(). As you can see, the function consists of a series of statements, which set the data members of WNDCLASSEX data type. The type WNDCLASSEX can be used to define a window class via a call to RegisterClassEx(), which we perform at the end of the function.

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style      = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon      = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32LESSON1));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_MENUTEXT);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32LESSON1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

Each data member of WNDCLASSEX is assigned a value in this function. These members allow us to set various properties of the window class: the icons, the color of the background in the client area, the cursor that is used when it enters the client area, the menu for the window, the style, and so on. Once the window class is registered, we can make windows of this type by calling CreateWindow(), as we do in the InitInstance() function below.

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

The InitInstance() function performs initialization for our application, which includes creating our window via a call to CreateWindow(). The CreateWindow() function takes several parameters, which tell the function what kind of window should be created. The first parameter is the name of a window class type that was registered. This gives the basic properties of the window.

After this, are that parameters that adjust the window's appearence and functionality: the title that will appear in the title bar, the style, the x and y coordinates of the position of the upper-left hand corner, the width and height of the window, a handle to the parent window, a handle to an alternative menu, a handle to the module, and a void pointer that points to a CREATESTRUCT.