Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls



  1. On the Controls toolbox, click the Picture button and draw a rectangular object
    on the dialog box

  2. Change its ID to IDC_PREVIEW

  3. Add a CStatic Control variable for the picture object and name it m_Preview

  4. 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();
};


  1. 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
}


  1. 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);
}


  1. Save All

Free download pdf