Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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



  1. To set the starting color of the preview picture, access the OnPain() event of the
    CcolorPreviewDlg class and change it as follows:


void CColorPreviewDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect RectPreview;
CBrush BrushGray(PreviewColor);

m_Preview.GetWindowRect(&RectPreview);
CBrush *pOldBrush = dc.SelectObject(&BrushGray);

ScreenToClient(&RectPreview);
dc.Rectangle(RectPreview);

dc.SelectObject(pOldBrush);

if (IsIconic())
{
SendMessage(WM_ICONERASEBKGND,
reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}


  1. Test the application and return to MSVC


17.1.2..Creating a Spin Button.......................................................................


To create a spin button, at design time, you can click the Spin button on the Controls
toolbox and click the desired area on the intended host.

A spin button control is based on the CSpinButtonCtrl class. Therefore, to
programmatically create a spin button, declare a pointer to CSpinButtonCtrl then call its
Create() method to initialize it. The syntax of the CSpinButtonCtrl::Create() method
is:

BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );

Because a spin button cannot be a parent, the minimum style you must apply is
WS_CHILD. If you want the control to be visible upon creating, add the WS_VISIBLE
style. Here is an example:
Free download pdf