| 1 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // SystemTray.cpp : implementation file |
|---|
| 3 | // |
|---|
| 4 | // MFC VERSION |
|---|
| 5 | // |
|---|
| 6 | // This is a conglomeration of ideas from the MSJ "Webster" application, |
|---|
| 7 | // sniffing round the online docs, and from other implementations such |
|---|
| 8 | // as PJ Naughter's "CTrayNotifyIcon" (http://indigo.ie/~pjn/ntray.html) |
|---|
| 9 | // especially the "CSystemTray::OnTrayNotification" member function. |
|---|
| 10 | // Joerg Koenig suggested the icon animation stuff |
|---|
| 11 | // |
|---|
| 12 | // This class is a light wrapper around the windows system tray stuff. It |
|---|
| 13 | // adds an icon to the system tray with the specified ToolTip text and |
|---|
| 14 | // callback notification value, which is sent back to the Parent window. |
|---|
| 15 | // |
|---|
| 16 | // The tray icon can be instantiated using either the constructor or by |
|---|
| 17 | // declaring the object and creating (and displaying) it later on in the |
|---|
| 18 | // program. eg. |
|---|
| 19 | // |
|---|
| 20 | // CSystemTray m_SystemTray; // Member variable of some class |
|---|
| 21 | // |
|---|
| 22 | // ... |
|---|
| 23 | // // in some member function maybe... |
|---|
| 24 | // m_SystemTray.Create(pParentWnd, WM_MY_NOTIFY, "Click here", |
|---|
| 25 | // hIcon, nSystemTrayID); |
|---|
| 26 | // |
|---|
| 27 | // Written by Chris Maunder (cmaunder@mail.com) |
|---|
| 28 | // Copyright (c) 1998-2003. |
|---|
| 29 | // |
|---|
| 30 | // Updated: 25 Jul 1998 - Added icon animation, and derived class |
|---|
| 31 | // from CWnd in order to handle messages. (CJM) |
|---|
| 32 | // (icon animation suggested by Joerg Koenig. |
|---|
| 33 | // Added API to set default menu item. Code provided |
|---|
| 34 | // by Enrico Lelina. |
|---|
| 35 | // |
|---|
| 36 | // Updated: 6 June 1999 - SetIcon can now load non-standard sized icons (Chip Calvert) |
|---|
| 37 | // Added "bHidden" parameter when creating icon |
|---|
| 38 | // (Thanks to Michael Gombar for these suggestions) |
|---|
| 39 | // Restricted tooltip text to 64 characters. |
|---|
| 40 | // |
|---|
| 41 | // Updated: 9 Nov 1999 - Now works in WindowsCE. |
|---|
| 42 | // Fix for use in NT services (Thomas Mooney, TeleProc, Inc) |
|---|
| 43 | // Added W2K stuff by Michael Dunn |
|---|
| 44 | // |
|---|
| 45 | // Updated: 1 Jan 2000 - Added tray minimisation stuff. |
|---|
| 46 | // |
|---|
| 47 | // Updated: 21 Sep 2000 - Added GetDoWndAnimation - animation only occurs if the system |
|---|
| 48 | // settings allow it (Matthew Ellis). Updated the GetTrayWndRect |
|---|
| 49 | // function to include more fallback logic (Matthew Ellis) |
|---|
| 50 | // NOTE: Signature of GetTrayWndRect has changed! |
|---|
| 51 | // |
|---|
| 52 | // Updated: 4 Aug 2003 - Fixed bug that was stopping icon from being recreated when |
|---|
| 53 | // Explorer crashed |
|---|
| 54 | // Fixed resource leak in SetIcon |
|---|
| 55 | // Animate() now checks for empty icon list - Anton Treskunov |
|---|
| 56 | // Added the virutal CustomizeMenu() method - Anton Treskunov |
|---|
| 57 | // |
|---|
| 58 | // |
|---|
| 59 | // This code may be used in compiled form in any way you desire. This |
|---|
| 60 | // file may be redistributed unmodified by any means PROVIDING it is |
|---|
| 61 | // not sold for profit without the authors written consent, and |
|---|
| 62 | // providing that this notice and the authors name is included. If |
|---|
| 63 | // the source code in this file is used in any commercial application |
|---|
| 64 | // then acknowledgement must be made to the author of this file |
|---|
| 65 | // (in whatever form you wish). |
|---|
| 66 | // |
|---|
| 67 | // This file is provided "as is" with no expressed or implied warranty. |
|---|
| 68 | // The author accepts no liability for any damage caused through use. |
|---|
| 69 | // |
|---|
| 70 | // Expect bugs. |
|---|
| 71 | // |
|---|
| 72 | // Please use and enjoy. Please let me know of any bugs/mods/improvements |
|---|
| 73 | // that you have found/implemented and I will fix/incorporate them into this |
|---|
| 74 | // file. |
|---|
| 75 | // |
|---|
| 76 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 77 | |
|---|
| 78 | #include "stdafx.h" |
|---|
| 79 | #include "SystemTray.h" |
|---|
| 80 | |
|---|
| 81 | #ifdef _DEBUG |
|---|
| 82 | #define new DEBUG_NEW |
|---|
| 83 | #undef THIS_FILE |
|---|
| 84 | static char THIS_FILE[] = __FILE__; |
|---|
| 85 | #endif |
|---|
| 86 | |
|---|
| 87 | #ifndef _WIN32_WCE // Use C++ exception handling instead of structured. |
|---|
| 88 | #undef TRY |
|---|
| 89 | #undef CATCH |
|---|
| 90 | #undef END_CATCH |
|---|
| 91 | #define TRY try |
|---|
| 92 | #define CATCH(ex_class, ex_object) catch(ex_class* ex_object) |
|---|
| 93 | #define END_CATCH |
|---|
| 94 | #endif // _WIN32_WCE |
|---|
| 95 | |
|---|
| 96 | #ifndef _countof |
|---|
| 97 | #define _countof(x) ( sizeof(x) / sizeof(x[0]) ) |
|---|
| 98 | #endif |
|---|
| 99 | |
|---|
| 100 | IMPLEMENT_DYNAMIC(CSystemTray, CWnd) |
|---|
| 101 | |
|---|
| 102 | const UINT CSystemTray::m_nTimerID = 4567; |
|---|
| 103 | UINT CSystemTray::m_nMaxTooltipLength = 128; // This may change... |
|---|
| 104 | const UINT CSystemTray::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated")); |
|---|
| 105 | CWnd CSystemTray::m_wndInvisible; |
|---|
| 106 | |
|---|
| 107 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 108 | // CSystemTray construction/creation/destruction |
|---|
| 109 | |
|---|
| 110 | CSystemTray::CSystemTray() |
|---|
| 111 | { |
|---|
| 112 | Initialise(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | CSystemTray::CSystemTray(CWnd* pParent, // The window that will recieve tray notifications |
|---|
| 116 | UINT uCallbackMessage, // the callback message to send to parent |
|---|
| 117 | LPCTSTR szToolTip, // tray icon tooltip |
|---|
| 118 | HICON icon, // Handle to icon |
|---|
| 119 | UINT uID, // Identifier of tray icon |
|---|
| 120 | BOOL bHidden /*=FALSE*/, // Hidden on creation? |
|---|
| 121 | LPCTSTR szBalloonTip /*=NULL*/, // Ballon tip (w2k only) |
|---|
| 122 | LPCTSTR szBalloonTitle /*=NULL*/, // Balloon tip title (w2k) |
|---|
| 123 | DWORD dwBalloonIcon /*=NIIF_NONE*/,// Ballon tip icon (w2k) |
|---|
| 124 | UINT uBalloonTimeout /*=10*/) // Balloon timeout (w2k) |
|---|
| 125 | { |
|---|
| 126 | Initialise(); |
|---|
| 127 | Create(pParent, uCallbackMessage, szToolTip, icon, uID, bHidden, |
|---|
| 128 | szBalloonTip, szBalloonTitle, dwBalloonIcon, uBalloonTimeout); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void CSystemTray::Initialise() |
|---|
| 132 | { |
|---|
| 133 | memset(&m_tnd, 0, sizeof(m_tnd)); |
|---|
| 134 | |
|---|
| 135 | m_bEnabled = FALSE; |
|---|
| 136 | m_bHidden = TRUE; |
|---|
| 137 | m_bRemoved = TRUE; |
|---|
| 138 | |
|---|
| 139 | m_DefaultMenuItemID = 0; |
|---|
| 140 | m_DefaultMenuItemByPos = TRUE; |
|---|
| 141 | |
|---|
| 142 | m_bShowIconPending = FALSE; |
|---|
| 143 | |
|---|
| 144 | m_uIDTimer = 0; |
|---|
| 145 | m_hSavedIcon = NULL; |
|---|
| 146 | |
|---|
| 147 | m_pTargetWnd = NULL; |
|---|
| 148 | m_uCreationFlags = 0; |
|---|
| 149 | |
|---|
| 150 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 151 | OSVERSIONINFO os = { sizeof(os) }; |
|---|
| 152 | GetVersionEx(&os); |
|---|
| 153 | m_bWin2K = ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 5 ); |
|---|
| 154 | #else |
|---|
| 155 | m_bWin2K = FALSE; |
|---|
| 156 | #endif |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | // update by Michael Dunn, November 1999 |
|---|
| 160 | // |
|---|
| 161 | // New version of Create() that handles new features in Win 2K. |
|---|
| 162 | // |
|---|
| 163 | // Changes: |
|---|
| 164 | // szTip: Same as old, but can be 128 characters instead of 64. |
|---|
| 165 | // szBalloonTip: Text for a balloon tooltip that is shown when the icon |
|---|
| 166 | // is first added to the tray. Pass "" if you don't want |
|---|
| 167 | // a balloon. |
|---|
| 168 | // szBalloonTitle: Title text for the balloon tooltip. This text is shown |
|---|
| 169 | // in bold above the szBalloonTip text. Pass "" if you |
|---|
| 170 | // don't want a title. |
|---|
| 171 | // dwBalloonIcon: Specifies which icon will appear in the balloon. Legal |
|---|
| 172 | // values are: |
|---|
| 173 | // NIIF_NONE: No icon |
|---|
| 174 | // NIIF_INFO: Information |
|---|
| 175 | // NIIF_WARNING: Exclamation |
|---|
| 176 | // NIIF_ERROR: Critical error (red circle with X) |
|---|
| 177 | // uBalloonTimeout: Number of seconds for the balloon to remain visible. |
|---|
| 178 | // Must be between 10 and 30 inclusive. |
|---|
| 179 | |
|---|
| 180 | BOOL CSystemTray::Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szToolTip, |
|---|
| 181 | HICON icon, UINT uID, BOOL bHidden /*=FALSE*/, |
|---|
| 182 | LPCTSTR szBalloonTip /*=NULL*/, |
|---|
| 183 | LPCTSTR szBalloonTitle /*=NULL*/, |
|---|
| 184 | DWORD dwBalloonIcon /*=NIIF_NONE*/, |
|---|
| 185 | UINT uBalloonTimeout /*=10*/) |
|---|
| 186 | { |
|---|
| 187 | #ifdef _WIN32_WCE |
|---|
| 188 | m_bEnabled = TRUE; |
|---|
| 189 | #else |
|---|
| 190 | // this is only for Windows 95 (or higher) |
|---|
| 191 | m_bEnabled = (GetVersion() & 0xff) >= 4; |
|---|
| 192 | if (!m_bEnabled) |
|---|
| 193 | { |
|---|
| 194 | ASSERT(FALSE); |
|---|
| 195 | return FALSE; |
|---|
| 196 | } |
|---|
| 197 | #endif |
|---|
| 198 | |
|---|
| 199 | m_nMaxTooltipLength = _countof(m_tnd.szTip); |
|---|
| 200 | |
|---|
| 201 | // Make sure we avoid conflict with other messages |
|---|
| 202 | ASSERT(uCallbackMessage >= WM_APP); |
|---|
| 203 | |
|---|
| 204 | // Tray only supports tooltip text up to m_nMaxTooltipLength) characters |
|---|
| 205 | ASSERT(AfxIsValidString(szToolTip)); |
|---|
| 206 | ASSERT(_tcslen(szToolTip) <= m_nMaxTooltipLength); |
|---|
| 207 | |
|---|
| 208 | // Create an invisible window |
|---|
| 209 | CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,0,0, NULL, 0); |
|---|
| 210 | |
|---|
| 211 | // load up the NOTIFYICONDATA structure |
|---|
| 212 | m_tnd.cbSize = sizeof(NOTIFYICONDATA); |
|---|
| 213 | m_tnd.hWnd = pParent->GetSafeHwnd()? pParent->GetSafeHwnd() : m_hWnd; |
|---|
| 214 | m_tnd.uID = uID; |
|---|
| 215 | m_tnd.hIcon = icon; |
|---|
| 216 | m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; |
|---|
| 217 | m_tnd.uCallbackMessage = uCallbackMessage; |
|---|
| 218 | _tcsncpy(m_tnd.szTip, szToolTip, m_nMaxTooltipLength-1); |
|---|
| 219 | |
|---|
| 220 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 221 | if (m_bWin2K && szBalloonTip) |
|---|
| 222 | { |
|---|
| 223 | // The balloon tooltip text can be up to 255 chars long. |
|---|
| 224 | ASSERT(AfxIsValidString(szBalloonTip)); |
|---|
| 225 | ASSERT(lstrlen(szBalloonTip) < 256); |
|---|
| 226 | |
|---|
| 227 | // The balloon title text can be up to 63 chars long. |
|---|
| 228 | if (szBalloonTitle) |
|---|
| 229 | { |
|---|
| 230 | ASSERT(AfxIsValidString(szBalloonTitle)); |
|---|
| 231 | ASSERT(lstrlen(szBalloonTitle) < 64); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | // dwBalloonIcon must be valid. |
|---|
| 235 | ASSERT(NIIF_NONE == dwBalloonIcon || NIIF_INFO == dwBalloonIcon || |
|---|
| 236 | NIIF_WARNING == dwBalloonIcon || NIIF_ERROR == dwBalloonIcon); |
|---|
| 237 | |
|---|
| 238 | // The timeout must be between 10 and 30 seconds. |
|---|
| 239 | ASSERT(uBalloonTimeout >= 10 && uBalloonTimeout <= 30); |
|---|
| 240 | |
|---|
| 241 | m_tnd.uFlags |= NIF_INFO; |
|---|
| 242 | |
|---|
| 243 | _tcsncpy(m_tnd.szInfo, szBalloonTip, 255); |
|---|
| 244 | if (szBalloonTitle) |
|---|
| 245 | _tcsncpy(m_tnd.szInfoTitle, szBalloonTitle, 63); |
|---|
| 246 | else |
|---|
| 247 | m_tnd.szInfoTitle[0] = _T('\0'); |
|---|
| 248 | m_tnd.uTimeout = uBalloonTimeout * 1000; // convert time to ms |
|---|
| 249 | m_tnd.dwInfoFlags = dwBalloonIcon; |
|---|
| 250 | } |
|---|
| 251 | #endif |
|---|
| 252 | |
|---|
| 253 | m_bHidden = bHidden; |
|---|
| 254 | |
|---|
| 255 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 256 | if (m_bWin2K && m_bHidden) |
|---|
| 257 | { |
|---|
| 258 | m_tnd.uFlags = NIF_STATE; |
|---|
| 259 | m_tnd.dwState = NIS_HIDDEN; |
|---|
| 260 | m_tnd.dwStateMask = NIS_HIDDEN; |
|---|
| 261 | } |
|---|
| 262 | #endif |
|---|
| 263 | |
|---|
| 264 | m_uCreationFlags = m_tnd.uFlags; // Store in case we need to recreate in OnTaskBarCreate |
|---|
| 265 | |
|---|
| 266 | BOOL bResult = TRUE; |
|---|
| 267 | if (!m_bHidden || m_bWin2K) |
|---|
| 268 | { |
|---|
| 269 | bResult = Shell_NotifyIcon(NIM_ADD, &m_tnd); |
|---|
| 270 | m_bShowIconPending = m_bHidden = m_bRemoved = !bResult; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 274 | if (m_bWin2K && szBalloonTip) |
|---|
| 275 | { |
|---|
| 276 | // Zero out the balloon text string so that later operations won't redisplay |
|---|
| 277 | // the balloon. |
|---|
| 278 | m_tnd.szInfo[0] = _T('\0'); |
|---|
| 279 | } |
|---|
| 280 | #endif |
|---|
| 281 | |
|---|
| 282 | return bResult; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | CSystemTray::~CSystemTray() |
|---|
| 286 | { |
|---|
| 287 | RemoveIcon(); |
|---|
| 288 | m_IconList.RemoveAll(); |
|---|
| 289 | DestroyWindow(); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 293 | // CSystemTray icon manipulation |
|---|
| 294 | |
|---|
| 295 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 296 | // |
|---|
| 297 | // Function: SetFocus() |
|---|
| 298 | // |
|---|
| 299 | // Description: |
|---|
| 300 | // Sets the focus to the tray icon. Microsoft's Win 2K UI guidelines |
|---|
| 301 | // say you should do this after the user dismisses the icon's context |
|---|
| 302 | // menu. |
|---|
| 303 | // |
|---|
| 304 | // Input: |
|---|
| 305 | // Nothing. |
|---|
| 306 | // |
|---|
| 307 | // Returns: |
|---|
| 308 | // Nothing. |
|---|
| 309 | // |
|---|
| 310 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 311 | // Added by Michael Dunn, November, 1999 |
|---|
| 312 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 313 | |
|---|
| 314 | void CSystemTray::SetFocus() |
|---|
| 315 | { |
|---|
| 316 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 317 | Shell_NotifyIcon ( NIM_SETFOCUS, &m_tnd ); |
|---|
| 318 | #endif |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | BOOL CSystemTray::MoveToRight() |
|---|
| 322 | { |
|---|
| 323 | RemoveIcon(); |
|---|
| 324 | return AddIcon(); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | BOOL CSystemTray::AddIcon() |
|---|
| 328 | { |
|---|
| 329 | if (!m_bRemoved) |
|---|
| 330 | RemoveIcon(); |
|---|
| 331 | |
|---|
| 332 | if (m_bEnabled) |
|---|
| 333 | { |
|---|
| 334 | m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; |
|---|
| 335 | if (!Shell_NotifyIcon(NIM_ADD, &m_tnd)) |
|---|
| 336 | m_bShowIconPending = TRUE; |
|---|
| 337 | else |
|---|
| 338 | m_bRemoved = m_bHidden = FALSE; |
|---|
| 339 | } |
|---|
| 340 | return (m_bRemoved == FALSE); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | BOOL CSystemTray::RemoveIcon() |
|---|
| 344 | { |
|---|
| 345 | m_bShowIconPending = FALSE; |
|---|
| 346 | |
|---|
| 347 | if (!m_bEnabled || m_bRemoved) |
|---|
| 348 | return TRUE; |
|---|
| 349 | |
|---|
| 350 | m_tnd.uFlags = 0; |
|---|
| 351 | if (Shell_NotifyIcon(NIM_DELETE, &m_tnd)) |
|---|
| 352 | m_bRemoved = m_bHidden = TRUE; |
|---|
| 353 | |
|---|
| 354 | return (m_bRemoved == TRUE); |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | BOOL CSystemTray::HideIcon() |
|---|
| 358 | { |
|---|
| 359 | if (!m_bEnabled || m_bRemoved || m_bHidden) |
|---|
| 360 | return TRUE; |
|---|
| 361 | |
|---|
| 362 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 363 | if (m_bWin2K) |
|---|
| 364 | { |
|---|
| 365 | m_tnd.uFlags = NIF_STATE; |
|---|
| 366 | m_tnd.dwState = NIS_HIDDEN; |
|---|
| 367 | m_tnd.dwStateMask = NIS_HIDDEN; |
|---|
| 368 | |
|---|
| 369 | m_bHidden = Shell_NotifyIcon( NIM_MODIFY, &m_tnd); |
|---|
| 370 | } |
|---|
| 371 | else |
|---|
| 372 | #endif |
|---|
| 373 | RemoveIcon(); |
|---|
| 374 | |
|---|
| 375 | return (m_bHidden == TRUE); |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | BOOL CSystemTray::ShowIcon() |
|---|
| 379 | { |
|---|
| 380 | if (m_bRemoved) |
|---|
| 381 | return AddIcon(); |
|---|
| 382 | |
|---|
| 383 | if (!m_bHidden) |
|---|
| 384 | return TRUE; |
|---|
| 385 | |
|---|
| 386 | #ifdef SYSTEMTRAY_USEW2K |
|---|
| 387 | if (m_bWin2K) |
|---|
| 388 | { |
|---|
| 389 | m_tnd.uFlags = NIF_STATE; |
|---|
| 390 | m_tnd.dwState = 0; |
|---|
| 391 | m_tnd.dwStateMask = NIS_HIDDEN; |
|---|
| 392 | Shell_NotifyIcon ( NIM_MODIFY, &m_tnd ); |
|---|
| 393 | m_bHidden = FALSE; |
|---|
| 394 | } |
|---|
| 395 | else |
|---|
| 396 | #endif |
|---|
| 397 | AddIcon(); |
|---|
| 398 | |
|---|
| 399 | return (m_bHidden == FALSE); |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | BOOL CSystemTray::SetIcon(HICON hIcon) |
|---|
| 403 | { |
|---|
| 404 | if (!m_bEnabled) |
|---|
| 405 | return FALSE; |
|---|
| 406 | |
|---|
| 407 | m_tnd.uFlags = NIF_ICON; |
|---|
| 408 | m_tnd.hIcon = hIcon; |
|---|
| 409 | |
|---|
| 410 | if (m_bHidden) |
|---|
| 411 | return TRUE; |
|---|
| 412 | else |
|---|
| 413 | return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName) |
|---|
| 417 | { |
|---|
| 418 | HICON hIcon = (HICON) ::LoadImage(AfxGetResourceHandle(), |
|---|
| 419 | lpszIconName, |
|---|
| 420 | IMAGE_ICON, |
|---|
| 421 | 0, 0, |
|---|
| 422 | LR_DEFAULTCOLOR); |
|---|
| 423 | if (!hIcon) |
|---|
| 424 | return FALSE; |
|---|
| 425 | |
|---|
| 426 | BOOL bSuccess = SetIcon(hIcon); |
|---|
| 427 | ::DestroyIcon(hIcon); |
|---|
| 428 | |
|---|
| 429 | return bSuccess; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | BOOL CSystemTray::SetIcon(UINT nIDResource) |
|---|
| 433 | { |
|---|
| 434 | return SetIcon(MAKEINTRESOURCE(nIDResource)); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName) |
|---|
| 438 | { |
|---|
| 439 | HICON hIcon = LoadIcon(NULL, lpIconName); |
|---|
| 440 | |
|---|
| 441 | return SetIcon(hIcon); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | BOOL CSystemTray::SetStandardIcon(UINT nIDResource) |
|---|
| 445 | { |
|---|
| 446 | return SetStandardIcon(MAKEINTRESOURCE(nIDResource)); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | HICON CSystemTray::GetIcon() const |
|---|
| 450 | { |
|---|
| 451 | return (m_bEnabled)? m_tnd.hIcon : NULL; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | BOOL CSystemTray::SetIconList(UINT uFirstIconID, UINT uLastIconID) |
|---|
| 455 | { |
|---|
| 456 | if (uFirstIconID > uLastIconID) |
|---|
| 457 | return FALSE; |
|---|
| 458 | |
|---|
| 459 | const CWinApp* pApp = AfxGetApp(); |
|---|
| 460 | if (!pApp) |
|---|
| 461 | { |
|---|
| 462 | ASSERT(FALSE); |
|---|
| 463 | return FALSE; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | m_IconList.RemoveAll(); |
|---|
| 467 | TRY { |
|---|
| 468 | for (UINT i = uFirstIconID; i <= uLastIconID; i++) |
|---|
| 469 | m_IconList.Add(pApp->LoadIcon(i)); |
|---|
| 470 | } |
|---|
| 471 | CATCH(CMemoryException, e) |
|---|
| 472 | { |
|---|
| 473 | e->ReportError(); |
|---|
| 474 | e->Delete(); |
|---|
| 475 | m_IconList.RemoveAll(); |
|---|
| 476 | return FALSE; |
|---|
| 477 | } |
|---|
| 478 | END_CATCH |
|---|
| 479 | |
|---|
| 480 | return TRUE; |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | BOOL CSystemTray::SetIconList(HICON* pHIconList, UINT nNumIcons) |
|---|
| 484 | { |
|---|
| 485 | m_IconList.RemoveAll(); |
|---|
| 486 | |
|---|
| 487 | TRY { |
|---|
| 488 | for (UINT i = 0; i < nNumIcons; i++) |
|---|
| 489 | m_IconList.Add(pHIconList[i]); |
|---|
| 490 | } |
|---|
| 491 | CATCH (CMemoryException, e) |
|---|
| 492 | { |
|---|
| 493 | e->ReportError(); |
|---|
| 494 | e->Delete(); |
|---|
| 495 | m_IconList.RemoveAll(); |
|---|
| 496 | return FALSE; |
|---|
| 497 | } |
|---|
| 498 | END_CATCH |
|---|
| 499 | |
|---|
| 500 | return TRUE; |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | BOOL CSystemTray::Animate(UINT nDelayMilliSeconds, int nNumSeconds /*=-1*/) |
|---|
| 504 | { |
|---|
| 505 | if (m_IconList.IsEmpty()) |
|---|
| 506 | return FALSE; |
|---|
| 507 | |
|---|
| 508 | StopAnimation(); |
|---|
| 509 | |
|---|
| 510 | m_nCurrentIcon = 0; |
|---|
| 511 | m_StartTime = COleDateTime::GetCurrentTime(); |
|---|
| 512 | m_nAnimationPeriod = nNumSeconds; |
|---|
| 513 | m_hSavedIcon = GetIcon(); |
|---|
| 514 | |
|---|
| 515 | // Setup a timer for the animation |
|---|
| 516 | m_uIDTimer = SetTimer(m_nTimerID, nDelayMilliSeconds, NULL); |
|---|
| 517 | |
|---|
| 518 | return (m_uIDTimer != 0); |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | BOOL CSystemTray::StepAnimation() |
|---|
| 522 | { |
|---|
| 523 | if (!m_IconList.GetSize()) |
|---|
| 524 | return FALSE; |
|---|
| 525 | |
|---|
| 526 | m_nCurrentIcon++; |
|---|
| 527 | if (m_nCurrentIcon >= m_IconList.GetSize()) |
|---|
| 528 | m_nCurrentIcon = 0; |
|---|
| 529 | |
|---|
| 530 | return SetIcon(m_IconList[m_nCurrentIcon]); |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | BOOL CSystemTray::StopAnimation() |
|---|
| 534 | { |
|---|
| 535 | BOOL bResult = FALSE; |
|---|
| 536 | |
|---|
| 537 | if (m_uIDTimer) |
|---|
| 538 | bResult = KillTimer(m_uIDTimer); |
|---|
| 539 | m_uIDTimer = 0; |
|---|
| 540 | |
|---|
| 541 | if (m_hSavedIcon) |
|---|
| 542 | SetIcon(m_hSavedIcon); |
|---|
| 543 | m_hSavedIcon = NULL; |
|---|
| 544 | |
|---|
| 545 | return bResult; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 549 | // CSystemTray tooltip text manipulation |
|---|
| 550 | |
|---|
| 551 | BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip) |
|---|
| 552 | { |
|---|
| 553 | ASSERT(AfxIsValidString(pszTip)); // (md) |
|---|
| 554 | ASSERT(_tcslen(pszTip) < m_nMaxTooltipLength); |
|---|
| 555 | |
|---|
| 556 | if (!m_bEnabled) |
|---|
| 557 | return FALSE; |
|---|
| 558 | |
|---|
| 559 | m_tnd.uFlags = NIF_TIP; |
|---|
| 560 | _tcsncpy(m_tnd.szTip, pszTip, m_nMaxTooltipLength-1); |
|---|
| 561 | |
|---|
| 562 | if (m_bHidden) |
|---|
| 563 | return TRUE; |
|---|
| 564 | else |
|---|
| 565 | return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | BOOL CSystemTray::SetTooltipText(UINT nID) |
|---|
| 569 | { |
|---|
| 570 | CString strText; |
|---|
| 571 | VERIFY(strText.LoadString(nID)); |
|---|
| 572 | |
|---|
| 573 | return SetTooltipText(strText); |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | CString CSystemTray::GetTooltipText() const |
|---|
| 577 | { |
|---|
| 578 | CString strText; |
|---|
| 579 | if (m_bEnabled) |
|---|
| 580 | strText = m_tnd.szTip; |
|---|
| 581 | |
|---|
| 582 | return strText; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 586 | // CSystemTray support for Win 2K features. |
|---|
| 587 | |
|---|
| 588 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 589 | // |
|---|
| 590 | // Function: ShowBalloon |
|---|
| 591 | // |
|---|
| 592 | // Description: |
|---|
| 593 | // Shows a balloon tooltip over the tray icon. |
|---|
| 594 | // |
|---|
| 595 | // Input: |
|---|
| 596 | // szText: [in] Text for the balloon tooltip. |
|---|
| 597 | // szTitle: [in] Title for the balloon. This text is shown in bold above |
|---|
| 598 | // the tooltip text (szText). Pass "" if you don't want a title. |
|---|
| 599 | // dwIcon: [in] Specifies an icon to appear in the balloon. Legal values are: |
|---|
| 600 | // NIIF_NONE: No icon |
|---|
| 601 | // NIIF_INFO: Information |
|---|
| 602 | // NIIF_WARNING: Exclamation |
|---|
| 603 | // NIIF_ERROR: Critical error (red circle with X) |
|---|
| 604 | // uTimeout: [in] Number of seconds for the balloon to remain visible. Can |
|---|
| 605 | // be between 10 and 30 inclusive. |
|---|
| 606 | // |
|---|
| 607 | // Returns: |
|---|
| 608 | // TRUE if successful, FALSE if not. |
|---|
| 609 | // |
|---|
| 610 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 611 | // Added by Michael Dunn, November 1999 |
|---|
| 612 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 613 | |
|---|
| 614 | BOOL CSystemTray::ShowBalloon(LPCTSTR szText, |
|---|
| 615 | LPCTSTR szTitle /*=NULL*/, |
|---|
| 616 | DWORD dwIcon /*=NIIF_NONE*/, |
|---|
| 617 | UINT uTimeout /*=10*/ ) |
|---|
| 618 | { |
|---|
| 619 | #ifndef SYSTEMTRAY_USEW2K |
|---|
| 620 | return FALSE; |
|---|
| 621 | #else |
|---|
| 622 | // Bail out if we're not on Win 2K. |
|---|
| 623 | if (!m_bWin2K) |
|---|
| 624 | return FALSE; |
|---|
| 625 | |
|---|
| 626 | // Verify input parameters. |
|---|
| 627 | |
|---|
| 628 | // The balloon tooltip text can be up to 255 chars long. |
|---|
| 629 | ASSERT(AfxIsValidString(szText)); |
|---|
| 630 | ASSERT(lstrlen(szText) < 256); |
|---|
| 631 | |
|---|
| 632 | // The balloon title text can be up to 63 chars long. |
|---|
| 633 | if (szTitle) |
|---|
| 634 | { |
|---|
| 635 | ASSERT(AfxIsValidString( szTitle)); |
|---|
| 636 | ASSERT(lstrlen(szTitle) < 64); |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | // dwBalloonIcon must be valid. |
|---|
| 640 | ASSERT(NIIF_NONE == dwIcon || NIIF_INFO == dwIcon || |
|---|
| 641 | NIIF_WARNING == dwIcon || NIIF_ERROR == dwIcon); |
|---|
| 642 | |
|---|
| 643 | // The timeout must be between 10 and 30 seconds. |
|---|
| 644 | ASSERT(uTimeout >= 10 && uTimeout <= 30); |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | m_tnd.uFlags = NIF_INFO; |
|---|
| 648 | _tcsncpy(m_tnd.szInfo, szText, 256); |
|---|
| 649 | if (szTitle) |
|---|
| 650 | _tcsncpy(m_tnd.szInfoTitle, szTitle, 64); |
|---|
| 651 | else |
|---|
| 652 | m_tnd.szInfoTitle[0] = _T('\0'); |
|---|
| 653 | m_tnd.dwInfoFlags = dwIcon; |
|---|
| 654 | m_tnd.uTimeout = uTimeout * 1000; // convert time to ms |
|---|
| 655 | |
|---|
| 656 | BOOL bSuccess = Shell_NotifyIcon (NIM_MODIFY, &m_tnd); |
|---|
| 657 | |
|---|
| 658 | // Zero out the balloon text string so that later operations won't redisplay |
|---|
| 659 | // the balloon. |
|---|
| 660 | m_tnd.szInfo[0] = _T('\0'); |
|---|
| 661 | |
|---|
| 662 | return bSuccess; |
|---|
| 663 | #endif |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 667 | // CSystemTray notification window stuff |
|---|
| 668 | |
|---|
| 669 | BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd) |
|---|
| 670 | { |
|---|
| 671 | if (!m_bEnabled) |
|---|
| 672 | return FALSE; |
|---|
| 673 | |
|---|
| 674 | // Make sure Notification window is valid |
|---|
| 675 | if (!pWnd || !::IsWindow(pWnd->GetSafeHwnd())) |
|---|
| 676 | { |
|---|
| 677 | ASSERT(FALSE); |
|---|
| 678 | return FALSE; |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | m_tnd.hWnd = pWnd->GetSafeHwnd(); |
|---|
| 682 | m_tnd.uFlags = 0; |
|---|
| 683 | |
|---|
| 684 | if (m_bHidden) |
|---|
| 685 | return TRUE; |
|---|
| 686 | else |
|---|
| 687 | return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | CWnd* CSystemTray::GetNotificationWnd() const |
|---|
| 691 | { |
|---|
| 692 | return CWnd::FromHandle(m_tnd.hWnd); |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | // Hatr added |
|---|
| 696 | |
|---|
| 697 | // Hatr added |
|---|
| 698 | |
|---|
| 699 | // Change or retrive the window to send menu commands to |
|---|
| 700 | BOOL CSystemTray::SetTargetWnd(CWnd* pTargetWnd) |
|---|
| 701 | { |
|---|
| 702 | m_pTargetWnd = pTargetWnd; |
|---|
| 703 | return TRUE; |
|---|
| 704 | } // CSystemTray::SetTargetWnd() |
|---|
| 705 | |
|---|
| 706 | CWnd* CSystemTray::GetTargetWnd() const |
|---|
| 707 | { |
|---|
| 708 | if (m_pTargetWnd) |
|---|
| 709 | return m_pTargetWnd; |
|---|
| 710 | else |
|---|
| 711 | return AfxGetMainWnd(); |
|---|
| 712 | } // CSystemTray::GetTargetWnd() |
|---|
| 713 | |
|---|
| 714 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 715 | // CSystemTray notification message stuff |
|---|
| 716 | |
|---|
| 717 | BOOL CSystemTray::SetCallbackMessage(UINT uCallbackMessage) |
|---|
| 718 | { |
|---|
| 719 | if (!m_bEnabled) |
|---|
| 720 | return FALSE; |
|---|
| 721 | |
|---|
| 722 | // Make sure we avoid conflict with other messages |
|---|
| 723 | ASSERT(uCallbackMessage >= WM_APP); |
|---|
| 724 | |
|---|
| 725 | m_tnd.uCallbackMessage = uCallbackMessage; |
|---|
| 726 | m_tnd.uFlags = NIF_MESSAGE; |
|---|
| 727 | |
|---|
| 728 | if (m_bHidden) |
|---|
| 729 | return TRUE; |
|---|
| 730 | else |
|---|
| 731 | return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | UINT CSystemTray::GetCallbackMessage() const |
|---|
| 735 | { |
|---|
| 736 | return m_tnd.uCallbackMessage; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 740 | // CSystemTray menu manipulation |
|---|
| 741 | |
|---|
| 742 | BOOL CSystemTray::SetMenuDefaultItem(UINT uItem, BOOL bByPos) |
|---|
| 743 | { |
|---|
| 744 | #ifdef _WIN32_WCE |
|---|
| 745 | return FALSE; |
|---|
| 746 | #else |
|---|
| 747 | if ((m_DefaultMenuItemID == uItem) && (m_DefaultMenuItemByPos == bByPos)) |
|---|
| 748 | return TRUE; |
|---|
| 749 | |
|---|
| 750 | m_DefaultMenuItemID = uItem; |
|---|
| 751 | m_DefaultMenuItemByPos = bByPos; |
|---|
| 752 | |
|---|
| 753 | CMenu menu, *pSubMenu; |
|---|
| 754 | |
|---|
| 755 | if (!menu.LoadMenu(m_tnd.uID)) |
|---|
| 756 | return FALSE; |
|---|
| 757 | |
|---|
| 758 | pSubMenu = menu.GetSubMenu(0); |
|---|
| 759 | if (!pSubMenu) |
|---|
| 760 | return FALSE; |
|---|
| 761 | |
|---|
| 762 | ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos); |
|---|
| 763 | |
|---|
| 764 | return TRUE; |
|---|
| 765 | #endif |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | void CSystemTray::GetMenuDefaultItem(UINT& uItem, BOOL& bByPos) |
|---|
| 769 | { |
|---|
| 770 | uItem = m_DefaultMenuItemID; |
|---|
| 771 | bByPos = m_DefaultMenuItemByPos; |
|---|
| 772 | } |
|---|
| 773 | |
|---|
| 774 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 775 | // CSystemTray message handlers |
|---|
| 776 | |
|---|
| 777 | BEGIN_MESSAGE_MAP(CSystemTray, CWnd) |
|---|
| 778 | //{{AFX_MSG_MAP(CSystemTray) |
|---|
| 779 | ON_WM_TIMER() |
|---|
| 780 | //}}AFX_MSG_MAP |
|---|
| 781 | #ifndef _WIN32_WCE |
|---|
| 782 | ON_WM_SETTINGCHANGE() |
|---|
| 783 | #endif |
|---|
| 784 | ON_REGISTERED_MESSAGE(CSystemTray::m_nTaskbarCreatedMsg, OnTaskbarCreated) |
|---|
| 785 | END_MESSAGE_MAP() |
|---|
| 786 | |
|---|
| 787 | void CSystemTray::OnTimer(UINT_PTR nIDEvent) |
|---|
| 788 | { |
|---|
| 789 | if (nIDEvent != m_uIDTimer) |
|---|
| 790 | { |
|---|
| 791 | ASSERT(FALSE); |
|---|
| 792 | return; |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | COleDateTime CurrentTime = COleDateTime::GetCurrentTime(); |
|---|
| 796 | COleDateTimeSpan period = CurrentTime - m_StartTime; |
|---|
| 797 | |
|---|
| 798 | if (m_nAnimationPeriod > 0 && m_nAnimationPeriod < period.GetTotalSeconds()) |
|---|
| 799 | { |
|---|
| 800 | StopAnimation(); |
|---|
| 801 | return; |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | StepAnimation(); |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | // This is called whenever the taskbar is created (eg after explorer crashes |
|---|
| 808 | // and restarts. Please note that the WM_TASKBARCREATED message is only passed |
|---|
| 809 | // to TOP LEVEL windows (like WM_QUERYNEWPALETTE) |
|---|
| 810 | LRESULT CSystemTray::OnTaskbarCreated(WPARAM /*wParam*/, LPARAM /*lParam*/) |
|---|
| 811 | { |
|---|
| 812 | m_bShowIconPending = TRUE; // !m_bHidden; |
|---|
| 813 | InstallIconPending(); |
|---|
| 814 | |
|---|
| 815 | return 0L; |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | #ifndef _WIN32_WCE |
|---|
| 819 | void CSystemTray::OnSettingChange(UINT uFlags, LPCTSTR lpszSection) |
|---|
| 820 | { |
|---|
| 821 | CWnd::OnSettingChange(uFlags, lpszSection); |
|---|
| 822 | } |
|---|
| 823 | #endif |
|---|
| 824 | |
|---|
| 825 | LRESULT CSystemTray::OnTrayNotification(WPARAM wParam, LPARAM lParam) |
|---|
| 826 | { |
|---|
| 827 | //Return quickly if its not for this tray icon |
|---|
| 828 | if (wParam != m_tnd.uID) |
|---|
| 829 | return 0L; |
|---|
| 830 | |
|---|
| 831 | CMenu menu, *pSubMenu; |
|---|
| 832 | CWnd *pTargetWnd = GetTargetWnd(); |
|---|
| 833 | if (!pTargetWnd) |
|---|
| 834 | return 0L; |
|---|
| 835 | |
|---|
| 836 | // Clicking with right button brings up a context menu |
|---|
| 837 | #if defined(_WIN32_WCE) //&& _WIN32_WCE < 211 |
|---|
| 838 | BOOL bAltPressed = ((GetKeyState(VK_MENU) & (1 << (sizeof(SHORT)*8-1))) != 0); |
|---|
| 839 | if (LOWORD(lParam) == WM_LBUTTONUP && bAltPressed) |
|---|
| 840 | #else |
|---|
| 841 | if (LOWORD(lParam) == WM_RBUTTONUP) |
|---|
| 842 | #endif |
|---|
| 843 | { |
|---|
| 844 | if (!menu.LoadMenu(m_tnd.uID)) |
|---|
| 845 | return 0; |
|---|
| 846 | |
|---|
| 847 | pSubMenu = menu.GetSubMenu(0); |
|---|
| 848 | if (!pSubMenu) |
|---|
| 849 | return 0; |
|---|
| 850 | |
|---|
| 851 | #ifndef _WIN32_WCE |
|---|
| 852 | // Make chosen menu item the default (bold font) |
|---|
| 853 | ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos); |
|---|
| 854 | #endif |
|---|
| 855 | |
|---|
| 856 | CustomizeMenu(pSubMenu); |
|---|
| 857 | |
|---|
| 858 | // Display and track the popup menu |
|---|
| 859 | CPoint pos; |
|---|
| 860 | #ifdef _WIN32_WCE |
|---|
| 861 | pos = CPoint(GetMessagePos()); |
|---|
| 862 | #else |
|---|
| 863 | GetCursorPos(&pos); |
|---|
| 864 | #endif |
|---|
| 865 | |
|---|
| 866 | pTargetWnd->SetForegroundWindow(); |
|---|
| 867 | |
|---|
| 868 | #ifndef _WIN32_WCE |
|---|
| 869 | ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, |
|---|
| 870 | pTargetWnd->GetSafeHwnd(), NULL); |
|---|
| 871 | #else |
|---|
| 872 | pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL); |
|---|
| 873 | #endif |
|---|
| 874 | |
|---|
| 875 | // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly" |
|---|
| 876 | pTargetWnd->PostMessage(WM_NULL, 0, 0); |
|---|
| 877 | |
|---|
| 878 | menu.DestroyMenu(); |
|---|
| 879 | } |
|---|
| 880 | #if defined(_WIN32_WCE) //&& _WIN32_WCE < 211 |
|---|
| 881 | if (LOWORD(lParam) == WM_LBUTTONDBLCLK && bAltPressed) |
|---|
| 882 | #else |
|---|
| 883 | else if (LOWORD(lParam) == WM_LBUTTONDBLCLK) |
|---|
| 884 | #endif |
|---|
| 885 | { |
|---|
| 886 | // double click received, the default action is to execute default menu item |
|---|
| 887 | pTargetWnd->SetForegroundWindow(); |
|---|
| 888 | |
|---|
| 889 | UINT uItem; |
|---|
| 890 | if (m_DefaultMenuItemByPos) |
|---|
| 891 | { |
|---|
| 892 | if (!menu.LoadMenu(m_tnd.uID)) |
|---|
| 893 | return 0; |
|---|
| 894 | |
|---|
| 895 | pSubMenu = menu.GetSubMenu(0); |
|---|
| 896 | if (!pSubMenu) |
|---|
| 897 | return 0; |
|---|
| 898 | |
|---|
| 899 | uItem = pSubMenu->GetMenuItemID(m_DefaultMenuItemID); |
|---|
| 900 | |
|---|
| 901 | menu.DestroyMenu(); |
|---|
| 902 | } |
|---|
| 903 | else |
|---|
| 904 | uItem = m_DefaultMenuItemID; |
|---|
| 905 | |
|---|
| 906 | pTargetWnd->PostMessage(WM_COMMAND, uItem, 0); |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | return 1; |
|---|
| 910 | } |
|---|
| 911 | |
|---|
| 912 | LRESULT CSystemTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) |
|---|
| 913 | { |
|---|
| 914 | if (message == m_tnd.uCallbackMessage) |
|---|
| 915 | return OnTrayNotification(wParam, lParam); |
|---|
| 916 | |
|---|
| 917 | return CWnd::WindowProc(message, wParam, lParam); |
|---|
| 918 | } |
|---|
| 919 | |
|---|
| 920 | void CSystemTray::InstallIconPending() |
|---|
| 921 | { |
|---|
| 922 | // Is the icon display pending, and it's not been set as "hidden"? |
|---|
| 923 | if (!m_bShowIconPending || m_bHidden) |
|---|
| 924 | return; |
|---|
| 925 | |
|---|
| 926 | // Reset the flags to what was used at creation |
|---|
| 927 | m_tnd.uFlags = m_uCreationFlags; |
|---|
| 928 | |
|---|
| 929 | // Try and recreate the icon |
|---|
| 930 | m_bHidden = !Shell_NotifyIcon(NIM_ADD, &m_tnd); |
|---|
| 931 | |
|---|
| 932 | // If it's STILL hidden, then have another go next time... |
|---|
| 933 | m_bShowIconPending = m_bHidden; |
|---|
| 934 | |
|---|
| 935 | ASSERT(m_bHidden == FALSE); |
|---|
| 936 | } |
|---|
| 937 | |
|---|
| 938 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 939 | // For minimising/maximising from system tray |
|---|
| 940 | |
|---|
| 941 | BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam) |
|---|
| 942 | { |
|---|
| 943 | TCHAR szClassName[256]; |
|---|
| 944 | GetClassName(hwnd, szClassName, 255); |
|---|
| 945 | |
|---|
| 946 | // Did we find the Main System Tray? If so, then get its size and keep going |
|---|
| 947 | if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0) |
|---|
| 948 | { |
|---|
| 949 | CRect *pRect = (CRect*) lParam; |
|---|
| 950 | ::GetWindowRect(hwnd, pRect); |
|---|
| 951 | return TRUE; |
|---|
| 952 | } |
|---|
| 953 | |
|---|
| 954 | // Did we find the System Clock? If so, then adjust the size of the rectangle |
|---|
| 955 | // we have and quit (clock will be found after the system tray) |
|---|
| 956 | if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0) |
|---|
| 957 | { |
|---|
| 958 | CRect *pRect = (CRect*) lParam; |
|---|
| 959 | CRect rectClock; |
|---|
| 960 | ::GetWindowRect(hwnd, rectClock); |
|---|
| 961 | // if clock is above system tray adjust accordingly |
|---|
| 962 | if (rectClock.bottom < pRect->bottom-5) // 10 = random fudge factor. |
|---|
| 963 | pRect->top = rectClock.bottom; |
|---|
| 964 | else |
|---|
| 965 | pRect->right = rectClock.left; |
|---|
| 966 | return FALSE; |
|---|
| 967 | } |
|---|
| 968 | |
|---|
| 969 | return TRUE; |
|---|
| 970 | } |
|---|
| 971 | |
|---|
| 972 | #ifndef _WIN32_WCE |
|---|
| 973 | // enhanced version by Matthew Ellis <m.t.ellis@bigfoot.com> |
|---|
| 974 | void CSystemTray::GetTrayWndRect(LPRECT lprect) |
|---|
| 975 | { |
|---|
| 976 | #define DEFAULT_RECT_WIDTH 150 |
|---|
| 977 | #define DEFAULT_RECT_HEIGHT 30 |
|---|
| 978 | |
|---|
| 979 | HWND hShellTrayWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL); |
|---|
| 980 | if (hShellTrayWnd) |
|---|
| 981 | { |
|---|
| 982 | ::GetWindowRect(hShellTrayWnd, lprect); |
|---|
| 983 | EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect); |
|---|
| 984 | return; |
|---|
| 985 | } |
|---|
| 986 | // OK, we failed to get the rect from the quick hack. Either explorer isn't |
|---|
| 987 | // running or it's a new version of the shell with the window class names |
|---|
| 988 | // changed (how dare Microsoft change these undocumented class names!) So, we |
|---|
| 989 | // try to find out what side of the screen the taskbar is connected to. We |
|---|
| 990 | // know that the system tray is either on the right or the bottom of the |
|---|
| 991 | // taskbar, so we can make a good guess at where to minimize to |
|---|
| 992 | APPBARDATA appBarData; |
|---|
| 993 | appBarData.cbSize=sizeof(appBarData); |
|---|
| 994 | if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData)) |
|---|
| 995 | { |
|---|
| 996 | // We know the edge the taskbar is connected to, so guess the rect of the |
|---|
| 997 | // system tray. Use various fudge factor to make it look good |
|---|
| 998 | switch(appBarData.uEdge) |
|---|
| 999 | { |
|---|
| 1000 | case ABE_LEFT: |
|---|
| 1001 | case ABE_RIGHT: |
|---|
| 1002 | // We want to minimize to the bottom of the taskbar |
|---|
| 1003 | lprect->top = appBarData.rc.bottom-100; |
|---|
| 1004 | lprect->bottom = appBarData.rc.bottom-16; |
|---|
| 1005 | lprect->left = appBarData.rc.left; |
|---|
| 1006 | lprect->right = appBarData.rc.right; |
|---|
| 1007 | break; |
|---|
| 1008 | |
|---|
| 1009 | case ABE_TOP: |
|---|
| 1010 | case ABE_BOTTOM: |
|---|
| 1011 | // We want to minimize to the right of the taskbar |
|---|
| 1012 | lprect->top = appBarData.rc.top; |
|---|
| 1013 | lprect->bottom = appBarData.rc.bottom; |
|---|
| 1014 | lprect->left = appBarData.rc.right-100; |
|---|
| 1015 | lprect->right = appBarData.rc.right-16; |
|---|
| 1016 | break; |
|---|
| 1017 | } |
|---|
| 1018 | return; |
|---|
| 1019 | } |
|---|
| 1020 | |
|---|
| 1021 | // Blimey, we really aren't in luck. It's possible that a third party shell |
|---|
| 1022 | // is running instead of explorer. This shell might provide support for the |
|---|
| 1023 | // system tray, by providing a Shell_TrayWnd window (which receives the |
|---|
| 1024 | // messages for the icons) So, look for a Shell_TrayWnd window and work out |
|---|
| 1025 | // the rect from that. Remember that explorer's taskbar is the Shell_TrayWnd, |
|---|
| 1026 | // and stretches either the width or the height of the screen. We can't rely |
|---|
| 1027 | // on the 3rd party shell's Shell_TrayWnd doing the same, in fact, we can't |
|---|
| 1028 | // rely on it being any size. The best we can do is just blindly use the |
|---|
| 1029 | // window rect, perhaps limiting the width and height to, say 150 square. |
|---|
| 1030 | // Note that if the 3rd party shell supports the same configuraion as |
|---|
| 1031 | // explorer (the icons hosted in NotifyTrayWnd, which is a child window of |
|---|
| 1032 | // Shell_TrayWnd), we would already have caught it above |
|---|
| 1033 | if (hShellTrayWnd) |
|---|
| 1034 | { |
|---|
| 1035 | ::GetWindowRect(hShellTrayWnd, lprect); |
|---|
| 1036 | if (lprect->right - lprect->left > DEFAULT_RECT_WIDTH) |
|---|
| 1037 | lprect->left = lprect->right - DEFAULT_RECT_WIDTH; |
|---|
| 1038 | if (lprect->bottom - lprect->top > DEFAULT_RECT_HEIGHT) |
|---|
| 1039 | lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT; |
|---|
| 1040 | |
|---|
| 1041 | return; |
|---|
| 1042 | } |
|---|
| 1043 | |
|---|
| 1044 | // OK. Haven't found a thing. Provide a default rect based on the current work |
|---|
| 1045 | // area |
|---|
| 1046 | SystemParametersInfo(SPI_GETWORKAREA,0, lprect, 0); |
|---|
| 1047 | lprect->left = lprect->right - DEFAULT_RECT_WIDTH; |
|---|
| 1048 | lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT; |
|---|
| 1049 | } |
|---|
| 1050 | |
|---|
| 1051 | // Check to see if the animation has been disabled (Matthew Ellis <m.t.ellis@bigfoot.com>) |
|---|
| 1052 | BOOL CSystemTray::GetDoWndAnimation() |
|---|
| 1053 | { |
|---|
| 1054 | ANIMATIONINFO ai; |
|---|
| 1055 | |
|---|
| 1056 | ai.cbSize=sizeof(ai); |
|---|
| 1057 | SystemParametersInfo(SPI_GETANIMATION,sizeof(ai),&ai,0); |
|---|
| 1058 | |
|---|
| 1059 | return ai.iMinAnimate?TRUE:FALSE; |
|---|
| 1060 | } |
|---|
| 1061 | #endif |
|---|
| 1062 | |
|---|
| 1063 | BOOL CSystemTray::RemoveTaskbarIcon(CWnd* pWnd) |
|---|
| 1064 | { |
|---|
| 1065 | LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0); |
|---|
| 1066 | |
|---|
| 1067 | // Create static invisible window |
|---|
| 1068 | if (!::IsWindow(m_wndInvisible.m_hWnd)) |
|---|
| 1069 | { |
|---|
| 1070 | if (!m_wndInvisible.CreateEx(0, pstrOwnerClass, _T(""), WS_POPUP, |
|---|
| 1071 | CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, |
|---|
| 1072 | NULL, 0)) |
|---|
| 1073 | return FALSE; |
|---|
| 1074 | } |
|---|
| 1075 | |
|---|
| 1076 | pWnd->SetParent(&m_wndInvisible); |
|---|
| 1077 | |
|---|
| 1078 | return TRUE; |
|---|
| 1079 | } |
|---|
| 1080 | |
|---|
| 1081 | void CSystemTray::MinimiseToTray(CWnd* pWnd, BOOL bForceAnimation /*= FALSE*/) |
|---|
| 1082 | { |
|---|
| 1083 | #ifndef _WIN32_WCE |
|---|
| 1084 | if (bForceAnimation || GetDoWndAnimation()) |
|---|
| 1085 | { |
|---|
| 1086 | CRect rectFrom, rectTo; |
|---|
| 1087 | |
|---|
| 1088 | pWnd->GetWindowRect(rectFrom); |
|---|
| 1089 | GetTrayWndRect(rectTo); |
|---|
| 1090 | |
|---|
| 1091 | ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo); |
|---|
| 1092 | } |
|---|
| 1093 | |
|---|
| 1094 | RemoveTaskbarIcon(pWnd); |
|---|
| 1095 | pWnd->ModifyStyle(WS_VISIBLE, 0); |
|---|
| 1096 | #endif |
|---|
| 1097 | } |
|---|
| 1098 | |
|---|
| 1099 | void CSystemTray::MaximiseFromTray(CWnd* pWnd, BOOL bForceAnimation /*= TRUE*/) |
|---|
| 1100 | { |
|---|
| 1101 | #ifndef _WIN32_WCE |
|---|
| 1102 | if (bForceAnimation || GetDoWndAnimation()) |
|---|
| 1103 | { |
|---|
| 1104 | CRect rectTo; |
|---|
| 1105 | pWnd->GetWindowRect(rectTo); |
|---|
| 1106 | |
|---|
| 1107 | CRect rectFrom; |
|---|
| 1108 | GetTrayWndRect(rectFrom); |
|---|
| 1109 | |
|---|
| 1110 | pWnd->SetParent(NULL); |
|---|
| 1111 | ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo); |
|---|
| 1112 | } |
|---|
| 1113 | else |
|---|
| 1114 | pWnd->SetParent(NULL); |
|---|
| 1115 | |
|---|
| 1116 | pWnd->ModifyStyle(0, WS_VISIBLE); |
|---|
| 1117 | pWnd->RedrawWindow(NULL, NULL, RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_FRAME | |
|---|
| 1118 | RDW_INVALIDATE | RDW_ERASE); |
|---|
| 1119 | |
|---|
| 1120 | // Move focus away and back again to ensure taskbar icon is recreated |
|---|
| 1121 | if (::IsWindow(m_wndInvisible.m_hWnd)) |
|---|
| 1122 | m_wndInvisible.SetActiveWindow(); |
|---|
| 1123 | pWnd->SetActiveWindow(); |
|---|
| 1124 | pWnd->SetForegroundWindow(); |
|---|
| 1125 | #endif |
|---|
| 1126 | } |
|---|