Click here to Skip to main content
Licence 
First Posted 22 Dec 2002
Views 74,309
Bookmarked 30 times

MouseLeave and MouseEnter functions

By | 22 Dec 2002 | Article
A simple way to implement MouseLeave & Mouse Enter functions

Sample Image - MouseEnterLeave.gif

Introduction

Whenever MFC applications are written and, above all whenever we write controls, it often happens that we want to find a way to know when the mouse enters on our control window and when it leaves. Unfortunately, as we know, Windows does not support these kind of events.

Using the code

The idea standing behind this source is very simple, just handle the WM_MOUSEMOVE message and capture the mouse input with the SetCapture function. So you will have to test if the pointer is actually on your window. If not you have to release the mouse (ReleaseCapture).

In order to have the OnMouseEnter & OnMouseLeave functions you have to keep a BOOL updated with TRUE whether the pointer is on the Window and FALSE when it isn't. When you try to turn on the BOOL, if it's not already TRUE you will have a MouseEnter. While, every time you call a ReleaseCapture you will have a MouseLeave.

void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)
{
  SetCapture();               //  Capture the mouse input
  CRect wndRect;
  GetWndRect(&wndRect);
  ScreenToClient(&wndRect);

  if (wndRect.PtInRect(point))	
  {  // Test if the pointer is on the window
    if (m_PointerOnWnd != TRUE)	
    {
      OnMouseEnter();
      m_PointerOnWnd = TRUE;
    }
  } 
  else
  {
    ReleaseCapture();
    m_PointerOnWnd = FALSE;
  }
  CWnd::OnMouseMove(nFlags, point);
}

There's no problem even if the mouse is on the window when the application starts, because in this case Windows will automatically send a WM_MOUSEMOVE event.

That's all folks

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

turco-7th

Web Developer

Italy Italy

Member

Amedeo Margarese lives in Palermo, italy (please don't you say MAfia and blablabla Smile | :) ) He started programming when he was fourteen moving his efforts on Windows MFC Application and Game developing.
He has a company based in italy, the 7th Sense s.r.l. www.7th-sense.net where he is the Head of AGB (Game Boy Advance) development.

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwhere is the source code? Pinmemberamarasat6:00 20 Jun '11  
GeneralAn alternate (and very simple) method using PreTranslateMessage PinmemberMikus Lóránt4:55 20 Sep '07  
GeneralRe: An alternate (and very simple) method using PreTranslateMessage PinmemberMikus Lóránt12:18 20 Sep '07  
QuestionHow to apply on CStatic/CButton PinmemberCosmoNova16:51 25 Jul '05  
AnswerRe: How to apply on CStatic/CButton PinmemberBigAlmond22:37 12 Oct '05  
GeneralDOES NOT WORK !!! Pinmembersimone massaro5:38 30 Jun '05  
Generaltext editor in c++ Pinmembersankritayayana9:09 14 Jun '05  
GeneralRe: text editor in c++ PinmemberChristof Schardt9:31 14 Jun '05  
GeneralNot robust PinadminChris Maunder12:52 25 Dec '02  
QuestionOnMouseLeave? PinmemberMarc Clifton5:16 23 Dec '02  
GeneralSwitching Applications PinmemberMarc Clifton5:14 23 Dec '02  
GeneralWhy not use TrackMouseEvent() PinmemberAndreas Saurwein2:57 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberPaul Lyons3:31 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein3:48 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberBill Gates Antimatter Particle5:05 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein5:21 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberRabidCow11:15 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein15:15 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberExsuscito8:52 2 Dec '03  
GeneralRe: Why not use TrackMouseEvent() PinsussAnonymous23:25 23 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein7:49 25 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberGregor S.1:14 24 Dec '02  
GeneralRe: Why not use TrackMouseEvent() PinmemberYi Yang17:47 25 Dec '02  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 23 Dec 2002
Article Copyright 2002 by turco-7th
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid