Click here to Skip to main content
Click here to Skip to main content

CInputEvent class

, 6 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Easier mouse event management for your owner drawn controls

Introduction

The 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 OnLButtonDown, OnMouseMove, etc. methods anymore Also, all the usual m_OverTracking, m_ButtonDown, 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.

To Use

Declare a pointer to a CInputEvent data :

CInputEvent* m_pInputEvent;
Init this data once the CWnd button is created, in the PreSubclassWindow.
void CMyButton::PreSubclassWindow() 
{
    //--------------------------------------------------
    // Init InputEvents
    //--------------------------------------------------
    m_pInputEvent = new CInputEvent(this);
    CButton::PreSubclassWindow();
}

Filter the Windows Message in PreTranslateMessage(MSG* pMsg). This will fill a data structure which updates the button state.

The CInputEvent class will filter the messages:

WM_MOUSEMOVE<BR>WM_MOUSELEAVE<BR>WM_LBUTTONDOWN<BR>WM_LBUTTONUP<BR>WM_LBUTTONDBLCLK
BOOL CMyButton::PreTranslateMessage(MSG* pMsg) 
{
    //--------------------------------------------------
    // Pass message to CInputEvent and relay it or not to mother class
    //--------------------------------------------------
    m_pInputEvent->RelayMsg(pMsg);
    return CButton::PreTranslateMessage(pMsg);
}

In the 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) 
{
    // Gets Button status
    CInputEvent::INPUTSTATUS ips;
    m_pInputEvent->GetStatus(&ips);
    CString    str;

    // Gets DC
    CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);

    // Button is disabled
    if (lpDrawItemStruct->itemState & ODS_DISABLED)
    {
        str="Disabled";
    }
    else
    {
        switch (ips.drawPosition)
        {
        case 1:     //CInputEvent::DRAW_NORMAL:
            str="Normal";
            break;
        case 2:     //CInputEvent::DRAW_OVER:
            str="Over";
            break;
        case 3:     //CInputEvent::DRAW_PUSHED:
            str="Pushed";
            break;
        }
    }
    
    pDC->FillSolidRect(&ips.rect, RGB(100,200,255));
    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 WM_ENABLE message. So I use the DrawItem Structure to get this info. I could use WindowProc method but it receives messages from other controls.

Delete the CInputEvent data in the button destructor.

CMyButton::~CMyButton()
{
    // free memory
    delete m_pInputEvent;
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

RiriOnTheWeb
Software Developer (Junior)
France France
No Biography provided

Comments and Discussions

 
Generalvery nice Pinmemberericnparker9-Aug-07 16:46 
Generalhelp PinmemberKiethnt12-Nov-06 23:30 
GeneralCode Practices PinmemberBlake Miller10-Mar-03 3:33 
GeneralRe: Code Practices PinmemberRichardV10-Mar-03 10:42 
Generala good solution Pinmemberxuell009-Mar-03 16:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150326.1 | Last Updated 7 Mar 2003
Article Copyright 2003 by RiriOnTheWeb
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid