Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the default caption bar height of a window in Windows?

I am developing an application which employs a self-drawn titlebar, which needs to mimic the system default titlebar.

So how could I get the default titlebar height of an overloapped window in Windows?

like image 445
Jichao Avatar asked Dec 25 '22 22:12

Jichao


2 Answers

One solution is to use the AdjustWindowRectEx function which also computes other window borders width, and allows for window style variations:

RECT rcFrame = { 0 };
AdjustWindowRectEx(&rcFrame, WS_OVERLAPPEDWINDOW, FALSE, 0);
// abs(rcFrame.top) will contain the caption bar height

And for modern Windows (10+), there is the DPI-aware version:

// get DPI from somewhere, for example from the GetDpiForWindow function
const UINT dpi = GetDpiForWindow(myHwnd);
...
RECT rcFrame = { 0 };
AdjustWindowRectExForDpi(&rcFrame, WS_OVERLAPPEDWINDOW, FALSE, 0, dpi);
// abs(rcFrame.top) will contain the caption bar height
like image 37
Simon Mourier Avatar answered Dec 28 '22 10:12

Simon Mourier


Source code ported from Firefox:

// mCaptionHeight is the default size of the NC area at
// the top of the window. If the window has a caption,
// the size is calculated as the sum of:
//      SM_CYFRAME        - The thickness of the sizing border
//                          around a resizable window
//      SM_CXPADDEDBORDER - The amount of border padding
//                          for captioned windows
//      SM_CYCAPTION      - The height of the caption area
//
// If the window does not have a caption, mCaptionHeight will be equal to
// `GetSystemMetrics(SM_CYFRAME)`
int height = (GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION) +
    GetSystemMetrics(SM_CXPADDEDBORDER));
return height;

PS: the height is dpi-dependent.

like image 142
Jichao Avatar answered Dec 28 '22 09:12

Jichao