?? systemtray.cpp
字號:
pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return FALSE;
::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
return TRUE;
#endif
}
void CSystemTray::GetMenuDefaultItem(UINT& uItem, BOOL& bByPos)
{
uItem = m_DefaultMenuItemID;
bByPos = m_DefaultMenuItemByPos;
}
/////////////////////////////////////////////////////////////////////////////
// CSystemTray message handlers
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(CSystemTray::m_nTaskbarCreatedMsg, OnTaskbarCreated)
END_MESSAGE_MAP()
void CSystemTray::OnTimer(UINT nIDEvent)
{
if (nIDEvent != m_uIDTimer)
{
ASSERT(FALSE);
return;
}
COleDateTime CurrentTime = COleDateTime::GetCurrentTime();
COleDateTimeSpan period = CurrentTime - m_StartTime;
if (m_nAnimationPeriod > 0 && m_nAnimationPeriod < period.GetTotalSeconds())
{
StopAnimation();
return;
}
StepAnimation();
}
// 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)
void CSystemTray::OnTaskbarCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
InstallIconPending();
}
#ifndef _WIN32_WCE
void CSystemTray::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
CWnd::OnSettingChange(uFlags, lpszSection);
if (uFlags == SPI_SETWORKAREA)
InstallIconPending();
}
#endif
LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam)
{
//Return quickly if its not for this tray icon
if (wParam != m_tnd.uID)
return 0L;
CMenu menu, *pSubMenu;
CWnd *pTargetWnd = GetTargetWnd();
if (!pTargetWnd)
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
{
if (!menu.LoadMenu(m_tnd.uID))
return 0;
pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return 0;
#ifndef _WIN32_WCE
// Make chosen menu item the default (bold font)
::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
#endif
// Display and track the popup menu
CPoint pos;
#ifdef _WIN32_WCE
pos = CPoint(GetMessagePos());
#else
GetCursorPos(&pos);
#endif
pTargetWnd->SetForegroundWindow();
#ifndef _WIN32_WCE
::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0,
pTargetWnd->GetSafeHwnd(), NULL);
#else
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL);
#endif
// BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
pTargetWnd->PostMessage(WM_NULL, 0, 0);
menu.DestroyMenu();
}
#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
pTargetWnd->SetForegroundWindow();
UINT uItem;
if (m_DefaultMenuItemByPos)
{
if (!menu.LoadMenu(m_tnd.uID))
return 0;
pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return 0;
uItem = pSubMenu->GetMenuItemID(m_DefaultMenuItemID);
menu.DestroyMenu();
}
else
uItem = m_DefaultMenuItemID;
pTargetWnd->SendMessage(WM_COMMAND, uItem, 0);
}
return 1;
}
LRESULT CSystemTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == m_tnd.uCallbackMessage)
return OnTrayNotification(wParam, lParam);
return CWnd::WindowProc(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)
{
CRect *pRect = (CRect*) lParam;
::GetWindowRect(hwnd, pRect);
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)
{
CRect *pRect = (CRect*) lParam;
CRect rectClock;
::GetWindowRect(hwnd, rectClock);
// if clock is above system tray adjust accordingly
if (rectClock.bottom < pRect->bottom-5) // 10 = random fudge factor.
pRect->top = rectClock.bottom;
else
pRect->right = rectClock.left;
return FALSE;
}
return TRUE;
}
#ifndef _WIN32_WCE
// enhanced version by Matthew Ellis <m.t.ellis@bigfoot.com>
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(CWnd* pWnd)
{
LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);
// Create static invisible window
if (!::IsWindow(m_wndInvisible.m_hWnd))
{
if (!m_wndInvisible.CreateEx(0, pstrOwnerClass, _T(""), WS_POPUP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, 0))
return FALSE;
}
pWnd->SetParent(&m_wndInvisible);
return TRUE;
}
void CSystemTray::MinimiseToTray(CWnd* pWnd)
{
#ifndef _WIN32_WCE
if (GetDoWndAnimation())
{
CRect rectFrom, rectTo;
pWnd->GetWindowRect(rectFrom);
GetTrayWndRect(rectTo);
DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
}
RemoveTaskbarIcon(pWnd);
pWnd->ModifyStyle(WS_VISIBLE, 0);
#endif
}
void CSystemTray::MaximiseFromTray(CWnd* pWnd)
{
#ifndef _WIN32_WCE
if (GetDoWndAnimation())
{
CRect rectTo;
pWnd->GetWindowRect(rectTo);
CRect rectFrom;
GetTrayWndRect(rectFrom);
pWnd->SetParent(NULL);
DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
}
else
pWnd->SetParent(NULL);
pWnd->ModifyStyle(0, WS_VISIBLE);
pWnd->RedrawWindow(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_wndInvisible.m_hWnd))
m_wndInvisible.SetActiveWindow();
pWnd->SetActiveWindow();
pWnd->SetForegroundWindow();
#endif
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -