Just curious to see who has memorized the 70 or so lines to create a window, I for one am almost to the point of having it totally memorized, and I also know what it all means, thats obviously important :P

Dani AI

Generated

Quick primer that builds on what , and brought up: memorizing the boilerplate is fine for muscle memory, but the real value is understanding the flow — registering a class, writing a WindowProc, creating/showing the window, and handling the message loop. Knowing that lets you debug problems when the compiler-generated code or a framework hides a detail.

The minimal flow, in order:

  1. define a WNDCLASSEX and lpfnWndProc, call RegisterClassEx;
  2. call CreateWindowEx to make the HWND;
  3. call ShowWindow/UpdateWindow;
  4. run the GetMessage/TranslateMessage/DispatchMessage loop;
  5. respond to messages in WndProc (e.g. WM_PAINT, WM_DESTROY).

A tiny skeleton you can paste and compile:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_DESTROY) { PostQuitMessage(0); return 0; }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
    WNDCLASSEX wc = {0}; wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = "MyWindowClass";
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

    if (!RegisterClassEx(&wc)) return -1;
    HWND hWnd = CreateWindowEx(0, wc.lpszClassName, "Minimal Win32", WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
    if (!hWnd) return -1;
    ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
    return (int)msg.wParam;
}

Common gotchas and quick fixes: set wc.cbSize, check RegisterClassEx/CreateWindowEx return values and call GetLastError when they fail, remember ShowWindow/UpdateWindow, and handle WM_PAINT for custom drawing. Link with user32.lib (or -luser32 for MinGW). Authoritative references: RegisterClassEx, CreateWindowEx and Window Procedures / message handling. If you prefer letting an IDE or framework generate scaffolding (as does), learn the scaffolding enough to debug it — that’s where understanding wins over pure memorization.

Recommended Answers

All 3 Replies

It's not exactly hard to memorize the code to make a window :P What's hard is being able to make the window do something interesting or useful.

I didn't bother to memorize them -- let VC compiler generate them. Although I admit I would use MFC wxWidgets instead of pure win32 api.

>let VC compiler generate them.
I don't like the code that's generated, I generally like to start everything from scratch. I like knowing I could still make an entire windows program with nothing more than notepad and a compiler :icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.