/////////////////////////////////////////////////////////////////////////////
// SystemTray.cpp : implementation file
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SystemTray.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifndef _WIN32_WCE // Use C++ exception handling instead of structured.
#undef TRY
#undef CATCH
#undef END_CATCH
#define TRY try
#define CATCH(ex_class, ex_object) catch(ex_class* ex_object)
#define END_CATCH
#endif // _WIN32_WCE
#ifndef _countof
#define _countof(x) ( sizeof(x) / sizeof(x[0]) )
#endif
IMPLEMENT_DYNAMIC(CSystemTray, CWnd)
const UINT CSystemTray::m_nTimerID = 4567;
UINT CSystemTray::m_nMaxTooltipLength = 64; // This may change...
const UINT CSystemTray::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated"));
CWnd CSystemTray::m_wndInvisible;
/////////////////////////////////////////////////////////////////////////////
// CSystemTray construction/creation/destruction
CSystemTray::CSystemTray()
{
Initialise();
}
CSystemTray::CSystemTray(CWnd* pParent, // The window that will recieve tray notifications
UINT uCallbackMessage, // the callback message to send to parent
LPCTSTR szToolTip, // tray icon tooltip
HICON icon, // Handle to icon
UINT uID, // Identifier of tray icon
BOOL bHidden /*=FALSE*/, // Hidden on creation?
LPCTSTR szBalloonTip /*=NULL*/, // Ballon tip (w2k only)
LPCTSTR szBalloonTitle /*=NULL*/, // Balloon tip title (w2k)
DWORD dwBalloonIcon /*=NIIF_NONE*/,// Ballon tip icon (w2k)
UINT uBalloonTimeout /*=10*/) // Balloon timeout (w2k)
{
Initialise();
Create(pParent, uCallbackMessage, szToolTip, icon, uID, bHidden,
szBalloonTip, szBalloonTitle, dwBalloonIcon, uBalloonTimeout);
}
void CSystemTray::Initialise()
{
memset(&m_tnd, 0, sizeof(m_tnd));
m_bEnabled = FALSE;
m_bHidden = TRUE;
m_bRemoved = TRUE;
m_DefaultMenuItemID = 0;
m_DefaultMenuItemByPos = TRUE;
m_bShowIconPending = FALSE;
m_uIDTimer = 0;
m_hSavedIcon = NULL;
m_pTargetWnd = NULL;
m_uCreationFlags = 0;
#ifdef SYSTEMTRAY_USEW2K
OSVERSIONINFO os = { sizeof(os) };
GetVersionEx(&os);
m_bWin2K = ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 5 );
#else
m_bWin2K = FALSE;
#endif
}
/////////////////////////////////////////////////////////////////////////////
// szBalloonTip: Text for a balloon tooltip that is shown when the icon
// is first added to the tray. Pass "" if you don't want
// a balloon.
// szBalloonTitle: Title text for the balloon tooltip. This text is shown
// in bold above the szBalloonTip text. Pass "" if you
// don't want a title.
// dwBalloonIcon: Specifies which icon will appear in the balloon. Legal
// values are:
// NIIF_NONE: No icon
// NIIF_INFO: Information
// NIIF_WARNING: Exclamation
// NIIF_ERROR: Critical error (red circle with X)
// uBalloonTimeout: Number of seconds for the balloon to remain visible.
// Must be between 10 and 30 inclusive.
BOOL CSystemTray::Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szToolTip,
HICON icon, UINT uID, BOOL bHidden /*=FALSE*/,
LPCTSTR szBalloonTip /*=NULL*/,
LPCTSTR szBalloonTitle /*=NULL*/,
DWORD dwBalloonIcon /*=NIIF_NONE*/,
UINT uBalloonTimeout /*=10*/)
{
#ifdef _WIN32_WCE
m_bEnabled = TRUE;
#else
// this is only for Windows 95 (or higher)
m_bEnabled = (GetVersion() & 0xff) >= 4;
if (!m_bEnabled)
{
ASSERT(FALSE);
return FALSE;
}
#endif
m_nMaxTooltipLength = _countof(m_tnd.szTip);
// Make sure we avoid conflict with other messages
ASSERT(uCallbackMessage >= WM_APP);
// Tray only supports tooltip text up to m_nMaxTooltipLength) characters
ASSERT(AfxIsValidString(szToolTip));
ASSERT(_tcslen(szToolTip) <= m_nMaxTooltipLength);
// Create an invisible window
CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,0,0, NULL, 0);
// load up the NOTIFYICONDATA structure
m_tnd.cbSize = sizeof(NOTIFYICONDATA);
m_tnd.hWnd = pParent->GetSafeHwnd()? pParent->GetSafeHwnd() : m_hWnd;
m_tnd.uID = uID;
m_tnd.hIcon = icon;
m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_tnd.uCallbackMessage = uCallbackMessage;
_tcsncpy(m_tnd.szTip, szToolTip, m_nMaxTooltipLength-1);
#ifdef SYSTEMTRAY_USEW2K
if (m_bWin2K && szBalloonTip)
{
// The balloon tooltip text can be up to 255 chars long.
ASSERT(AfxIsValidString(szBalloonTip));
ASSERT(lstrlen(szBalloonTip) < 256);
// The balloon title text can be up to 63 chars long.
if (szBalloonTitle)
{
ASSERT(AfxIsValidString(szBalloonTitle));
ASSERT(lstrlen(szBalloonTitle) < 64);
}
// dwBalloonIcon must be valid.
ASSERT(NIIF_NONE == dwBalloonIcon || NIIF_INFO == dwBalloonIcon ||
NIIF_WARNING == dwBalloonIcon || NIIF_ERROR == dwBalloonIcon);
// The timeout must be between 10 and 30 seconds.
ASSERT(uBalloonTimeout >= 10 && uBalloonTimeout <= 30);
m_tnd.uFlags |= NIF_INFO;
_tcsncpy(m_tnd.szInfo, szBalloonTip, 255);
if (szBalloonTitle)
_tcsncpy(m_tnd.szInfoTitle, szBalloonTitle, 63);
else
m_tnd.szInfoTitle[0] = _T('\0');
m_tnd.uTimeout = uBalloonTimeout * 1000; // convert time to ms
m_tnd.dwInfoFlags = dwBalloonIcon;
}
#endif
m_bHidden = bHidden;
#ifdef SYSTEMTRAY_USEW2K
if (m_bWin2K && m_bHidden)
{
m_tnd.uFlags = NIF_STATE;
m_tnd.dwState = NIS_HIDDEN;
m_tnd.dwStateMask = NIS_HIDDEN;
}
#endif
m_uCreationFlags = m_tnd.uFlags; // Store in case we need to recreate in OnTaskBarCreate
BOOL bResult = TRUE;
if (!m_bHidden || m_bWin2K)
{
bResult = Shell_NotifyIcon(NIM_ADD, &m_tnd);
m_bShowIconPending = m_bHidden = m_bRemoved = !bResult;
}
#ifdef SYSTEMTRAY_USEW2K
if (m_bWin2K && szBalloonTip)
{
// Zero out the balloon text string so that later operations won't redisplay
// the balloon.
m_tnd.szInfo[0] = _T('\0');
}
#endif
return bResult;
}
CSystemTray::~CSystemTray()
{
RemoveIcon();
m_IconList.RemoveAll();
DestroyWindow();
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray icon manipulation
//////////////////////////////////////////////////////////////////////////
//
// Function: SetFocus()
//
// Description:
// Sets the focus to the tray icon. Microsoft's Win 2K UI guidelines
// say you should do this after the user dismisses the icon's context
// menu.
//
// Input:
// Nothing.
//
// Returns:
// Nothing.
//
//////////////////////////////////////////////////////////////////////////
// Added by Michael Dunn, November, 1999
//////////////////////////////////////////////////////////////////////////
void CSystemTray::SetFocus()
{
#ifdef SYSTEMTRAY_USEW2K
Shell_NotifyIcon ( NIM_SETFOCUS, &m_tnd );
#endif
}
BOOL CSystemTray::MoveToRigh