Structuring Win32 GUI Code

I want to improve my code and file structure in large Win32 projects with many windows and controls. Currently, I tend to have one title and one source file for the entire implementation of a window or dialog. This is great for small projects, but now it has come to the point that these implementations begin to reach 1000-2000 lines, which is tedious to view.

A typical source file is as follows:

static LRESULT CALLBACK on_create(const HWND hwnd, WPARAM wp, LPARAM lp) {
    setup_menu(hwnd);
    setup_list(hwnd);
    setup_context_menu(hwnd);

    /* clip */

    return 0;
}

static LRESULT CALLBACK on_notify(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
    const NMHDR* header = (const NMHDR*)lp;

    /* At this point I feel that the control event handlers doesn't
     * necessarily belong in the same source file. Perhaps I could move
     * each control creation code and event handlers into a separate
     * source file? Good practice or cause of confusion? */

    switch (header->idFrom) {
    case IDC_WINDOW_LIST:
        switch (header->code) {
        case NM_RCLICK:
            return on_window_list_right_click(hwnd, wp, lp);

        /* clip */
        }
    }
}

static LRESULT CALLBACK wndmain_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
    switch (msg) {
    case WM_CREATE:
        return on_create(hwnd, wp, lp);

    case WM_CLOSE:
        return on_close(hwnd, wp, lp);

    case WM_NOTIFY:
        return on_notify(hwnd, wp, lp);

    /* It doesn't matter much how the window proc looks as it just forwards
     * events to the appropriate handler. */

    /* clip */

    default:
        return DefWindowProc(hwnd, msg, wp, lp);
    }
}

, , , , , , ..... , , .

Win32, , , , GUI, GUI Win32 , CreateWindowEx, proc .

, , .

!

- , , Win32 API .

, , , .

+5
1

-, crackers windowsx.h; , , .

+5

All Articles