CInputEvent class manages mouse events. It updates a data structure which is available DrawItem or OnPaint methods to allow you to draw control at with the correct visual state.
Using the code
This class avoids the use of all the methods usually used to get the button state. The Button class doesn't need all the
OnMouseMove, etc. methods anymore Also, all the usual m_
TrackMouseEvent etc. are not necessary in the derived
CWnd class (button, static, etc...).
The data structure is updated for each of these received messages. When the button needs to be drawn, say in the DrawItem() method, you just need to get the button status and then draw the corresponding bitmap or text or what ever you want.
Declare a pointer to a
CInputEvent data :
Init this data once the CWnd button is created, in the
m_pInputEvent = new CInputEvent(this);
Filter the Windows Message in
PreTranslateMessage(MSG* pMsg). This will fill a data structure which updates the button state.
CInputEvent class will filter the messages:
BOOL CMyButton::PreTranslateMessage(MSG* pMsg)
DrawItem method, get the
InputStatus sructure. This structure contains the button state, the client rect (
CRect), the mouse coords, the status of the ctrl and shift keys and other mouse buttons.
Here is the code used in the demo :
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
if (lpDrawItemStruct->itemState & ODS_DISABLED)
pDC->DrawText(str, &ips.rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
Note that the disabled state is not managed since the button is disabled before receiving the
message. So I use the
Structure to get this info. I could use
method but it receives messages from other controls.
CInputEvent data in the button destructor.
Points of Interest
Using this class avoids to overload an Ownerdraw button class or a graphical static responding to mouse.
The other point is the windows message filtering. The class can manage any of the received messages. It becomes easy to add Wheel mouse, right click, keyboard input etc.. to manage the drawing of any control.