www.pudn.com > VCShell_Samples.rar > Trayicon2.cpp


//////////////////////////////////////////////////////////////// 
// CTrayIcon2 Copyright 1996 Microsoft Systems Journal. 
// 
// If this code works, it was written by Paul DiLascia. 
// If not, I don't know who wrote it. 
// 
// CTrayIcon2, modified by ThMaurel. 
// 
#include "stdafx.h" 
#include "resource.h" 
#include "trayicon2.h" 
#include 		// for AfxLoadString 
 
IMPLEMENT_DYNAMIC(CTrayIcon2, CCmdTarget) 
 
CTrayIcon2::CTrayIcon2(UINT uID) 
{ 
	// Initialize NOTIFYICONDATA 
	memset(&m_nid, 0 , sizeof(m_nid)); 
	m_nid.cbSize = sizeof(m_nid); 
	m_nid.uID = uID;	// never changes after construction 
 
	// Use resource string as tip if there is one 
	AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip)); 
} 
 
CTrayIcon2::~CTrayIcon2() 
{ 
	SetIcon(0); // remove icon from system tray 
} 
 
////////////////// 
// Set notification window. It must created already. 
// 
void CTrayIcon2::SetNotificationWnd(CWnd* pNotifyWnd, UINT uCbMsg) 
{ 
	// If the following assert fails, you're probably 
	// calling me before you created your window. Oops. 
	ASSERT(pNotifyWnd==NULL || ::IsWindow(pNotifyWnd->GetSafeHwnd())); 
	m_nid.hWnd = pNotifyWnd->GetSafeHwnd(); 
 
	ASSERT(uCbMsg==0 || uCbMsg>=WM_USER); 
	m_nid.uCallbackMessage = uCbMsg; 
} 
 
////////////////// 
// This is the main variant for setting the icon. 
// Sets both the icon and tooltip from resource ID 
// To remove the icon, call SetIcon(0) 
// 
BOOL CTrayIcon2::SetIcon(UINT uID) 
{  
	HICON hicon=NULL; 
	if (uID) { 
		AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip)); 
		hicon = AfxGetApp()->LoadIcon(uID); 
	} 
	return SetIcon(hicon, NULL); 
} 
 
////////////////// 
// Common SetIcon for all overloads.  
// 
BOOL CTrayIcon2::SetIcon(HICON hicon, LPCSTR lpTip)  
{ 
	UINT msg; 
	m_nid.uFlags = 0; 
 
	// Set the icon 
	if (hicon) { 
		// Add or replace icon in system tray 
		msg = m_nid.hIcon ? NIM_MODIFY : NIM_ADD; 
		m_nid.hIcon = hicon; 
		m_nid.uFlags |= NIF_ICON; 
	} else { // remove icon from tray 
		if (m_nid.hIcon==NULL) 
			return TRUE;		// already deleted 
		msg = NIM_DELETE; 
	} 
 
	// Use the tip, if any 
	if (lpTip) 
		strncpy(m_nid.szTip, lpTip, sizeof(m_nid.szTip)); 
	if (m_nid.szTip[0]) 
		m_nid.uFlags |= NIF_TIP; 
 
	// Use callback if any 
	if (m_nid.uCallbackMessage && m_nid.hWnd) 
		m_nid.uFlags |= NIF_MESSAGE; 
 
	// Do it 
	BOOL bRet = Shell_NotifyIcon(msg, &m_nid); 
	if (msg==NIM_DELETE || !bRet) 
		m_nid.hIcon = NULL;	// failed 
	return bRet; 
} 
 
///////////////// 
// Default event handler handles right-menu and doubleclick. 
// Call this function from your own notification handler. 
// 
LRESULT CTrayIcon2::OnTrayNotification(WPARAM wID, LPARAM lEvent) 
{ 
// NOTE : The original code has been moved to the CMainFrame class, 
// because of my need to change some parts of it. The class name  
// is changed to CTrayIcon22, but it is a minor change... 
	if (wID!=m_nid.uID || 
		(lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK)) 
		return 0; 
 
    return 1; // handled 
}