Custom Window Class

Windows subsystem defines many built-in classes for creating different windows. Custom window class should be defined by user. User can use WNDCLASS or WNDCLASSEX structure to define a custom class. User can define different attributes of an window class along with the name of the class. This class then should be registered for further use in the program. CreateWindow and CreateWindowEx takes this name as an argument and creates the window handle.

WNDCLASS

WNDCLASS: The WNDCLASS structure contains the window class attributes that are registered by the RegisterClass() function.

typedef struct _WNDCLASS { 
    UINT    style; 
    WNDPROC lpfnWndProc; 
    int     cbClsExtra; 
    int     cbWndExtra; 
    HANDLE  hInstance; 
    HICON   hIcon; 
    HCURSOR hCursor; 
    HBRUSH  hbrBackground; 
    LPCTSTR lpszMenuName; 
    LPCTSTR lpszClassName; 
} WNDCLASS;

WNDCLASSEX

WNDCLASSEX: The WNDCLASSEX structure contains the window class attributes that are registered by the RegisterClassEx() function.

typedef struct _WNDCLASSEX {
  UINT      cbSize;
  UINT      style;
  WNDPROC   lpfnWndProc;
  int       cbClsExtra;
  int       cbWndExtra;
  HINSTANCE hInstance;
  HICON     hIcon;
  HCURSOR   hCursor;
  HBRUSH    hbrBackground;
  LPCSTR    lpszMenuName;
  LPCSTR    lpszClassName;
  HICON     hIconSm;
} WNDCLASSEX, *PWNDCLASSEX;

RegisterClass

RegisterClass: The RegisterClass() function registers a window class for subsequent use in calls to the CreateWindow() or CreateWindowEx() function.

ATOM RegisterClass(
  CONST WNDCLASS *lpWndClass   /* address of structure with class // data );

RegisterClassEx

RegisterClassEx: The RegisterClassEx() function registers a window class for subsequent use in calls to the CreateWindow() or CreateWindowEx() function.

ATOM RegisterClass(
  CONST WNDCLASSEX *lpWndClass   /* address of structure with class // data );

CreateWindow

CreateWindow: The CreateWindow function takes the name of the class which is registered by the RegisterClass function.

void CreateWindow(
   lpClassName,
   lpWindowName,
   dwStyle,
   x,
   y,
   nWidth,
   nHeight,
   hWndParent,
   hMenu,
   hInstance,
   lpParam
);

CreateWindowEx

CreateWindowEx: The CreateWindowEx function takes the name of the class which is registered by the RegisterClass/RegisterClassEx function. This function is primarily used for the extended window class with extended styles etc.

HWND CreateWindowEx(
  DWORD     dwExStyle,
  LPCSTR    lpClassName,
  LPCSTR    lpWindowName,
  DWORD     dwStyle,
  int       X,
  int       Y,
  int       nWidth,
  int       nHeight,
  HWND      hWndParent,
  HMENU     hMenu,
  HINSTANCE hInstance,
  LPVOID    lpParam
);

Example Code:

#include <windows.h>


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  WNDCLASS wc;
  MSG msg;
  HWND hWnd;
  ZeroMemory(&wc, sizeof(WNDCLASS));

  wc.style           = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc     = (WNDPROC)WndProc;
  wc.hInstance       = hInstance;
  wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
  wc.lpszClassName   = (LPCTSTR)"MyWin32Class";


  RegisterClass(&wc);

   hWnd = CreateWindow("MyWin32Class", "Hello World!", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  /*Place your code here*/
  return 0;
}

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#