Adding the FlashColor Function

The FlashColor function paints the circle using the color value stored as the value of the FlashColor property. The code is similar to the code in the OnDraw function. The difference is that the FlashColor function paints the circle itself using the value of the FlashColor property, rather than that of the BackColor property, to fill the background of the ellipse.

To implement the FlashColor function

  1. On WizardBar, select CCircCtl to edit the CCIRCCTL.CPP file.

  2. Click the arrow on the action button, located on the right end of WizardBar, and select Add Member Function.

    The Add Member Function dialog box is displayed.

  3. In the Function Type edit box, type void. This is the type of value the function will return.

  4. In the Function Declaration edit box, type FlashColor(CDC* pdc).

  5. Under Access, click Protected.

  6. Click OK to close the Add Member Function dialog box.

    This will add the following declaration in the protected section of CIRCCTL.H:

    void FlashColor(CDC* pdc);
    

    It will also add a function definition at the end of CIRCCTL.CPP and position the cursor in the function:

    void CCircCtrl::FlashColor(CDC* pdc)
    {
    
    }
    
  7. Add code to the function in CIRCCTL.CPP as shown:
    void CCircCtrl::FlashColor(CDC* pdc)
    {
        CBrush* pOldBrush;
        CBrush flashBrush(TranslateColor(m_flashColor));
        CPen* pOldPen;
        CRect rc;
    
        GetClientRect(rc);
        GetDrawRect(&rc);
        pOldBrush = pdc->SelectObject(&flashBrush);
        pOldPen = (CPen*)pdc->SelectStockObject(BLACK_PEN);
        pdc->Ellipse(rc);
        pdc->SelectObject(pOldPen);
        pdc->SelectObject(pOldBrush);
    }
    

The flashBrush variable constructs a solid brush using the value of the FlashColor property stored in m_flashBrush. Because m_flashBrush is an OLE_COLOR value, the TranslateColor function is called to convert it to a COLORREF value first. The code selects the brush into the device context pdc, making sure to retain the old brush value in pOldBrush. A stock black pen is also selected into the device context. The old pen value is saved in pOldPen. The ellipse is then drawn with the black pen and filled in with the color specified by the value of the FlashColor property. Finally, the code selects the original pen and brush back into the device context pdc. The solid brush created when flashBrush is constructed is deleted when the flashBrush destructor is called. This occurs when the FlashColor function returns and the flashBrush variable goes out of scope.