Chapter 15: Fundamental Controls Visual C++ and MFC Fundamentals
GetClientRect() function is called to get the dimensions of the view’s client area of a
frame-based application and use the resulting rectangle to paint that area:
void CCView1View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect Recto;
CBrush SelectedBrush(SelectedColor);
CPen SelectedBlue(PS_SOLID, 1, SelectedColor);
GetClientRect(&Recto);
CBrush *pOldBrush = dc.SelectObject(&SelectedBrush);
CPen *pOldPen = dc.SelectObject(&SelectedBlue);
dc.Rectangle(Recto);
dc.SelectObject(pOldBrush);
// Do not call CView::OnPaint() for painting messages
}
Once the control is already positioned on the client area, to get its location and
dimensions, you can call the CWnd::GetWindowRect() method. Here is an example:
void CTabDlg::OnBtnInfo()
{
// TODO: Add your control notification handler code here
CRect Recto;
char LocDim[80];
m_Panel.GetWindowRect(&Recto);
sprintf(LocDim, " - Panel Information -\nLeft: %d,"
"\nTop: %d,\nWidth: %d,\nHeight: %d",
Recto.left, Recto.top, Recto.Width(), Recto.Height());
MessageBox(LocDim);
}
Practical Learning: Using the Client Area
- Open the Geometry application you were working on earlier
- Open the Quadrilateral.cpp source file and change its OnPaint() event as follows: