| 83 | //A Simple and fast Tray Icon Application class. |
| 84 | |
| 85 | class CTrayIcon : public CWnd { |
| 86 | public: |
| 87 | // disallow copying |
| 88 | CTrayIcon(CTrayIcon const&) = delete; |
| 89 | void operator=(CTrayIcon const&) = delete; |
| 90 | |
| 91 | CTrayIcon() {} |
| 92 | CTrayIcon(LPCTSTR szTip, HICON hIcon) {Create(szTip, hIcon);} |
| 93 | virtual ~CTrayIcon() { |
| 94 | RemoveIcon(); |
| 95 | DestroyWindow(); |
| 96 | } |
| 97 | |
| 98 | CString GetTooltipText() const {return NID.szTip;} |
| 99 | BOOL SetTooltipText(LPCTSTR szTip){ |
| 100 | if(!OK) return FALSE; |
| 101 | NID.uFlags=NIF_TIP; |
| 102 | wcscpy_s(NID.szTip, sizeof(NID.szTip) / sizeof(NID.szTip[0]), szTip); |
| 103 | return Shell_NotifyIcon(NIM_MODIFY, &NID); |
| 104 | } |
| 105 | |
| 106 | HICON GetIcon() const {return NID.hIcon;} |
| 107 | BOOL SetIcon(HICON hIcon) { |
| 108 | if(!OK) return FALSE; |
| 109 | NID.uFlags=NIF_ICON; |
| 110 | NID.hIcon=hIcon; |
| 111 | return Shell_NotifyIcon(NIM_MODIFY, &NID); |
| 112 | } |
| 113 | |
| 114 | protected: |
| 115 | BOOL OK; // true if the operating system supports tray icons and the Icon was successfully created |
| 116 | NOTIFYICONDATA NID; |
| 117 | |
| 118 | // Operations |
| 119 | BOOL Create(LPCTSTR szTip, HICON hIcon, UINT Menu=0) { //Menu may be used by derived classes |
| 120 | //VERIFY(OK=(GetVersion() & 0xFF)>=4); // this is only for Windows 95 (or higher) |
| 121 | OK = TRUE; |
| 122 | if(OK) { |
| 123 | ASSERT(_tcslen(szTip)<=64); // Tray only supports tooltip text up to 64 characters |
| 124 | // Create this invisible window for Message passing |
| 125 | CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,10,10, 0, 0); |
| 126 | memset(&NID, 0, sizeof(NID)); |
| 127 | NID.cbSize=sizeof(NOTIFYICONDATA); |
| 128 | NID.hWnd =m_hWnd;//pParent->GetSafeHwnd()? pParent->GetSafeHwnd() : m_hWnd; |
| 129 | NID.uID =Menu; |
| 130 | NID.hIcon =hIcon; |
| 131 | NID.uFlags=NIF_MESSAGE | NIF_ICON | NIF_TIP; |
| 132 | NID.uCallbackMessage=RegisterWindowMessage(L"CTrayIcon"); |
| 133 | wcscpy_s(NID.szTip, sizeof(NID.szTip) / sizeof(NID.szTip[0]), szTip); |
| 134 | VERIFY(OK=Shell_NotifyIcon(NIM_ADD, &NID)); |
| 135 | } |
| 136 | if(!OK) memset(&NID, 0, sizeof(NID)); |
| 137 | return OK; |
| 138 | } |
| 139 | |
| 140 | void RemoveIcon() { |
| 141 | if(!OK) return; |
| 142 | NID.uFlags=0; |
nothing calls this directly
no outgoing calls
no test coverage detected