如何获得Windows中窗口的默认标题栏高度?

问题描述 投票:4回答:2

我正在开发一个应用程序,该应用程序采用了自绘标题栏,需要模仿系统默认的标题栏。

那么,我如何在Windows中获得一个被覆盖的窗口的默认标题栏高度?

windows winapi win32gui
2个回答
8
投票

源代码移植自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:高度是依赖于dpi的。


1
投票

一个解决方案是使用 AdjustWindowRectEx函数 它还可以计算其他窗口边框的宽度,并允许窗口风格的变化。

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

对于现代的Windows(10+),还有DPI感知版本。

// 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
© www.soinside.com 2019 - 2024. All rights reserved.