📄 systemtray.cpp
字号:
BOOL CSystemTray::ShowIcon()
{
if (m_bRemoved)
return AddIcon();
if (!m_bHidden)
return TRUE;
#ifdef SYSTEMTRAY_USEW2K
if (m_bWin2K)
{
m_tnd.uFlags = NIF_STATE;
m_tnd.dwState = 0;
m_tnd.dwStateMask = NIS_HIDDEN;
Shell_NotifyIcon ( NIM_MODIFY, &m_tnd );
}
else
#endif
AddIcon();
return (m_bHidden == FALSE);
}
BOOL CSystemTray::SetIcon(HICON hIcon)
{
if (!m_bEnabled)
return FALSE;
m_tnd.uFlags = NIF_ICON;
m_tnd.hIcon = hIcon;
if (m_bHidden)
return TRUE;
else
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName)
{
HICON hIcon = (HICON) ::LoadImage(AfxGetResourceHandle(),
lpszIconName,
IMAGE_ICON,
0, 0,
LR_DEFAULTCOLOR);
return SetIcon(hIcon);
}
BOOL CSystemTray::SetIcon(UINT nIDResource)
{
return SetIcon(MAKEINTRESOURCE(nIDResource));
}
BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName)
{
HICON hIcon = LoadIcon(NULL, lpIconName);
return SetIcon(hIcon);
}
BOOL CSystemTray::SetStandardIcon(UINT nIDResource)
{
return SetStandardIcon(MAKEINTRESOURCE(nIDResource));
}
HICON CSystemTray::GetIcon() const
{
return (m_bEnabled)? m_tnd.hIcon : NULL;
}
BOOL CSystemTray::SetIconList(UINT uFirstIconID, UINT uLastIconID)
{
if (uFirstIconID > uLastIconID)
return FALSE;
const CWinApp* pApp = AfxGetApp();
if (!pApp)
{
ASSERT(FALSE);
return FALSE;
}
m_IconList.RemoveAll();
TRY {
for (UINT i = uFirstIconID; i <= uLastIconID; i++)
m_IconList.Add(pApp->LoadIcon(i));
}
CATCH(CMemoryException, e)
{
e->ReportError();
e->Delete();
m_IconList.RemoveAll();
return FALSE;
}
END_CATCH
return TRUE;
}
BOOL CSystemTray::SetIconList(HICON* pHIconList, UINT nNumIcons)
{
m_IconList.RemoveAll();
TRY {
for (UINT i = 0; i <= nNumIcons; i++)
m_IconList.Add(pHIconList[i]);
}
CATCH (CMemoryException, e)
{
e->ReportError();
e->Delete();
m_IconList.RemoveAll();
return FALSE;
}
END_CATCH
return TRUE;
}
BOOL CSystemTray::Animate(UINT nDelayMilliSeconds, int nNumSeconds /*=-1*/)
{
StopAnimation();
m_nCurrentIcon = 0;
m_StartTime = COleDateTime::GetCurrentTime();
m_nAnimationPeriod = nNumSeconds;
m_hSavedIcon = GetIcon();
// Setup a timer for the animation
m_uIDTimer = SetTimer(m_nTimerID, nDelayMilliSeconds, NULL);
return (m_uIDTimer != 0);
}
BOOL CSystemTray::StepAnimation()
{
if (!m_IconList.GetSize())
return FALSE;
m_nCurrentIcon++;
if (m_nCurrentIcon >= m_IconList.GetSize())
m_nCurrentIcon = 0;
return SetIcon(m_IconList[m_nCurrentIcon]);
}
BOOL CSystemTray::StopAnimation()
{
BOOL bResult = FALSE;
if (m_uIDTimer)
bResult = KillTimer(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(AfxIsValidString(pszTip)); // (md)
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)
{
CString strText;
VERIFY(strText.LoadString(nID));
return SetTooltipText(strText);
}
CString CSystemTray::GetTooltipText() const
{
CString strText;
if (m_bEnabled)
strText = m_tnd.szTip;
return strText;
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray support for Win 2K features.
//////////////////////////////////////////////////////////////////////////
//
// 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(CWnd* pWnd)
{
if (!m_bEnabled)
return FALSE;
// Make sure Notification window is valid
if (!pWnd || !::IsWindow(pWnd->GetSafeHwnd()))
{
ASSERT(FALSE);
return FALSE;
}
m_tnd.hWnd = pWnd->GetSafeHwnd();
m_tnd.uFlags = 0;
if (m_bHidden)
return TRUE;
else
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
CWnd* CSystemTray::GetNotificationWnd() const
{
return CWnd::FromHandle(m_tnd.hWnd);
}
// Hatr added
// Hatr added
// Change or retrive the window to send menu commands to
BOOL CSystemTray::SetTargetWnd(CWnd* pTargetWnd)
{
m_pTargetWnd = pTargetWnd;
return TRUE;
} // CSystemTray::SetTargetWnd()
CWnd* CSystemTray::GetTargetWnd() const
{
if (m_pTargetWnd)
return m_pTargetWnd;
else
return AfxGetMainWnd();
} // 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;
CMenu menu, *pSubMenu;
if (!menu.LoadMenu(m_tnd.uID))
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -