Window Functions (Win32 API)
Overview
The Win32 Window functions enable creation, management, and interaction with top‑level and child windows. These functions are fundamental for building graphical user interfaces on Windows.
Key Functions
| Function | Header | Purpose |
|---|---|---|
CreateWindowEx | winuser.h | Creates an overlapped, pop‑up, or child window with extended styles. |
DestroyWindow | winuser.h | Destroys the specified window. |
ShowWindow | winuser.h | Sets the window's show state. |
UpdateWindow | winuser.h | Updates the client area of the specified window. |
SetWindowPos | winuser.h | Changes the size, position, and Z order of a window. |
GetWindowLongPtr | winuser.h | Retrieves information about the specified window. |
SetWindowLongPtr | winuser.h | Changes an attribute of the specified window. |
DefWindowProc | winuser.h | Default processing for window messages. |
Example: Creating a Simple Window
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"SampleWindowClass";
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Hello, Win32!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
NULL,
NULL,
hInst,
NULL
);
if (hwnd == NULL) return 0;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}