Click here to Skip to main content
13,259,539 members (78,334 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 6 Mar 2003

CInputEvent class

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


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);

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:

BOOL CMyButton::PreTranslateMessage(MSG* pMsg) 
    // Pass message to CInputEvent and relay it or not to mother class
    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;
    CString    str;

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

    // Button is disabled
    if (lpDrawItemStruct->itemState & ODS_DISABLED)
        switch (ips.drawPosition)
        case 1:     //CInputEvent::DRAW_NORMAL:
        case 2:     //CInputEvent::DRAW_OVER:
        case 3:     //CInputEvent::DRAW_PUSHED:
    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.

    // 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.


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

Software Developer (Junior)
France France
No Biography provided

You may also be interested in...

Comments and Discussions

Generalvery nice Pin
ericnparker9-Aug-07 16:46
memberericnparker9-Aug-07 16:46 
Generalhelp Pin
Kiethnt12-Nov-06 23:30
memberKiethnt12-Nov-06 23:30 
GeneralCode Practices Pin
Blake Miller10-Mar-03 3:33
memberBlake Miller10-Mar-03 3:33 
GeneralRe: Code Practices Pin
RichardV10-Mar-03 10:42
memberRichardV10-Mar-03 10:42 
Generala good solution Pin
xuell009-Mar-03 16:39
memberxuell009-Mar-03 16: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 7 Mar 2003
Article Copyright 2003 by RiriOnTheWeb
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid