Introduction
The Win32 API is the core set of interfaces for developing native Windows applications. This guide will walk you through setting up a development environment, creating your first Win32 program, and understanding the basic concepts.
Prerequisites
- Windows 10 or later
- Visual Studio 2022 (Community or higher)
- C++ development workload installed
Create a New Project
- Open Visual Studio and select Create a new project.
- Search for
Win32 Project
and choose Windows Desktop Application (C++). - Enter a project name, e.g.,
HelloWin32
, and click Create. - In the wizard, select Empty project and click Finish.
First Win32 Program
Replace the generated main.cpp
with the following code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Hello, Win32!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
nullptr,
nullptr,
hInstance,
nullptr
);
if (hwnd == nullptr) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg = { };
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Build and Run
Press Ctrl+Shift+B to build the solution, then F5 to run. A window titled “Hello, Win32!” should appear.
Next Steps
- Explore message handling in Window Functions.
- Learn about common controls in the Controls Overview.
- Read about memory management with Win32 Memory Functions.