Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls
- On the Controls toolbox, click the Picture button and draw a rectangular object
on the dialog box - Change its ID to IDC_PREVIEW
- Add a CStatic Control variable for the picture object and name it m_Preview
- In the header file of the CColorPreviewDlg class, declare a private COLORREF
variable named PreviewColor and a member function named UpdatePreview of
type void
private:
COLORREF PreviewColor;
void UpdatePreview();
};
- In the OnInitDialog event of the dialog, initialize the PreviewColor variable with a
gray color as follows:
PreviewColor = RGB(192, 192, 192);
return TRUE; // return TRUE unless you set the focus to a control
}
- In the source file of the dialog box, implement the UpdatePreview() method as
follows:
void CColorPreviewDlg::UpdatePreview()
{
CClientDC dc(this); // device context for painting
// TODO: Add your message handler code here
CBrush BrushBG(PreviewColor);
CRect RectPreview;
m_Preview.GetWindowRect(&RectPreview);
CBrush *pOldBrush = dc.SelectObject(&BrushBG);
ScreenToClient(&RectPreview);
dc.Rectangle(RectPreview);
dc.SelectObject(pOldBrush);
}
- Save All