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

前端 未结 6 917
慢半拍i
慢半拍i 2020-12-08 10:10
      ::GetSystemMetrics (SM_CYBORDER)

...comes back with 1 and I know the title bar is taller than ONE pixel :/

I also tried:

     RECT r;
         


        
相关标签:
6条回答
  • 2020-12-08 10:31
    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.

    0 讨论(0)
  • 2020-12-08 10:32

    Head Geek gives the detailed answer: use GetSystemMetrics to add up the caption and border bits. You can also do a difference on width/height between the GetWindowRect and GetClientRect. This will give you the total of all captions/borders/etc.

    0 讨论(0)
  • 2020-12-08 10:40

    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.

    0 讨论(0)
  • 2020-12-08 10:40

    You have another solution... You can pre calculate the border by calling a dedicated function while in WM_CREATE and WM_INITDIALOG messages. And refresh the values when you change your window style or when menu split in two rows.

    RECT cRect, wRect, oRect;
    GetWindowRect(hWnd, &wRect);
    GetClientRect(hWnd, &cRect);
    MapWindowPoints(hWnd, NULL, (LPPOINT)&cRect, 2);
    
    oRect.left = cRect.left - wRect.left;
    oRect.top = cRect.top - wRect.top;
    oRect.right = wRect.right - cRect.right;
    oRect.bottom = wRect.bottom - cRect.bottom;
    
    0 讨论(0)
  • 2020-12-08 10:41

    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);
    }
    
    0 讨论(0)
  • 2020-12-08 10:44

    The method suggested by stukelly will work unless the window is minimized or not fully initialzied. An alternate approach that will give you the border size in these conditions is to use the AdjustWindowRectEx function. Here is an example:

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

    Depending on the application, it may be necessary to combine this approach with stukelly's if the current visible border size is necessary:

    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();
       }
    }
    
    0 讨论(0)
提交回复
热议问题