window border width and height in Win32 - how do I get it?

The GetWindowRect and GetClientRect functions can be used calculate the size of all the window borders.

Suite101 has a article on resizing a window and the keeping client area at a know size.

Here is their sample code:

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

I think what you're looking for is SM_CYCAPTION -- that's the height of the title bar. SM_CYBORDER is the height of the horizontal edges of a window.


int border_thickness = GetSystemMetrics(SM_CXSIZEFRAME);

In fact, the above result might be equal to:

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

but GetSystemMetrics(SM_CXSIZEFRAME) is easier to be used.

Tags:

C++

Winapi