Win32中的窗口边框宽度和高度 - 我如何获得它?

Ste*_*zel 27 c++ winapi

      ::GetSystemMetrics (SM_CYBORDER)

...回来1,我知道标题栏高于一个像素:/

我也尝试过:

     RECT r;
      r.left = r.top = 0;   r.right = r.bottom = 400;
      ::AdjustWindowRect (& r, WS_OVERLAPPED, FALSE);
      _bdW = (uword)(r.right - r.left - 400);
      _bdH = (uword)(r.bottom - r.top - 400);

但边界w,h回归为0.

在我的WM_SIZE处理程序中,我需要确保窗口的高度在"步骤"中变化,因此,例如,一个全新的文本行可以放在窗口中,底部没有"junky partial line space".

但是:: MoveWindow需要添加边框空间的尺寸.

有人必须在此之前完成此任务...感谢您的帮助:)

stu*_*lly 39

GetWindowRectGetClientRect函数可用于计算所有窗口边框的大小.

Suite101有一篇关于调整窗口大小和保持客户区域大小的文章.

这是他们的示例代码:

void ClientResize(HWND hWnd, int nWidth, int nHeight)
{
  RECT rcClient, rcWind;
  POINT ptDiff;
  GetClientRect(hWnd, &rcClient);
  GetWindowRect(hWnd, &rcWind);
  ptDiff.x = (rcWind.right - rcWind.left) - rcClient.right;
  ptDiff.y = (rcWind.bottom - rcWind.top) - rcClient.bottom;
  MoveWindow(hWnd,rcWind.left, rcWind.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
}
Run Code Online (Sandbox Code Playgroud)

  • 有一个WinAPI函数来调整窗口大小以具有给定的客户区大小:AdjustWindowRectEx() (3认同)
  • 我不认为这种方法适用于Windows 10,因为透明的8px边框允许调整窗口大小. (3认同)
  • 我想我会坚持使用GetWindowRect() - GetClientRect()方法.这样我就不必混淆窗口样式了.这与GetSystemMetrics(无论如何)有同样的问题...谢谢大家 - 这个地方摇滚:) (2认同)

小智 12

int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME);
Run Code Online (Sandbox Code Playgroud)

事实上,上述结果等于:

GetClientRect(hWnd, &rcClient); 
GetWindowRect(hWnd, &rcWind); 
int border_thickness = ((rcWind.right - rcWind.left) - rcClient.right) / 2; 
Run Code Online (Sandbox Code Playgroud)

但"GetSystemMetrics(SM_CXSIZEFRAME)"很容易使用.

  • 如果不传入 hWnd,GetSystemMetrics 如何知道您指的是哪个窗口?不同的窗口可以有不同的边框厚度。我认为你的函数检索系统范围的设置。 (2认同)

Hea*_*eek 11

我想你要找的是SM_CYCAPTION- 这就是标题栏的高度.SM_CYBORDER是窗口水平边缘的高度.


小智 5

除非窗口最小化或未完全初始化,否则 Stukelly 建议的方法将起作用。在这些条件下为您提供边框大小的另一种方法是使用该AdjustWindowRectEx函数。这是一个例子:

CSize GetBorderSize(const CWnd& window)
{
   // Determine the border size by asking windows to calculate the window rect
   // required for a client rect with a width and height of 0
   CRect rect;
   AdjustWindowRectEx(&rect, window.GetStyle(), FALSE, window.GetExStyle());
   return rect.Size();
}
Run Code Online (Sandbox Code Playgroud)

根据应用程序的不同,如果需要当前可见边框大小,则可能需要将此方法与 Stukelly 的方法结合起来:

CSize GetBorderSize(const CWnd& window)
{
   if (window.IsZoomed())
   {
      // The total border size is found by subtracting the size of the client rect
      // from the size of the window rect. Note that when the window is zoomed, the
      // borders are hidden, but the title bar is not.
      CRect wndRect, clientRect;
      window.GetWindowRect(&wndRect);
      window.GetClientRect(&clientRect);
      return wndRect.Size() - clientRect.Size();
   }
   else
   {
      // Determine the border size by asking windows to calculate the window rect
      // required for a client rect with a width and height of 0. This method will
      // work before the window is fully initialized and when the window is minimized.
      CRect rect;
      AdjustWindowRectEx(&rect, window.GetStyle(), FALSE, window.GetExStyle());
      return rect.Size();
   }
}
Run Code Online (Sandbox Code Playgroud)