WinAPI: Create a window with a specified client area size

25,025

You can use the AdjustWindowRect or AdjustWindowRectEx function to calculate the window size given a desired client area size.

Share:
25,025
GeReV
Author by

GeReV

I am a full-stack developer currently working at WeWork. I have previously worked at Onavo, Facebook and Life on Air on Meerkat and Houseparty. Have a look at my GitHub repository.

Updated on October 18, 2020

Comments

  • GeReV
    GeReV over 3 years

    I was wondering how can I create a window using Win32 API with a specific client area size.

    When trying to create a window using the following piece of code, the entire window is 640x480, with the window's chrome taking some of the client area:

    HWND       hWnd;
    WNDCLASSEX WndClsEx;
    ZeroMemory(&WndClsEx, sizeof(WNDCLASSEX));
    
    WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc   = DefWindowProc;
    WndClsEx.cbClsExtra    = 0;
    WndClsEx.cbWndExtra    = 0;
    WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClsEx.lpszMenuName  = NULL;
    WndClsEx.lpszClassName = TEXT("Title");
    WndClsEx.hInstance     = hInstance;
    WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
    RegisterClassEx(&WndClsEx);
    
    hWnd = CreateWindowEx(  NULL,
                TEXT("Title"),
                TEXT("Title"),
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                640,
                480,
                NULL,
                NULL,
                hInstance,
                NULL);
    

    Assuming simple math won't quite solve the problem, how do I take the chrome size into account?

    Note: I'm using SDL after creating the window, but I'm guessing it's bound to the window size and makes no difference to its size.

  • engf-010
    engf-010 over 13 years
    Correction ,it calculates the coordinates of the Window-Area based on the coordinates of the Client-Area.
  • zwcloud
    zwcloud about 7 years
    A related answer on why AdjustWindowRect cannot be used with WS_OVERLAPPED.