No announcement yet.

GetWindowRect GetClientRect - Menues, Toolbars, etc.

  • Filter
  • Time
  • Show
Clear All
new posts

  • GetWindowRect GetClientRect - Menues, Toolbars, etc.

    For the moment I am excluding the differences between DDT "Dialog Units" and SDK "Pixels", but more to the core point a basic reference. If I understand correctly then from docs
    The GetWindowRect function retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
    The GetClientRect function retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
    To me that would mean that if I have a Dialog/Window, then I would use GetWindowRect to get size position etc....and anything contained in it would be GetClientRect (buttons, menues, toolbars, statusbars, etc...)

    By my rough guesstimation, my position and sizes are off by roughly the size of my Menu, and/or my Titlebar. So I wonder if there are ways of gathering this information? or if I am mis-guessing on who belongs to what Dialog/Window?

    Currently I am using SDK so that I can deal in Pixels only (less conversion rounding errors) but since the change to the new forums I can not find the code explaining the lil nuances of each so hoped someone could explain or point me to the correct links?
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Hi Cliff,

    This MSDN page describes the different parts of a window. You'll see that some of the bits you think of as "client" are actually "nonclient", which should explain the discrepancy you're seeing.




    • #3
      GetWindowRect returns coordinates based on the entire screen.

      GetClientRect returns relative coordinates based on the client area of a window. The top/left is always 0,0 no matter where the window is on the screen. The client area is the area inside the borders of a window and does not include the following:

      - borders (whether flat or 3D)
      - scrollbars of the window
      - caption bar of a window
      - menu of a window (ie. dialog)

      You need to examine the API's you use to find out what coordinates they expect. Some may expect screen coordinates and others client coordinates.

      There are two important API functions all PB'ers should familiarize thenselves with:


      These two functions not only allow you to convert between the two coordinate systems (screen and client), but they also can be used together to convert between the client coordinates of one window and the client coordinates of another window. You convert the first client coordinates to screen coordinates and then convert the screen coordinates back to client coordinates, but for another window.

      The area of a window outside the client area, is called the non-client area. Thats why some window messages start with NC (after the WM_ part) such as WM_NCLBUTTONDOWN . The non-client area is where a dialogs menu lies and not in the client area.

      This helps to better understand a number of things in windows, such as:

      Messages like WM_LBUTTONDOWN (client area mouse messages) pass the cursor position in client coordinates, while messages like WM_NCLBUTTONDOWN (ie. click on caption bar) pass the cursor position in screen coordinates. Non-Client areas of windows don't use client coordinates.

      This difference between client area and non-client area is important. Even controls have both client area and non-client area. For example a controls border is part of its non-client area.

      When you use window coordinates and something is off by an exact amount (ie. the height of a menu bar or the width of a scrollbar) , you are likely making an error in determining what is part of the client area and what is part of the non-client area.

      A very useful API function to use to compensate for missing the width or height of something like a border or menu is the GetSystemMetrics function. It can be used to get the height or width of elements of windows non-client areas (ie. a 3D border width).
      Last edited by Chris Boss; 15 Feb 2008, 12:27 AM.
      Chris Boss
      Computer Workshop
      Developer of "EZGUI"


      • #4
        GetWindowInfo is another useful function.

        This restricts the mouse cursor to a window's client area:
        wi.cbSize = SIZEOF(WINDOWINFO)
        GetWindowInfo(hWnd, wi)