To better understand how to implement FlashColor functionality, you need to understand the behavior the Circle control should have when the mouse is clicked inside it. When the left mouse button is pressed, the circle is painted using the color stored as the value of the FlashColor property. When the left mouse button is released, the circle is repainted using the color stored as the value of the BackColor property. Clicking in the circle causes it to flash.
To implement the flash behavior, the Circle control must handle mouse events. These events are mapped to the following Windows mouse messages:
Message | Response |
WM_LBUTTONDOWN | Paints the circle with the color stored as the value of the FlashColor property. |
WM_LBUTTONDBLCLK | Paints the circle with the color stored as the value of the FlashColor property. |
WM_LBUTTONUP | Paints the circle with the color stored as the value of the BackColor property. |
Notice that a WM_LBUTTONDBLCLK message is handled in the same manner as a WM_LBUTTONDOWN message. If the left mouse button is double-clicked in the circle, the desired flash effect occurs.
The next step is to use ClassWizard to add a message handler for each of the three mouse messages.
To add the message handlers
The list of message types appears in the Messages list box.
Notice that this new handler appears in the Member functions list box as:
Member functions:
OnLButtonDown ON_WM_LBUTTONDOWN
Notice that this new handler appears in the Member functions list box as:
Member functions:
OnLButtonDblClk ON_WM_LBUTTONDBLCLK
Notice that this new handler appears in the Member functions list box as:
Member functions:
OnLButtonUp ON_WM_LBUTTONUP
ClassWizard closes and the cursor is positioned at the CCircCtrl:OnLButtonDown
function in CIRCCTL.CPP.
For each message handled, ClassWizard automatically inserts the function with a //TODO comment line and a call to the base class, as shown in the code for OnLButtonDown
:
void CCircCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
COleControl::OnLButtonDown(nFlags, point);
}
The code below shows the fully implemented mouse message handlers. Replace the //TODO comment lines with your Circle drawing code as shown in the following code.
void CCircCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
CDC* pdc;
// Flash the color of the control if within the ellipse.
if (InCircle(point))
{
pdc = GetDC();
FlashColor(pdc);
ReleaseDC(pdc);
}
COleControl::OnLButtonDown(nFlags, point);
}
void CCircCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CDC* pdc;
// Flash the color of the control if within the ellipse.
if (InCircle(point))
{
pdc = GetDC();
FlashColor(pdc);
ReleaseDC(pdc);
}
COleControl::OnLButtonDblClk(nFlags, point);
}
void CCircCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
// Redraw the control.
if (InCircle(point))
InvalidateControl();
COleControl::OnLButtonUp(nFlags, point);
}
OnLButtonDown
and OnLButtonDblClk
implement the same code: The circle will be painted using the color stored as the value of the FlashColor property. The OnLButtonUp
function invalidates the control, causing the circle to be redrawn with the default background color.
Notice that to implement the FlashColor property's behavior, two new functions, InCircle
and FlashColor
, have been introduced. These functions are described in Hit Testing and Adding the FlashColor Function.