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

CInputEvent class

By , 6 Mar 2003
 

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

About the Author

RichardV
Software Developer (Junior)
France France
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalvery nicememberericnparker9 Aug '07 - 15:46 
GeneralhelpmemberKiethnt12 Nov '06 - 22:30 
GeneralCode PracticesmemberBlake Miller10 Mar '03 - 2:33 
GeneralRe: Code PracticesmemberRichardV10 Mar '03 - 9:42 
Generala good solutionmemberxuell009 Mar '03 - 15:39 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 7 Mar 2003
Article Copyright 2003 by RichardV
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid