2009-01-10 174 views
27
 
     ::GetSystemMetrics (SM_CYBORDER) 

...回来了1,我知道标题栏比一个像素高:我也尝试过/Win32窗口边框宽度和高度 - 我如何得到它?

 
    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处理程序中,我需要确保窗口的高度 “台阶”变化如此,例如文本的一个全新的行会适应窗口 没有“毫无价值部分行空间”底部。

但::的MoveWindow需要的尺寸与添加的边框空间。

一定有人做过这之前... 感谢所有帮助:)

+0

对于文档`AdjustWindowRect`说,相当无益,你不能使用`WS_OVERLAPPED`。 – JWWalker 2012-02-01 20:31:04

+0

@JWWalker:其实这很有帮助。现在,如果你也知道'WS_OVERLAPPED'被定义为'0x0',它应该很明显,为什么。 – IInspectable 2017-11-15 22:34:39

+0

@IInspectable,不,我不知道为什么WS_OVERLAPPED是0是相关的。 – JWWalker 2017-11-16 16:19:39

回答

38

GetWindowRectGetClientRect函数可以用来计算所有窗口边界的大小。

Suite101在resizing a window and the keeping client area at a know size上有一篇文章。

这里是他们的示例代码:

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); 
} 
9

我想你在找什么是SM_CYCAPTION - 这是标题栏的高度。 SM_CYBORDER是窗口水平边缘的高度。

+0

Hellow。作为我的[screen captrue工具](https://i.stack.imgur.com/Ikgeg.jpg),我知道标题栏的高度是28.但是你的`GetSystemMetrics(SM_CYCAPTION)`只给出`23`。 。 – yode 2017-11-15 09:50:18

2

Head Geek给出了详细的答案:使用GetSystemMetrics来添加标题和边界位。你也可以在GetWindowRect和GetClientRect之间的宽度/高度上做一些改变。这会给你所有的标题/边界/等。

10
int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME); 

实际上,以上的结果是等于:

GetClientRect(hWnd, &rcClient); 
GetWindowRect(hWnd, &rcWind); 
int border_thickness = ((rcWind.right - rcWind.left) - rcClient.right)/2; 

但 “GetSystemMetrics的(SM_CXSIZEFRAME)” 是容易被使用。

2

由stukelly建议的方法将工作,除非窗口最小化或未完全initialzied。在这些条件下给出边界大小的另一种方法是使用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(); 
} 

根据应用的不同,它可能是必要的,如果当前可见的边框尺寸是必要的这一做法与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(); 
    } 
} 
相关问题