📄 systemtraysdk.cpp
字号:
return FALSE;
BOOL returnCode = SetIcon(hIcon);
::DestroyIcon(hIcon);
return returnCode;
}
BOOL CSystemTray::SetIcon(UINT nIDResource)
{
HICON hIcon = (HICON) ::LoadImage(m_hInstance,
MAKEINTRESOURCE(nIDResource),
IMAGE_ICON,
0, 0,
LR_DEFAULTCOLOR);
BOOL returnCode = SetIcon(hIcon);
::DestroyIcon(hIcon);
return returnCode;
}
BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName)
{
HICON hIcon = ::LoadIcon(NULL, lpIconName);
return SetIcon(hIcon);
}
BOOL CSystemTray::SetStandardIcon(UINT nIDResource)
{
HICON hIcon = ::LoadIcon(NULL, MAKEINTRESOURCE(nIDResource));
return SetIcon(hIcon);
}
HICON CSystemTray::GetIcon() const
{
return (m_bEnabled)? m_tnd.hIcon : NULL;
}
BOOL CSystemTray::SetIconList(UINT uFirstIconID, UINT uLastIconID)
{
if (uFirstIconID > uLastIconID)
return FALSE;
UINT uIconArraySize = uLastIconID - uFirstIconID + 1;
m_IconList.clear();
try
{
for (UINT i = uFirstIconID; i <= uLastIconID; i++)
m_IconList.push_back(::LoadIcon(m_hInstance, MAKEINTRESOURCE(i)));
}
catch (...)
{
m_IconList.clear();
return FALSE;
}
return TRUE;
}
BOOL CSystemTray::SetIconList(HICON* pHIconList, UINT nNumIcons)
{
m_IconList.clear();
try {
for (UINT i = 0; i <= nNumIcons; i++)
m_IconList.push_back(pHIconList[i]);
}
catch (...)
{
m_IconList.clear();
return FALSE;
}
return TRUE;
}
BOOL CSystemTray::Animate(UINT nDelayMilliSeconds, int nNumSeconds /*=-1*/)
{
if (m_IconList.empty())
return FALSE;
StopAnimation();
m_nCurrentIcon = 0;
time(&m_StartTime);
m_nAnimationPeriod = nNumSeconds;
m_hSavedIcon = GetIcon();
// Setup a timer for the animation
m_uIDTimer = ::SetTimer(m_hWnd, m_nTimerID, nDelayMilliSeconds, NULL);
return (m_uIDTimer != 0);
}
BOOL CSystemTray::StepAnimation()
{
if (!m_IconList.size())
return FALSE;
m_nCurrentIcon++;
if (m_nCurrentIcon >= m_IconList.size())
m_nCurrentIcon = 0;
return SetIcon(m_IconList[m_nCurrentIcon]);
}
BOOL CSystemTray::StopAnimation()
{
BOOL bResult = FALSE;
if (m_uIDTimer)
bResult = ::KillTimer(m_hWnd, m_uIDTimer);
m_uIDTimer = 0;
if (m_hSavedIcon)
SetIcon(m_hSavedIcon);
m_hSavedIcon = NULL;
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray tooltip text manipulation
BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip)
{
ASSERT(_tcslen(pszTip) < m_nMaxTooltipLength);
if (!m_bEnabled)
return FALSE;
m_tnd.uFlags = NIF_TIP;
_tcsncpy(m_tnd.szTip, pszTip, m_nMaxTooltipLength-1);
if (m_bHidden)
return TRUE;
else
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
BOOL CSystemTray::SetTooltipText(UINT nID)
{
TCHAR strBuffer[1024];
ASSERT(1024 >= m_nMaxTooltipLength);
if (!LoadString(m_hInstance, nID, strBuffer, m_nMaxTooltipLength-1))
return FALSE;
return SetTooltipText(strBuffer);
}
LPTSTR CSystemTray::GetTooltipText() const
{
if (!m_bEnabled)
return FALSE;
static TCHAR strBuffer[1024];
ASSERT(1024 >= m_nMaxTooltipLength);
#ifdef _UNICODE
strBuffer[0] = _T('\0');
MultiByteToWideChar(CP_ACP, 0, m_tnd.szTip, -1, strBuffer, m_nMaxTooltipLength, NULL, NULL);
#else
strncpy(strBuffer, m_tnd.szTip, m_nMaxTooltipLength-1);
#endif
return strBuffer;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: ShowBalloon
//
// Description:
// Shows a balloon tooltip over the tray icon.
//
// Input:
// szText: [in] Text for the balloon tooltip.
// szTitle: [in] Title for the balloon. This text is shown in bold above
// the tooltip text (szText). Pass "" if you don't want a title.
// dwIcon: [in] Specifies an icon to 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)
// uTimeout: [in] Number of seconds for the balloon to remain visible. Can
// be between 10 and 30 inclusive.
//
// Returns:
// TRUE if successful, FALSE if not.
//
//////////////////////////////////////////////////////////////////////////
// Added by Michael Dunn, November 1999
//////////////////////////////////////////////////////////////////////////
BOOL CSystemTray::ShowBalloon(LPCTSTR szText,
LPCTSTR szTitle /*=NULL*/,
DWORD dwIcon /*=NIIF_NONE*/,
UINT uTimeout /*=10*/ )
{
#ifndef SYSTEMTRAY_USEW2K
return FALSE;
#else
// Bail out if we're not on Win 2K.
if (!m_bWin2K)
return FALSE;
// Verify input parameters.
// The balloon tooltip text can be up to 255 chars long.
ASSERT(AfxIsValidString(szText));
ASSERT(lstrlen(szText) < 256);
// The balloon title text can be up to 63 chars long.
if (szTitle)
{
ASSERT(AfxIsValidString( szTitle));
ASSERT(lstrlen(szTitle) < 64);
}
// dwBalloonIcon must be valid.
ASSERT(NIIF_NONE == dwIcon || NIIF_INFO == dwIcon ||
NIIF_WARNING == dwIcon || NIIF_ERROR == dwIcon);
// The timeout must be between 10 and 30 seconds.
ASSERT(uTimeout >= 10 && uTimeout <= 30);
m_tnd.uFlags = NIF_INFO;
_tcsncpy(m_tnd.szInfo, szText, 256);
if (szTitle)
_tcsncpy(m_tnd.szInfoTitle, szTitle, 64);
else
m_tnd.szInfoTitle[0] = _T('\0');
m_tnd.dwInfoFlags = dwIcon;
m_tnd.uTimeout = uTimeout * 1000; // convert time to ms
BOOL bSuccess = Shell_NotifyIcon (NIM_MODIFY, &m_tnd);
// Zero out the balloon text string so that later operations won't redisplay
// the balloon.
m_tnd.szInfo[0] = _T('\0');
return bSuccess;
#endif
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray notification window stuff
BOOL CSystemTray::SetNotificationWnd(HWND hNotifyWnd)
{
if (!m_bEnabled)
return FALSE;
// Make sure Notification window is valid
if (!hNotifyWnd || !::IsWindow(hNotifyWnd))
{
ASSERT(FALSE);
return FALSE;
}
m_tnd.hWnd = hNotifyWnd;
m_tnd.uFlags = 0;
if (m_bHidden)
return TRUE;
else
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
HWND CSystemTray::GetNotificationWnd() const
{
return m_tnd.hWnd;
}
// Hatr added
// Change or retrive the window to send menu commands to
BOOL CSystemTray::SetTargetWnd(HWND hTargetWnd)
{
m_hTargetWnd = hTargetWnd;
return TRUE;
} // CSystemTray::SetTargetWnd()
HWND CSystemTray::GetTargetWnd() const
{
if (m_hTargetWnd)
return m_hTargetWnd;
else
return m_tnd.hWnd;
} // CSystemTray::GetTargetWnd()
/////////////////////////////////////////////////////////////////////////////
// CSystemTray notification message stuff
BOOL CSystemTray::SetCallbackMessage(UINT uCallbackMessage)
{
if (!m_bEnabled)
return FALSE;
// Make sure we avoid conflict with other messages
ASSERT(uCallbackMessage >= WM_APP);
m_tnd.uCallbackMessage = uCallbackMessage;
m_tnd.uFlags = NIF_MESSAGE;
if (m_bHidden)
return TRUE;
else
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
UINT CSystemTray::GetCallbackMessage() const
{
return m_tnd.uCallbackMessage;
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray menu manipulation
BOOL CSystemTray::SetMenuDefaultItem(UINT uItem, BOOL bByPos)
{
#ifdef _WIN32_WCE
return FALSE;
#else
if ((m_DefaultMenuItemID == uItem) && (m_DefaultMenuItemByPos == bByPos))
return TRUE;
m_DefaultMenuItemID = uItem;
m_DefaultMenuItemByPos = bByPos;
HMENU hMenu = ::LoadMenu(m_hInstance, MAKEINTRESOURCE(m_tnd.uID));
if (!hMenu)
return FALSE;
HMENU hSubMenu = ::GetSubMenu(hMenu, 0);
if (!hSubMenu)
{
::DestroyMenu(hMenu);
return FALSE;
}
::SetMenuDefaultItem(hSubMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
::DestroyMenu(hSubMenu);
::DestroyMenu(hMenu);
return TRUE;
#endif
}
void CSystemTray::GetMenuDefaultItem(UINT& uItem, BOOL& bByPos)
{
uItem = m_DefaultMenuItemID;
bByPos = m_DefaultMenuItemByPos;
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray message handlers
/* If we were in MFC this is what we'd use...
BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
//{{AFX_MSG_MAP(CSystemTray)
ON_WM_TIMER()
//}}AFX_MSG_MAP
#ifndef _WIN32_WCE
ON_WM_SETTINGCHANGE()
#endif
ON_REGISTERED_MESSAGE(WM_TASKBARCREATED, OnTaskbarCreated)
END_MESSAGE_MAP()
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -