Skip to content

Instantly share code, notes, and snippets.

@skejeton
Created August 10, 2024 12:23
Show Gist options
  • Save skejeton/7e6bea1854ffcd87bc6aa614eb1bb00b to your computer and use it in GitHub Desktop.
Save skejeton/7e6bea1854ffcd87bc6aa614eb1bb00b to your computer and use it in GitHub Desktop.
// clang-format off
// Compile the program linking user32.lib
#pragma comment(lib, "user32.lib")
#include <Windows.h>
// Event handler.
LRESULT CALLBACK WndProc(
HWND hWnd, // Window the event was assigned to.
UINT Msg, // Message type.
WPARAM wParam, // 1st parameter to the message passed by Windows.
LPARAM lParam // 2nd parameter to the message passed by Windows.
)
{
switch (Msg) {
case WM_KEYDOWN: // On key down.
if (wParam == VK_ESCAPE) {
PostQuitMessage(0);
}
return 0;
case WM_COMMAND: // When we interact with some sort of control, like a button.
UINT Command = HIWORD(wParam);
if (Command == BN_CLICKED) {
MessageBox(hWnd, "Ow.", "Button", MB_ICONWARNING);
}
return 0;
case WM_DESTROY: // On window destroy.
PostQuitMessage(0);
return 0;
}
// Let Windows handle the rest.
return DefWindowProcA(hWnd, Msg, wParam, lParam);
}
// Entry point.
int APIENTRY WinMain(
HINSTANCE hInstance, // Current .exe
HINSTANCE hPrevInstance, // Parent .exe (explorer, cmd, &c.)
LPSTR lpCmdLine, // Command line parmeters all as a single string.
int nCmdShow // Whether to show/hide windows we create.
)
{
// Window classes are to identify types of windows.
WNDCLASSA WindowClass = { 0 };
WindowClass.lpszClassName = "YourWindowClassName";
WindowClass.lpfnWndProc = WndProc;
if (!RegisterClassA(&WindowClass)) {
MessageBoxA(NULL, "Couldn't register window class", "Windows app", MB_ICONERROR);
}
// Create the window.
HWND Window = CreateWindowA(
WindowClass.lpszClassName,
"Your title",
WS_OVERLAPPEDWINDOW, // How window appears. This value will show default window appearance.
CW_USEDEFAULT, CW_USEDEFAULT, // X and Y, CW_USEDEFAULT will put the window wherever the system chooses.
800, 600, // Window size.
NULL, // Parent window, we have none.
NULL, // Menu, we have none (this is also a bit outdated nowadays).
hInstance, // Current .exe instance.
NULL // Your custom parameter to WndProc upon creation.
);
if (Window == NULL) {
MessageBoxA(NULL, "Couldn't create a window.", "Windows app", MB_ICONERROR);
}
ShowWindow(Window, nCmdShow);
// Create a button (Yes, those are also Windows =D).
UINT ButtonStyle = WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON;
HWND Button = CreateWindowA(
"BUTTON",
"Dont click me!!",
ButtonStyle,
10, 10, // X and Y.
100, 20, // Size.
Window, // Parent window.
NULL, // Not used here.
hInstance, // Current .exe instance.
NULL // Your custom parameter to WndProc upon creation.
);
if (Button == NULL) {
MessageBoxA(NULL, "Couldn't create a button.", "Windows app", MB_ICONERROR);
}
// Poll for events.
// This will block the thread until a message is received, such as moving the mouse.
MSG Msg = { 0 };
while (GetMessageA(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg); // Translates key messages to character output.
DispatchMessage(&Msg); // Runs our WndProc function!
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment