Click here to Skip to main content
15,897,187 members
Articles / Programming Languages / C++

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

Rate me:
Please Sign up or sign in to vote.
3.36/5 (9 votes)
23 Aug 20042 min read 86.5K   1.6K   23  
Ever wanted to get mouse hover/leave message from system tray? Windows shell doesn't support that. This class is a solution that works.
// TrayPosDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TrayPosDemo.h"
#include "TrayPosDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTrayPosDemoDlg dialog

CTrayPosDemoDlg::CTrayPosDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTrayPosDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTrayPosDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTrayPosDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTrayPosDemoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTrayPosDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CTrayPosDemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_TRAYNOTIFY, OnTrayNotify)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrayPosDemoDlg message handlers

BOOL CTrayPosDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	NOTIFYICONDATA	nid;

	nid.cbSize = sizeof nid;
	nid.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	nid.hWnd = m_hWnd;
	nid.uCallbackMessage = WM_TRAYNOTIFY;
	nid.uID = 1;
	nid.uFlags = NIF_ICON | NIF_MESSAGE;

	Shell_NotifyIcon(NIM_ADD, &nid);
	m_traypos.SetNotifyIconInfo(m_hWnd, 1, WM_TRAYNOTIFY);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTrayPosDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTrayPosDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

LONG CTrayPosDemoDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
	if(wParam == 1)
	{
		switch(lParam)
		{
		case WM_MOUSEMOVE:
			m_traypos.OnMouseMove();
			break;

		case WM_MOUSEHOVER:
			m_dlg.Create(CBmpDlg::IDD, this);
			m_dlg.ShowWindow(SW_SHOW);
			break;

		case WM_MOUSELEAVE:
			m_dlg.ShowWindow(SW_HIDE);
			m_dlg.DestroyWindow();
			break;
		}
	}
	return 0;
}

void CTrayPosDemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	NOTIFYICONDATA	nid;
	
	nid.cbSize = sizeof nid;
	nid.hWnd = m_hWnd;
	nid.uID = 1;
	nid.uFlags = 0;
	
	Shell_NotifyIcon(NIM_DELETE, &nid);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Chief Technology Officer Wellbia.com Co., Ltd.
Korea (Republic of) Korea (Republic of)
YoungJin is a co-founder of Wellbia.com 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 (http://www.jiniya.net) that includes articles about system programming on Windows.

Comments and Discussions