3
votes

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.

3
There is not GetSize method in the CWnd class. And the GetWindowRect method needs a parameter which is the pointer to the rectangle where the position and the size of your window will go.Jabberwocky

3 Answers

8
votes

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);
6
votes

The answer is you use GetWindowRect.

CWnd* parent = this->GetParent();
CRect size;
parent->GetWindowRect(&size);

If you are asking why it is done like that, I can think of two answers:

  1. MFC is very old (older than some of the people reading this I suspect). Compilers couldn't handle returning structures by value in those days. Since then "backwards compatability".
  2. MFC is (or at least, was originally) a very thin wrapper over the Windows API functions.
0
votes

okay, I figured out my answer I believe. Here for anyone who cares to know ...

CRect rc_total_window;
this->GetWindowRect(rc_total_window);

where 'this' is a CWnd* object. Thanks again for all the history and explanation. It helps to know why things are the way they are so you can be sure that you aren't doing something wrong; especially helps when the methodology is different than what was learned in modern language courses.