【问题标题】:How to get the default caption bar height of a window in Windows?如何在 Windows 中获取窗口的默认标题栏高度?
【发布时间】:2015-04-15 23:06:09
【问题描述】:

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

那么我怎样才能在 Windows 中获得重叠窗口的默认标题栏高度?

【问题讨论】:

    标签: windows winapi win32gui


    【解决方案1】:

    从 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。

    【讨论】:

    • 测量不同 DPI 缩放设置下的实际标题栏大小,似乎没有我预期的那么简单(例如乘以 DPI/96.0f)。我已经到了:std::ceil( ((GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)) * dpi_scale) + GetSystemMetrics(SM_CXPADDEDBORDER) )
    • GetSystemMetricsForDpi
    【解决方案2】:

    一种解决方案是使用AdjustWindowRectEx function,它还计算其他窗口边框宽度,并允许窗口样式变化:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-25
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2014-08-24
      • 2011-05-15
      • 2019-08-30
      相关资源
      最近更新 更多