Click here to Skip to main content
15,889,116 members
Articles / Desktop Programming / MFC
Article

CInputEvent class

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
6 Mar 20032 min read 45.4K   678   15   5
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


Written By
Software Developer (Junior)
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvery nice Pin
ericnparker9-Aug-07 15:46
ericnparker9-Aug-07 15:46 
Generalhelp Pin
Kiethnt12-Nov-06 22:30
Kiethnt12-Nov-06 22:30 
Dear all,
I just start with VC++, let I desribe overview my project :
Firstly, I have a image view 320x240 pixels and on this view I would like to get the value of pixel follow horizon and vertical by left click button. These value (i,j) will be imported to another dialog. Any body know how to do this please show me. Many thanks for your help.
GeneralCode Practices Pin
Blake Miller10-Mar-03 2:33
Blake Miller10-Mar-03 2:33 
GeneralRe: Code Practices Pin
RiriOnTheWeb10-Mar-03 9:42
RiriOnTheWeb10-Mar-03 9:42 
Generala good solution Pin
xuell009-Mar-03 15:39
xuell009-Mar-03 15:39 

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

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