Create Window with Desired Client Size with AdjustWindowRect

I find it ridiculous that it’s so hard to find any code snippet for this simple demand so here I’m going to write one. This code does one simple job: create a window with some specific client size.

const auto style = WS_OVERLAPPEDWINDOW;
const auto style_ex = 0;

RECT rect = { };
rect.right = /* your window width */;
rect.bottom = /* your window height */;
AdjustWindowRectEx(&rect, style, FALSE, style_ex);

hwnd = CreateWindowEx(
    style_ex,
    L"YOUR WINDOW CLASS",
    L"YOUR WINDOW TITLE",
    style,

    // Size and position
    CW_USEDEFAULT, CW_USEDEFAULT,
    rect.right - rect.left, // Perform a subtraction to get the real size
    rect.bottom - rect.top,

    NULL,       	// Parent window
    NULL,       	// Menu
    hInstance,  	// Instance handle
    NULL        	// Additional application data
);
Yukino

Leave a Reply

Your email address will not be published. Required fields are marked *