source: trunk/SystemTray.cpp @ 67

Revision 67, 31.8 KB checked in by lowjoel, 7 years ago (diff)

-Updated the System Tray class
-Make the Scheduler popup menu slightly more meaningful (in terms of text chosen)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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
84static 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
100IMPLEMENT_DYNAMIC(CSystemTray, CWnd)
101
102const UINT CSystemTray::m_nTimerID    = 4567;
103UINT CSystemTray::m_nMaxTooltipLength  = 128;     // This may change...
104const UINT CSystemTray::m_nTaskbarCreatedMsg = ::RegisterWindowMessage(_T("TaskbarCreated"));
105CWnd  CSystemTray::m_wndInvisible;
106
107/////////////////////////////////////////////////////////////////////////////
108// CSystemTray construction/creation/destruction
109
110CSystemTray::CSystemTray()
111{
112    Initialise();
113}
114
115CSystemTray::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
131void 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
180BOOL 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
285CSystemTray::~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
314void CSystemTray::SetFocus()
315{
316#ifdef SYSTEMTRAY_USEW2K
317    Shell_NotifyIcon ( NIM_SETFOCUS, &m_tnd );
318#endif
319}
320
321BOOL CSystemTray::MoveToRight()
322{
323    RemoveIcon();
324    return AddIcon();
325}
326
327BOOL 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
343BOOL 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
357BOOL 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
378BOOL 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
402BOOL 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
416BOOL 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
432BOOL CSystemTray::SetIcon(UINT nIDResource)
433{
434    return SetIcon(MAKEINTRESOURCE(nIDResource));
435}
436
437BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName)
438{
439    HICON hIcon = LoadIcon(NULL, lpIconName);
440
441    return SetIcon(hIcon);
442}
443
444BOOL CSystemTray::SetStandardIcon(UINT nIDResource)
445{
446    return SetStandardIcon(MAKEINTRESOURCE(nIDResource));
447}
448 
449HICON CSystemTray::GetIcon() const
450{
451    return (m_bEnabled)? m_tnd.hIcon : NULL;
452}
453
454BOOL 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
483BOOL 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
503BOOL 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
521BOOL 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
533BOOL 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
551BOOL 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
568BOOL CSystemTray::SetTooltipText(UINT nID)
569{
570    CString strText;
571    VERIFY(strText.LoadString(nID));
572
573    return SetTooltipText(strText);
574}
575
576CString 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
614BOOL 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
669BOOL 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
690CWnd* 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
700BOOL CSystemTray::SetTargetWnd(CWnd* pTargetWnd)
701{
702    m_pTargetWnd = pTargetWnd;
703    return TRUE;
704} // CSystemTray::SetTargetWnd()
705
706CWnd* 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
717BOOL 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
734UINT CSystemTray::GetCallbackMessage() const
735{
736    return m_tnd.uCallbackMessage;
737}
738
739/////////////////////////////////////////////////////////////////////////////
740// CSystemTray menu manipulation
741
742BOOL 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
768void CSystemTray::GetMenuDefaultItem(UINT& uItem, BOOL& bByPos)
769{
770    uItem = m_DefaultMenuItemID;
771    bByPos = m_DefaultMenuItemByPos;
772}
773
774/////////////////////////////////////////////////////////////////////////////
775// CSystemTray message handlers
776
777BEGIN_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)
785END_MESSAGE_MAP()
786
787void 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)
810LRESULT 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
819void CSystemTray::OnSettingChange(UINT uFlags, LPCTSTR lpszSection) 
820{
821    CWnd::OnSettingChange(uFlags, lpszSection);
822
823    if (uFlags == SPI_SETWORKAREA)
824    {
825        m_bShowIconPending = !m_bHidden;
826        InstallIconPending();   
827    }
828}
829#endif
830
831LRESULT CSystemTray::OnTrayNotification(WPARAM wParam, LPARAM lParam) 
832{
833    //Return quickly if its not for this tray icon
834    if (wParam != m_tnd.uID)
835        return 0L;
836
837    CMenu menu, *pSubMenu;
838    CWnd *pTargetWnd = GetTargetWnd();
839    if (!pTargetWnd)
840        return 0L;
841
842    // Clicking with right button brings up a context menu
843#if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
844    BOOL bAltPressed = ((GetKeyState(VK_MENU) & (1 << (sizeof(SHORT)*8-1))) != 0);
845    if (LOWORD(lParam) == WM_LBUTTONUP && bAltPressed)
846#else
847    if (LOWORD(lParam) == WM_RBUTTONUP)
848#endif
849    {   
850        if (!menu.LoadMenu(m_tnd.uID))
851            return 0;
852       
853        pSubMenu = menu.GetSubMenu(0);
854        if (!pSubMenu)
855            return 0;
856
857#ifndef _WIN32_WCE
858        // Make chosen menu item the default (bold font)
859        ::SetMenuDefaultItem(pSubMenu->m_hMenu, m_DefaultMenuItemID, m_DefaultMenuItemByPos);
860#endif
861
862        CustomizeMenu(pSubMenu);
863
864        // Display and track the popup menu
865        CPoint pos;
866#ifdef _WIN32_WCE
867        pos = CPoint(GetMessagePos());
868#else
869        GetCursorPos(&pos);
870#endif
871
872        pTargetWnd->SetForegroundWindow(); 
873       
874#ifndef _WIN32_WCE
875        ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, 
876                         pTargetWnd->GetSafeHwnd(), NULL);
877#else
878        pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pos.x, pos.y, pTargetWnd, NULL);
879#endif
880
881        // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
882        pTargetWnd->PostMessage(WM_NULL, 0, 0);
883
884        menu.DestroyMenu();
885    } 
886#if defined(_WIN32_WCE) //&& _WIN32_WCE < 211
887    if (LOWORD(lParam) == WM_LBUTTONDBLCLK && bAltPressed)
888#else
889    else if (LOWORD(lParam) == WM_LBUTTONDBLCLK) 
890#endif
891    {
892        // double click received, the default action is to execute default menu item
893        pTargetWnd->SetForegroundWindow(); 
894
895        UINT uItem;
896        if (m_DefaultMenuItemByPos)
897        {
898            if (!menu.LoadMenu(m_tnd.uID))
899                return 0;
900           
901            pSubMenu = menu.GetSubMenu(0);
902            if (!pSubMenu)
903                return 0;
904           
905            uItem = pSubMenu->GetMenuItemID(m_DefaultMenuItemID);
906
907            menu.DestroyMenu();
908        }
909        else
910            uItem = m_DefaultMenuItemID;
911       
912        pTargetWnd->PostMessage(WM_COMMAND, uItem, 0);
913    }
914
915    return 1;
916}
917
918LRESULT CSystemTray::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
919{
920    if (message == m_tnd.uCallbackMessage)
921        return OnTrayNotification(wParam, lParam);
922   
923    return CWnd::WindowProc(message, wParam, lParam);
924}
925
926void CSystemTray::InstallIconPending()
927{
928    // Is the icon display pending, and it's not been set as "hidden"?
929    if (!m_bShowIconPending || m_bHidden)
930        return;
931
932    // Reset the flags to what was used at creation
933    m_tnd.uFlags = m_uCreationFlags;
934
935    // Try and recreate the icon
936    m_bHidden = !Shell_NotifyIcon(NIM_ADD, &m_tnd);
937
938    // If it's STILL hidden, then have another go next time...
939    m_bShowIconPending = m_bHidden;
940
941    ASSERT(m_bHidden == FALSE);
942}
943
944/////////////////////////////////////////////////////////////////////////////
945// For minimising/maximising from system tray
946
947BOOL CALLBACK FindTrayWnd(HWND hwnd, LPARAM lParam)
948{
949    TCHAR szClassName[256];
950    GetClassName(hwnd, szClassName, 255);
951
952    // Did we find the Main System Tray? If so, then get its size and keep going
953    if (_tcscmp(szClassName, _T("TrayNotifyWnd")) == 0)
954    {
955        CRect *pRect = (CRect*) lParam;
956        ::GetWindowRect(hwnd, pRect);
957        return TRUE;
958    }
959
960    // Did we find the System Clock? If so, then adjust the size of the rectangle
961    // we have and quit (clock will be found after the system tray)
962    if (_tcscmp(szClassName, _T("TrayClockWClass")) == 0)
963    {
964        CRect *pRect = (CRect*) lParam;
965        CRect rectClock;
966        ::GetWindowRect(hwnd, rectClock);
967        // if clock is above system tray adjust accordingly
968        if (rectClock.bottom < pRect->bottom-5) // 10 = random fudge factor.
969            pRect->top = rectClock.bottom;
970        else
971            pRect->right = rectClock.left;
972        return FALSE;
973    }
974 
975    return TRUE;
976}
977 
978#ifndef _WIN32_WCE
979// enhanced version by Matthew Ellis <m.t.ellis@bigfoot.com>
980void CSystemTray::GetTrayWndRect(LPRECT lprect)
981{
982#define DEFAULT_RECT_WIDTH 150
983#define DEFAULT_RECT_HEIGHT 30
984
985    HWND hShellTrayWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
986    if (hShellTrayWnd)
987    {
988        ::GetWindowRect(hShellTrayWnd, lprect);
989        EnumChildWindows(hShellTrayWnd, FindTrayWnd, (LPARAM)lprect);
990        return;
991    }
992    // OK, we failed to get the rect from the quick hack. Either explorer isn't
993    // running or it's a new version of the shell with the window class names
994    // changed (how dare Microsoft change these undocumented class names!) So, we
995    // try to find out what side of the screen the taskbar is connected to. We
996    // know that the system tray is either on the right or the bottom of the
997    // taskbar, so we can make a good guess at where to minimize to
998    APPBARDATA appBarData;
999    appBarData.cbSize=sizeof(appBarData);
1000    if (SHAppBarMessage(ABM_GETTASKBARPOS,&appBarData))
1001    {
1002        // We know the edge the taskbar is connected to, so guess the rect of the
1003        // system tray. Use various fudge factor to make it look good
1004        switch(appBarData.uEdge)
1005        {
1006        case ABE_LEFT:
1007        case ABE_RIGHT:
1008            // We want to minimize to the bottom of the taskbar
1009            lprect->top    = appBarData.rc.bottom-100;
1010            lprect->bottom = appBarData.rc.bottom-16;
1011            lprect->left   = appBarData.rc.left;
1012            lprect->right  = appBarData.rc.right;
1013            break;
1014           
1015        case ABE_TOP:
1016        case ABE_BOTTOM:
1017            // We want to minimize to the right of the taskbar
1018            lprect->top    = appBarData.rc.top;
1019            lprect->bottom = appBarData.rc.bottom;
1020            lprect->left   = appBarData.rc.right-100;
1021            lprect->right  = appBarData.rc.right-16;
1022            break;
1023        }
1024        return;
1025    }
1026   
1027    // Blimey, we really aren't in luck. It's possible that a third party shell
1028    // is running instead of explorer. This shell might provide support for the
1029    // system tray, by providing a Shell_TrayWnd window (which receives the
1030    // messages for the icons) So, look for a Shell_TrayWnd window and work out
1031    // the rect from that. Remember that explorer's taskbar is the Shell_TrayWnd,
1032    // and stretches either the width or the height of the screen. We can't rely
1033    // on the 3rd party shell's Shell_TrayWnd doing the same, in fact, we can't
1034    // rely on it being any size. The best we can do is just blindly use the
1035    // window rect, perhaps limiting the width and height to, say 150 square.
1036    // Note that if the 3rd party shell supports the same configuraion as
1037    // explorer (the icons hosted in NotifyTrayWnd, which is a child window of
1038    // Shell_TrayWnd), we would already have caught it above
1039    if (hShellTrayWnd)
1040    {
1041        ::GetWindowRect(hShellTrayWnd, lprect);
1042        if (lprect->right - lprect->left > DEFAULT_RECT_WIDTH)
1043            lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
1044        if (lprect->bottom - lprect->top > DEFAULT_RECT_HEIGHT)
1045            lprect->top = lprect->bottom - DEFAULT_RECT_HEIGHT;
1046       
1047        return;
1048    }
1049   
1050    // OK. Haven't found a thing. Provide a default rect based on the current work
1051    // area
1052    SystemParametersInfo(SPI_GETWORKAREA,0, lprect, 0);
1053    lprect->left = lprect->right - DEFAULT_RECT_WIDTH;
1054    lprect->top  = lprect->bottom - DEFAULT_RECT_HEIGHT;
1055}
1056
1057// Check to see if the animation has been disabled (Matthew Ellis <m.t.ellis@bigfoot.com>)
1058BOOL CSystemTray::GetDoWndAnimation()
1059{
1060  ANIMATIONINFO ai;
1061
1062  ai.cbSize=sizeof(ai);
1063  SystemParametersInfo(SPI_GETANIMATION,sizeof(ai),&ai,0);
1064
1065  return ai.iMinAnimate?TRUE:FALSE;
1066}
1067#endif
1068
1069BOOL CSystemTray::RemoveTaskbarIcon(CWnd* pWnd)
1070{
1071    LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);
1072
1073    // Create static invisible window
1074    if (!::IsWindow(m_wndInvisible.m_hWnd))
1075    {
1076        if (!m_wndInvisible.CreateEx(0, pstrOwnerClass, _T(""), WS_POPUP,
1077                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1078                NULL, 0))
1079            return FALSE;
1080    }
1081
1082    pWnd->SetParent(&m_wndInvisible);
1083
1084    return TRUE;
1085}
1086
1087void CSystemTray::MinimiseToTray(CWnd* pWnd, BOOL bForceAnimation /*= FALSE*/)
1088{
1089#ifndef _WIN32_WCE
1090    if (bForceAnimation || GetDoWndAnimation())
1091    {
1092        CRect rectFrom, rectTo;
1093
1094        pWnd->GetWindowRect(rectFrom);
1095        GetTrayWndRect(rectTo);
1096
1097        ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
1098    }
1099
1100    RemoveTaskbarIcon(pWnd);
1101    pWnd->ModifyStyle(WS_VISIBLE, 0);
1102#endif
1103}
1104
1105void CSystemTray::MaximiseFromTray(CWnd* pWnd, BOOL bForceAnimation /*= TRUE*/)
1106{
1107#ifndef _WIN32_WCE
1108    if (bForceAnimation || GetDoWndAnimation())
1109    {
1110        CRect rectTo;
1111        pWnd->GetWindowRect(rectTo);
1112
1113        CRect rectFrom;
1114        GetTrayWndRect(rectFrom);
1115
1116        pWnd->SetParent(NULL);
1117        ::DrawAnimatedRects(pWnd->m_hWnd, IDANI_CAPTION, rectFrom, rectTo);
1118    }
1119    else
1120        pWnd->SetParent(NULL);
1121
1122    pWnd->ModifyStyle(0, WS_VISIBLE);
1123    pWnd->RedrawWindow(NULL, NULL, RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_FRAME |
1124                                   RDW_INVALIDATE | RDW_ERASE);
1125
1126    // Move focus away and back again to ensure taskbar icon is recreated
1127    if (::IsWindow(m_wndInvisible.m_hWnd))
1128        m_wndInvisible.SetActiveWindow();
1129    pWnd->SetActiveWindow();
1130    pWnd->SetForegroundWindow();
1131#endif
1132}
Note: See TracBrowser for help on using the repository browser.