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

What is WinMain function in windows C? Main vs WinMain, WinMain prototype and arguments.
Describes What is WinMain()? Entry point funtion in Windows exe. Function prototype. Implementation, Command line argument passing. Show Window flag.

Steps to write a basic window program with C and Win32 APIs.
Include windows.h, Define WinMain, Define WNDCLASS attributes, RegisterClass, CreateWindow, ShowWindow, UpdateWindow, GetMessage, DispatchMessage, DefWindowProc

What are LPARAM and WPARAM in window proc function ? How Win32 subsystem pass parameters to this function?
Explains the parameters WPARAM and LPARAM and in window procedure function. What is this W and L stands for? How Win32 subsystem pass parameters?

What is WM_PAINT event ? When does Win32 subsystem call WM_PAINT event?
All you wanted to know Drawing in Windows, Paint Event WM_PAINT, Device Context, BeginPaint EndPaint GDI Objects, Pen, Brush, Font, Bitmap, Palette, Region.

#