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 LPRECT
parameter is a pointer to a RECT
structure (the "LP" prefix actually stands for "long pointer", for historical reasons).
The GetWindowRect
function is going to retrieve the window rectangle for your CWnd
object, but it's going to do so by filling in a RECT
structure with those coordinates. Therefore, you need to create a RECT
structure and pass a pointer to it to the GetWindowRect
function.
It is worth mentioning that the API accepts a pointer to a RECT
structure for full compatibility with Win32. The CRect
MFC class actually inherits from the RECT
structure defined by the SDK, so you can use a CRect
object interchangeably here. Which is nice, because CRect
provides member functions that make it easier to manipulate rectangles.
Sample code:
CWnd* pwndParent = this->GetParent();
CRect rc;
pwndParent->GetWindowRect(&rc);
// rc now contains the rectangle of your window!
Note that the GetWindowRect
function 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 GetClientRect
function in an identical manner. This time, we'll use a RECT
structure, just because we can:
CWnd* pwndParent = this->GetParent();
RECT rcClient;
pwndParent->GetClientRect(&rcClient);