Click here to Skip to main content
13,195,379 members (68,264 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 23 Aug 2004

How to implement mouse hover/leave message on system tray.

, 23 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Ever wanted to get mouse hover/leave message from system tray? Windows shell doesn't support that. This class is a solution that works.


This article illustrates how to implement tray mouse hover/leave message.

The problem

Some programs want to get mouse hover/leave message from system tray when they want to show popup window which is not tool tip. However, Windows shell doesn't support it. We can only get mouse move, up, down, and double click message from system tray.

It is very easy to implement WM_MOUSEHOVER message because the first WM_MOUSEMOVE is that. But WM_MOUSELEAVE is hard to implement, so I decided to write a compact class to notify a window of mouse hover/leave message.

The idea

The basic idea is very simple. It is that no WM_MOUSEMOVE message fires when the mouse is out of tray icon. This class saves mouse point whenever WM_MOUSEMOVE message fires on system tray, and then, another thread compares it with the current mouse point. The thread fires WM_MOUSELEAVE message when the two points are different.

The following code is WM_MOUSEMOVE handler:

VOID CTrayPos::OnMouseMove()

    if(m_bTrackMouse == FALSE)
        m_bTrackMouse = TRUE;


This is a thread function which checks the mouse point.

UINT CALLBACK CTrayPos::TrackMousePt(PVOID pvClass)
    POINT       ptMouse;
    CTrayPos    *pTrayPos = (CTrayPos *) pvClass;

    while(WaitForSingleObject(pTrayPos->m_hExitEvent, 2000) == WAIT_TIMEOUT)

        if(pTrayPos->m_bTrackMouse == TRUE)
            if(ptMouse.x != pTrayPos->m_ptMouse.x || 
                      ptMouse.y != pTrayPos->m_ptMouse.y)
                pTrayPos->m_bTrackMouse = FALSE;

    return 0;

Using the code

If you want to use this class in your project - just follow these simple steps.

  • Add traypos.h, traypos.cpp files to your project.
  • Declare a variable of this class like that:
    CMsgTrayPos traypos; 

    Note that this class instance exists until the tray icon is deleted, so you must declare it as a member variable or a global variable.

  • Add the following code after registering tray by calling Shell_NotifyIcon.

    The three parameters -- hwnd, uID, uMsgID -- are the same value which is the member of NOTIFYICONDATA.

    traypos.SetNotifyIconInfo(hwnd, uID, uMsgID);
  • Call CMsgTrayPos::OnMouseMove on your WM_MOUSEMOVE message handler from system tray.
  • Now you can add WM_MOUSEHOVER and WM_MOUSELEAVE messages to your tray message handler and write code there.

CMsgTrayPos API

  • VOID CMsgTrayPos::SetNotifyIconInfo(HWND hwnd, UINT uID, UINT uCallbackMsg);

    This function provides tray icon information to the class. The three parameters -- hwnd, uID, uCallbackMsg -- and members of NOTIFYICONDATA are the same.

  • VOID CMsgTrayPos::OnMouseMove()

    This function is very important. You must call this function when you get WM_MOUSEMOVE message from system tray. This function checks mouse hover status and updates mouse point internally.

  • BOOL CMsgTrayPos::IsIconHover();

    This function returns mouse hover status. It returns TRUE if your mouse is on your tray icon.


  • 22 Aug 2004 - Initial upload.


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

YoungJin Shin
Chief Technology Officer Co., Ltd.
Korea (Republic of) Korea (Republic of)
YoungJin is a co-founder of Co., Ltd., a security company in South Korea, and Visual C++ Microsoft Most Valuable Professional. He has developed anti-cheat program called XIGNCODE since 2007. He wrote several PC security programs like PC Firewall, Anti-Spyware, and Keyboard Security Software. He has contributed a number of articles about Windows programming to Microsoftware, the famous programming magazine of South Korea. He also hosts a blog ( that includes articles about system programming on Windows.

You may also be interested in...


Comments and Discussions

Generalawesome! Pin
slashaxl25-Sep-14 17:01
memberslashaxl25-Sep-14 17:01 
GeneralMy vote of 5 Pin
hacysean29-May-11 21:43
memberhacysean29-May-11 21:43 
Generalcool Pin
Lehi6-Jan-05 12:13
memberLehi6-Jan-05 12:13 
GeneralAlternate way to get mouse enter/leave Pin
Jim Crafton24-Aug-04 6:04
memberJim Crafton24-Aug-04 6:04 
GeneralRe: Alternate way to get mouse enter/leave Pin
Shin, YoungJin24-Aug-04 8:16
memberShin, YoungJin24-Aug-04 8:16 
GeneralRe: Alternate way to get mouse enter/leave Pin
Jim Crafton24-Aug-04 10:41
memberJim Crafton24-Aug-04 10:41 
GeneralSweet! Pin
Jim Crafton24-Aug-04 5:46
memberJim Crafton24-Aug-04 5:46 
Hell I gave you 5 just for the great screenshot! Smile | :)
Made my day Smile | :)

¡El diablo está en mis pantalones! ¡Mire, mire!

Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!

0 rows returned

GeneralRe: Sweet! Pin
Shin, YoungJin24-Aug-04 8:17
memberShin, YoungJin24-Aug-04 8:17 

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
Web01 | 2.8.171019.1 | Last Updated 24 Aug 2004
Article Copyright 2004 by YoungJin Shin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid