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
The Add Member Function dialog box is displayed.
void
. This is the type of value the function will return.FlashColor(CDC* pdc)
.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)
{
}
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.