Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls
dc.SelectObject(pOldBrush);
}
After executing the program and moving the dialog box somewhere to the middle center
of the screen and clicking the button, the result is as follows:
After moving the dialog box close to the top-left section of the screen and clicking the
button again, the result is the following:
This demonstrates that, although the control is a child of the dialog box, the rectangle
returned by the GetWindowRect() method is based on the screen and not the client
coordinates of the parent window. This is not an anomaly. It is purposely done so you can
specify what origin you want to consider.
As seen in previous lessons, the origin of the screen is positioned on the top-left corner of
the monitor. This is referred to as, or is said that the location uses, screen coordinates.
The origin of a client area is placed on its top-left corner. This is referred to as, or is said
that the location uses, client coordinates. For example, the origin used by the above
GetWindowRect() method is based on the screen. If you want the rectangle resulting
from a call to either the GetClientRect() or the GetWindowRect() methods to be based
on the client area (on client coordinates) of the control that called it, you can transfer the
origin from the screen to the client. This is conveniently done with a call to the
CWnd::ClientToScreen() method. It is overloaded as follows:
void ClientToScreen(LPPOINT lpPoint) const;
void ClientToScreen(LPRECT lpRect) const;
If the location you had requested is a point, pass its POINT or its CPoint variable to the
ClientToScreen() method. If the value you requested is a rectangle, pass its RECT or its
CRect variable. Here is an example:
void CTabDlg::OnBtnInfo()
{
// TODO: Add your control notification handler code here