How do you get the height and width of a CWnd*? The CWnd is the window correct? Why isn't the command:
CWnd* parent = this->GetParent(); // C++ command
parent->GetSize(); // what I think the method should be OR ...
parent->GetWindowRect(); // what i think it should be (no arguments)
what is this LPRECT? I already have the object ... why and what is the argument going into GetWindowRect? What am I pointing to? I already have the object i want to find the size of ... just give me the size.
The
LPRECTparameter is a pointer to aRECTstructure (the "LP" prefix actually stands for "long pointer", for historical reasons).The
GetWindowRectfunction is going to retrieve the window rectangle for yourCWndobject, but it's going to do so by filling in aRECTstructure with those coordinates. Therefore, you need to create aRECTstructure and pass a pointer to it to theGetWindowRectfunction.It is worth mentioning that the API accepts a pointer to a
RECTstructure for full compatibility with Win32. TheCRectMFC class actually inherits from theRECTstructure defined by the SDK, so you can use aCRectobject interchangeably here. Which is nice, becauseCRectprovides member functions that make it easier to manipulate rectangles.Sample code:
Note that the
GetWindowRectfunction will return the screen coordinates of your window. This is usually not what you want, unless you're trying to reposition the window on the screen. Screen coordinates are tricky to work with because they are relative to the entire virtual screen, which can have negative coordinates in a multi-monitor configuration. Also, if you try and determine the size of the window using its screen coordinates, you'll get the entire size of the window on the screen, including its non-client areas (like the title bar, the min/max/close buttons, etc.).What you normally want instead are the client coordinates of a window, retrievable by calling the
GetClientRectfunction in an identical manner. This time, we'll use aRECTstructure, just because we can: