?? systemtray.cpp
字號:
/////////////////////////////////////////////////////////////////////////////
// SystemTray.cpp : implementation file
//
// MFC VERSION
//
// This is a conglomeration of ideas from the MSJ "Webster" application,
// sniffing round the online docs, and from other implementations such
// as PJ Naughter's "CTrayNotifyIcon" (http://indigo.ie/~pjn/ntray.html)
// especially the "CSystemTray::OnTrayNotification" member function.
// Joerg Koenig suggested the icon animation stuff
//
// This class is a light wrapper around the windows system tray stuff. It
// adds an icon to the system tray with the specified ToolTip text and
// callback notification value, which is sent back to the Parent window.
//
// The tray icon can be instantiated using either the constructor or by
// declaring the object and creating (and displaying) it later on in the
// program. eg.
//
// CSystemTray m_SystemTray; // Member variable of some class
//
// ...
// // in some member function maybe...
// m_SystemTray.Create(pParentWnd, WM_MY_NOTIFY, "Click here",
// hIcon, nSystemTrayID);
//
// Written by Chris Maunder (cmaunder@mail.com)
// Copyright (c) 1998.
//
// Updated: 25 Jul 1998 - Added icon animation, and derived class
// from CWnd in order to handle messages. (CJM)
// (icon animation suggested by Joerg Koenig.
// Added API to set default menu item. Code provided
// by Enrico Lelina.
//
// Updated: 6 June 1999 - SetIcon can now load non-standard sized icons (Chip Calvert)
// Added "bHidden" parameter when creating icon
// (Thanks to Michael Gombar for these suggestions)
// Restricted tooltip text to 64 characters.
//
// Updated: 9 Nov 1999 - Now works in WindowsCE.
// Fix for use in NT services (Thomas Mooney, TeleProc, Inc)
// Added W2K stuff by Michael Dunn
//
// Updated: 1 Jan 2000 - Added tray minimisation stuff.
//
// Updated: 21 Sep 2000 - Added GetDoWndAnimation - animation only occurs if the system
// settings allow it (Matthew Ellis). Updated the GetTrayWndRect
// function to include more fallback logic (Matthew Ellis)
// NOTE: Signature of GetTrayWndRect has changed!
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then acknowledgement must be made to the author of this file
// (in whatever form you wish).
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage caused through use.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// 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
}
// update by Michael Dunn, November 1999
//
// New version of Create() that handles new features in Win 2K.
//
// Changes:
// szTip: Same as old, but can be 128 characters instead of 64.
// 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::MoveToRight()
{
RemoveIcon();
return AddIcon();
}
BOOL CSystemTray::AddIcon()
{
if (!m_bRemoved)
RemoveIcon();
if (m_bEnabled)
{
m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
if (!Shell_NotifyIcon(NIM_ADD, &m_tnd))
m_bShowIconPending = TRUE;
else
m_bRemoved = m_bHidden = FALSE;
}
return (m_bRemoved == FALSE);
}
BOOL CSystemTray::RemoveIcon()
{
m_bShowIconPending = FALSE;
if (!m_bEnabled || m_bRemoved)
return TRUE;
m_tnd.uFlags = 0;
if (Shell_NotifyIcon(NIM_DELETE, &m_tnd))
m_bRemoved = m_bHidden = TRUE;
return (m_bRemoved == TRUE);
}
BOOL CSystemTray::HideIcon()
{
if (!m_bEnabled || m_bRemoved || m_bHidden)
return TRUE;
#ifdef SYSTEMTRAY_USEW2K
if (m_bWin2K)
{
m_tnd.uFlags = NIF_STATE;
m_tnd.dwState = NIS_HIDDEN;
m_tnd.dwStateMask = NIS_HIDDEN;
m_bHidden = Shell_NotifyIcon( NIM_MODIFY, &m_tnd);
}
else
#endif
RemoveIcon();
return (m_bHidden == TRUE);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -