Thread: getting window size

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Malta
    Posts
    9

    getting window size

    Hi,

    Given a HWND, how can I get the width and height of a window?

    Thanks,

    Daniel

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The usual suspects:
    • GetClientRect - client dimensions (doesn't include caption, menus, borders etc)
    • GetWindowRect - the whole window in screen coordinates.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Malta
    Posts
    9
    Thanks. I didn't think it'd be so straightforward.

    HWND is some kind of pointer, no? If it points to some kind of struct related to the window, is it possible to get the width and height from it directly? (Just out of curiosity)

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    An HWND is designed to be used as a handle to a window, not the data of a window.

    Imagine for a moment if it were the opposite. Each and every time you wish to change the width and height of the window, you would have to do something like:

    Code:
    myHWnd.width = 500;
    myHWnd.height = 800;
    
    UpdateMyHWnd(myHWnd, UPDATEFLAG_SIZE);  // Don't forget this or nothing will happen to your window's dimensions!
    If you think about this, there are two ways of representing myHWnd. One way is to represent it as the data of the physical window on screen and every time you change the data, you have changed the data for the physical window. Imagine a car driving down the road where the mechanics are allowed to swap out the wheels at any time and you start to see some potential problems with a design like this.

    Another way is to store a unique ID in myHWnd so that we have a 'local' copy that we modify and then just fire off update requests when data has been changed. This may seem trivial to do, but we now have to store twice as much data for every single window (highly undesirable!).


    So the HWND handle is a simpler way of interfacing with your windows that removes the possibility of errors being made, removes the necessity of a large memory footprint, and provides an API-type of encapsulation on the window's data. Just think of the HWND as a unique ID that you use to interface with the data stored by Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM