Explore a collection of code examples to help you build powerful Windows applications.
Learn the fundamental steps to create a simple window using Win32 API, including message loop handling.
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"MyWindowClass";
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(
0,
L"MyWindowClass",
L"My First Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL
);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Demonstrates how to capture and respond to mouse clicks and movements within your window.
// ... (previous code for window creation) ...
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_LBUTTONDOWN: {
int xPos = LOWORD(lParam);
int yPos = HIWORD(lParam);
MessageBox(hWnd, L"Left mouse button clicked!", L"Mouse Event", MB_OK);
break;
}
case WM_MOUSEMOVE: {
// You can track mouse movement here if needed
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// ... (rest of WinMain function) ...
Shows how to create and manage common Windows controls like buttons and editable text fields.
// In WinMain, after CreateWindowEx:
// Create a Button
HWND hButton = CreateWindowEx(
0, L"BUTTON", L"Click Me",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 10, 100, 30,
hWnd, (HMENU)1, hInstance, NULL
);
// Create an Edit Box
HWND hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
10, 50, 200, 30,
hWnd, (HMENU)2, hInstance, NULL
);
// In WndProc, add a handler for button clicks:
case WM_COMMAND:
if (LOWORD(wParam) == 1) { // ID of the button
MessageBox(hWnd, L"Button clicked!", L"Control Event", MB_OK);
}
break;
// ...
An introduction to performing file operations asynchronously using the Windows API, preventing UI freezes.
#include <windows.h>
#include <iostream>
// ... (Window setup and message loop) ...
void PerformAsyncRead(HWND hWnd) {
HANDLE hFile = CreateFile(
L"mydata.txt", GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
// Handle error
return;
}
OVERLAPPED ol = {0};
char buffer[256];
DWORD bytesRead;
// Associate the file handle with the window for completion notifications
SetLastError(0); // Clear last error
if (!AttachConsole(ATTACH_PARENT_PROCESS) && GetLastError() != ERROR_INVALID_HANDLE) {
// You might want to redirect output to a debug console or log file
}
std::cout << "Attempting async read..." << std::endl;
if (!ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, &ol)) {
DWORD error = GetLastError();
if (error != ERROR_IO_PENDING) {
// Handle other errors
CloseHandle(hFile);
return;
}
// IO is pending, GetOverlappedResult will be used later
std::cout << "IO Pending." << std::endl;
} else {
// IO completed synchronously
buffer[bytesRead] = '\0';
std::cout << "Sync read complete: " << buffer << std::endl;
CloseHandle(hFile);
}
// Note: You would typically handle the completion of pending IO
// via WM_SOCKET message or GetOverlappedResult in a separate thread.
// This example is simplified.
}
// In your WndProc, you might add a handler to trigger this:
case WM_KEYDOWN:
if (wParam == VK_F5) { // Press F5 to trigger async read
PerformAsyncRead(hWnd);
}
break;
// ...
Looking for something specific? Browse the API Reference or try a targeted search.