Advertisement Jump to content
Sign in to follow this  

Programming a GUI - how to determine mouseover/mouseout?

This topic is 4909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm programming a GUI library in C++ (using sigc++ for event handling), and I've been wondering about this fairly simple problem. In most GUI systems today the library emits a message for 'mouse over' and 'mouse out', no? However, IIRC this message should only be emitted once, when the mouse is leaving/entering the section. My question is - I can determine when the mouse is on and when the mouse is off, but what is the best way to determine when it has crossed the boundary? Currently I am doing this:
class Widget
    bool m_MouseOver;
And the mouseover/mouseout code checks the variable to determine the status of the widget at the last frame. I'm wondering if there isn't a better (cleaner, more 'correct', 'cooler', whatever) method of achieving this same goal. For example, by storing the previous position of the mouse or something like that. I would really like to hear what your ideas about this may be. Cheers!

Share this post

Link to post
Share on other sites
Try using the internal MouseMove event. When the mouse moves, check to see what component/window it is over. Here is some pseudo code to explain:-

bool MouseMoveEventCallBack( CMouse* pMouse )
if ( pMouse == NULL )
return false;
CWindow* pWindow = pMouse->GetCurrentWindow( );

if ( pWindow != NULL )
// is the mouse still on the window?
if ( !pWindow->IsOn( pMouse ) ) {
pWindow->SendMouseOffEvent( )

// get what window/component the mouse is over now
pWindow = CGUI::GetWindowMouseIsOver( pMouse );

// send the MouseOn event if the mouse is over a window/component
if ( pWindow != NULL )
pWindow->SendMouseOnEvent( );

// store the new window
pMouse->SetCurrentWindow( pWindow );
} // end if
return true;
} // end MouseMove

So the mouse stores the window that it is over. When the mouse moves, we check its position. If the mouse is no longer on the window, then we know the it has just left it. So we send the MouseOff event. Then, we check to see what window the mouse has entered. If it is on one, we send the MouseOn event.

NOTE: This is only the theory. A better actual implementation approach would be to have the GUI hander store the mouse's current window. And, depending on how you manage your windows, it would store the window’s name and/or index against the mouse for mouse’s current window.

I hope this helps!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!