Hi,
Given a HWND, how can I get the width and height of a window?
Thanks,
Daniel
This is a discussion on getting window size within the Windows Programming forums, part of the Platform Specific Boards category; Hi, Given a HWND, how can I get the width and height of a window? Thanks, Daniel...
Hi,
Given a HWND, how can I get the width and height of a window?
Thanks,
Daniel
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.
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)
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:
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.Code:myHWnd.width = 500; myHWnd.height = 800; UpdateMyHWnd(myHWnd, UPDATEFLAG_SIZE); // Don't forget this or nothing will happen to your window's dimensions!
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.