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

MouseLeave and MouseEnter functions

By , 22 Dec 2002
Rate this:
Please Sign up or sign in to vote.
<!-- Download Links --> <!-- Article image -->

Sample Image - MouseEnterLeave.gif

<!-- Add the rest of your HTML here -->

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

Comments and Discussions

 
GeneralMy vote of 1 PinmvpSergey Alexandrovich Kryukov17-Mar-14 5:52 
Questionwhere is the source code? Pinmemberamarasat20-Jun-11 6:00 
GeneralAn alternate (and very simple) method using PreTranslateMessage PinmemberMikus Lóránt20-Sep-07 4:55 
GeneralRe: An alternate (and very simple) method using PreTranslateMessage PinmemberMikus Lóránt20-Sep-07 12:18 
QuestionHow to apply on CStatic/CButton PinmemberCosmoNova25-Jul-05 16:51 
AnswerRe: How to apply on CStatic/CButton PinmemberBigAlmond12-Oct-05 22:37 
GeneralDOES NOT WORK !!! Pinmembersimone massaro30-Jun-05 5:38 
Generaltext editor in c++ Pinmembersankritayayana14-Jun-05 9:09 
GeneralRe: text editor in c++ PinmemberChristof Schardt14-Jun-05 9:31 
GeneralNot robust PinadminChris Maunder25-Dec-02 12:52 
QuestionOnMouseLeave? PinmemberMarc Clifton23-Dec-02 5:16 
GeneralSwitching Applications PinmemberMarc Clifton23-Dec-02 5:14 
GeneralWhy not use TrackMouseEvent() PinmemberAndreas Saurwein23-Dec-02 2:57 
GeneralRe: Why not use TrackMouseEvent() PinmemberPaul Lyons23-Dec-02 3:31 
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein23-Dec-02 3:48 
GeneralRe: Why not use TrackMouseEvent() PinmemberBill Gates Antimatter Particle23-Dec-02 5:05 
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein23-Dec-02 5:21 
GeneralRe: Why not use TrackMouseEvent() PinmemberRabidCow23-Dec-02 11:15 
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein23-Dec-02 15:15 
GeneralRe: Why not use TrackMouseEvent() PinmemberExsuscito2-Dec-03 8:52 
GeneralRe: Why not use TrackMouseEvent() PinsussAnonymous23-Dec-02 23:25 
GeneralRe: Why not use TrackMouseEvent() PinmemberAndreas Saurwein25-Dec-02 7:49 
GeneralRe: Why not use TrackMouseEvent() PinmemberGregor S.24-Dec-02 1:14 
GeneralRe: Why not use TrackMouseEvent() PinmemberYi Yang25-Dec-02 17:47 

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.

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