📄 systemtraysdk.cpp
字号:
LRESULT CSystemTray::OnTimer(UINT nIDEvent)
{
if (nIDEvent != m_uIDTimer)
{
ASSERT(FALSE);
return 0L;
}
time_t CurrentTime;
time(&CurrentTime);
time_t period = CurrentTime - m_StartTime;
if (m_nAnimationPeriod > 0 && m_nAnimationPeriod < period)
{
StopAnimation();
return 0L;
}
StepAnimation();
return 0L;
}
// This is called whenever the taskbar is created (eg after explorer crashes
// and restarts. Please note that the WM_TASKBARCREATED message is only passed
// to TOP LEVEL windows (like WM_QUERYNEWPALETTE)
LRESULT CSystemTray::OnTaskbarCreated(WPARAM wParam, LPARAM lParam)
{
InstallIconPending();
return 0L;
}
#ifndef _WIN32_WCE
LRESULT CSystemTray::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
if (uFlags == SPI_SETWORKAREA)
InstallIconPending();
return 0L;
}
#endif
LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam)
{
//Return quickly if its not for this tray icon
if (wParam != m_tnd.uID)
return 0L;
HWND hTargetWnd = GetTargetWnd();
if (!hTargetWnd)
return 0L;
// Clicking with right button brings up a context menu
#if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
BOOL bAltPressed = ((GetKeyState(VK_MENU) & (1 << (sizeof(SHORT)*8-1))) != 0);
if (LOWORD(lParam) == WM_LBUTTONUP && bAltPressed)
#else
if (LOWORD(lParam) == WM_RBUTTONUP)
#endif
{
HMENU hMenu = ::LoadMenu(m_hInstance, MAKEINTRESOURCE(m_tnd.uID));
if (!hMenu)
return 0;
HMENU hSubMenu = ::GetSubMenu(hMenu, 0);
if (!hSubMenu)
{
::DestroyMenu(hMenu); //Be sure to Destroy Menu Before Returning
return 0;
}
#ifndef _WIN32_WCE
// Make chosen menu item the default (bold font)
::SetMenuDefaultItem(hSubMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
#endif
CustomizeMenu(hSubMenu);
// Display and track the popup menu
POINT pos;
#ifdef _WIN32_WCE
DWORD messagepos = ::GetMessagePos();
pos.x = GET_X_LPARAM(messagepos);
pos.y = GET_Y_LPARAM(messagepos);
#else
GetCursorPos(&pos);
#endif
::SetForegroundWindow(m_tnd.hWnd);
::TrackPopupMenu(hSubMenu, 0, pos.x, pos.y, 0, hTargetWnd, NULL);
// BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
::PostMessage(m_tnd.hWnd, WM_NULL, 0, 0);
DestroyMenu(hMenu);
}
#if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
if (LOWORD(lParam) == WM_LBUTTONDBLCLK && bAltPressed)
#else
else if (LOWORD(lParam) == WM_LBUTTONDBLCLK)
#endif
{
// double click received, the default action is to execute default menu item
::SetForegroundWindow(m_tnd.hWnd);
UINT uItem;
if (m_DefaultMenuItemByPos)
{
HMENU hMenu = ::LoadMenu(m_hInstance, MAKEINTRESOURCE(m_tnd.uID));
if (!hMenu)
return 0;
HMENU hSubMenu = ::GetSubMenu(hMenu, 0);
if (!hSubMenu)
return 0;
uItem = ::GetMenuItemID(hSubMenu, m_DefaultMenuItemID);
DestroyMenu(hMenu);
}
else
uItem = m_DefaultMenuItemID;
::PostMessage(hTargetWnd, WM_COMMAND, uItem, 0);
}
return 1;
}
// This is the global (static) callback function for all TrayIcon windows
LRESULT PASCAL CSystemTray::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// The option here is to maintain a list of all TrayIcon windows,
// and iterate through them. If you do this, remove these 3 lines.
CSystemTray* pTrayIcon = m_pThis;
if (pTrayIcon->GetSafeHwnd() != hWnd)
return ::DefWindowProc(hWnd, message, wParam, lParam);
// If maintaining a list of TrayIcon windows, then the following...
// pTrayIcon = GetFirstTrayIcon()
// while (pTrayIcon != NULL)
// {
// if (pTrayIcon->GetSafeHwnd() != hWnd) continue;
// Taskbar has been recreated - all TrayIcons must process this.
if (message == CSystemTray::m_nTaskbarCreatedMsg)
return pTrayIcon->OnTaskbarCreated(wParam, lParam);
// Animation timer
if (message == WM_TIMER && wParam == pTrayIcon->GetTimerID())
return pTrayIcon->OnTimer(wParam);
// Settings changed
if (message == WM_SETTINGCHANGE && wParam == pTrayIcon->GetTimerID())
return pTrayIcon->OnSettingChange(wParam, (LPCTSTR) lParam);
// Is the message from the icon for this TrayIcon?
if (message == pTrayIcon->GetCallbackMessage())
return pTrayIcon->OnTrayNotification(wParam, lParam);
// pTrayIcon = GetNextTrayIcon();
// }
// Message has not been processed, so default.
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
void CSystemTray::InstallIconPending()
{
// Is the icon display pending, and it's not been set as "hidden"?
if (!m_bShowIconPending || m_bHidden)
return;
// Reset the flags to what was used at creation
m_tnd.uFlags = m_uCreationFlags;
// Try and recreate the icon
m_bHidden = !Shell_NotifyIcon(NIM_ADD, &m_tnd);
// If it's STILL hidden, then have another go next time...
m_bShowIconPending = !m_bHidden;
ASSERT(m_bHidden == FALSE);
}
/////////////////////////////////////////////////////////////////////////////
// For minimising/maximising from system tray
BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
{
TCHAR szClassName[256];
GetClassName(hwnd, szClassName, 255);
// Did we find the Main System Tray? If so, then get its size and keep going
if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
{
LPRECT lpRect = (LPRECT) lParam;
::GetWindowRect(hwnd, lpRect);
return TRUE;
}
// Did we find the System Clock? If so, then adjust the size of the rectangle
// we have and quit (clock will be found after the system tray)
if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
{
LPRECT lpRect = (LPRECT) lParam;
RECT rectClock;
::GetWindowRect(hwnd, &rectClock);
// if clock is above system tray adjust accordingly
if (rectClock.bottom < lpRect->bottom-5) // 10 = random fudge factor.
lpRect->top = rectClock.bottom;
else
lpRect->right = rectClock.left;
return FALSE;
}
return TRUE;
}
#ifndef _WIN32_WCE
void CSystemTray::GetTrayWndRect(LPRECT lprect)
{
#define DEFAULT_RECT_WIDTH 150
#define DEFAULT_RECT_HEIGHT 30
HWND hShellTrayWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
if (hShellTrayWnd)
{
GetWindowRect(hShellTrayWnd, lprect);
EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect);
return;
}
// OK, we failed to get the rect from the quick hack. Either explorer isn't
// running or it's a new version of the shell with the window class names
// changed (how dare Microsoft change these undocumented class names!) So, we
// try to find out what side of the screen the taskbar is connected to. We
// know that the system tray is either on the right or the bottom of the
// taskbar, so we can make a good guess at where to minimize to
APPBARDATA appBarData;
appBarData.cbSize=sizeof(appBarData);
if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData))
{
// We know the edge the taskbar is connected to, so guess the rect of the
// system tray. Use various fudge factor to make it look good
switch(appBarData.uEdge)
{
case ABE_LEFT:
case ABE_RIGHT:
// We want to minimize to the bottom of the taskbar
lprect->top = appBarData.rc.bottom-100;
lprect->bottom = appBarData.rc.bottom-16;
lprect->left = appBarData.rc.left;
lprect->right = appBarData.rc.right;
break;
case ABE_TOP:
case ABE_BOTTOM:
// We want to minimize to the right of the taskbar
lprect->top = appBarData.rc.top;
lprect->bottom = appBarData.rc.bottom;
lprect->left = appBarData.rc.right-100;
lprect->right = appBarData.rc.right-16;
break;
}
return;
}
// Blimey, we really aren't in luck. It's possible that a third party shell
// is running instead of explorer. This shell might provide support for the
// system tray, by providing a Shell_TrayWnd window (which receives the
// messages for the icons) So, look for a Shell_TrayWnd window and work out
// the rect from that. Remember that explorer's taskbar is the Shell_TrayWnd,
// and stretches either the width or the height of the screen. We can't rely
// on the 3rd party shell's Shell_TrayWnd doing the same, in fact, we can't
// rely on it being any size. The best we can do is just blindly use the
// window rect, perhaps limiting the width and height to, say 150 square.
// Note that if the 3rd party shell supports the same configuraion as
// explorer (the icons hosted in NotifyTrayWnd, which is a child window of
// Shell_TrayWnd), we would already have caught it above
if (hShellTrayWnd)
{
::GetWindowRect(hShellTrayWnd, lprect);
if (lprect->right - lprect->left > DEFAULT_RECT_WIDTH)
lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
if (lprect->bottom - lprect->top > DEFAULT_RECT_HEIGHT)
lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
return;
}
// OK. Haven't found a thing. Provide a default rect based on the current work
// area
SystemParametersInfo(SPI_GETWORKAREA,0,lprect, 0);
lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
}
// Check to see if the animation has been disabled (Matthew Ellis <m.t.ellis@bigfoot.com>)
BOOL CSystemTray::GetDoWndAnimation()
{
ANIMATIONINFO ai;
ai.cbSize=sizeof(ai);
SystemParametersInfo(SPI_GETANIMATION,sizeof(ai),&ai,0);
return ai.iMinAnimate?TRUE:FALSE;
}
#endif
BOOL CSystemTray::RemoveTaskbarIcon(HWND hWnd)
{
// Create static invisible window
if (!::IsWindow(m_hWndInvisible))
{
m_hWndInvisible = CreateWindowEx(0, "Static", _T(""), WS_POPUP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, 0, NULL, 0);
if (!m_hWndInvisible)
return FALSE;
}
SetParent(hWnd, m_hWndInvisible);
return TRUE;
}
void CSystemTray::MinimiseToTray(HWND hWnd)
{
#ifndef _WIN32_WCE
if (GetDoWndAnimation())
{
RECT rectFrom, rectTo;
GetWindowRect(hWnd, &rectFrom);
GetTrayWndRect(&rectTo);
DrawAnimatedRects(hWnd, IDANI_CAPTION, &rectFrom, &rectTo);
}
RemoveTaskbarIcon(hWnd);
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) &~ WS_VISIBLE);
#endif
}
void CSystemTray::MaximiseFromTray(HWND hWnd)
{
#ifndef _WIN32_WCE
if (GetDoWndAnimation())
{
RECT rectTo;
::GetWindowRect(hWnd, &rectTo);
RECT rectFrom;
GetTrayWndRect(&rectFrom);
::SetParent(hWnd, NULL);
DrawAnimatedRects(hWnd, IDANI_CAPTION, &rectFrom, &rectTo);
}
else
::SetParent(hWnd, NULL);
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) | WS_VISIBLE);
RedrawWindow(hWnd, NULL, NULL, RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_FRAME |
RDW_INVALIDATE | RDW_ERASE);
// Move focus away and back again to ensure taskbar icon is recreated
if (::IsWindow(m_hWndInvisible))
SetActiveWindow(m_hWndInvisible);
SetActiveWindow(hWnd);
SetForegroundWindow(hWnd);
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -