123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #include "stdafx.h"
- #include "SystemTray.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNAMIC(CSystemTray, CWnd)
- CSystemTray::CSystemTray()
- {
- ZeroMemory(&m_NotifyIconData, sizeof(m_NotifyIconData));
- }
- CSystemTray::~CSystemTray()
- {
-
- m_NotifyIconData.uFlags = 0;
- Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
-
- DestroyWindow();
- }
- BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
-
-
- END_MESSAGE_MAP()
- BOOL CSystemTray::Create(UINT uCallbackMessage, LPCTSTR lpszToolTip, HICON hIcon, UINT uID)
- {
-
- if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), "CSystemTray Dummy Window", WS_POPUP, 0,0,0,0, NULL, 0))
- return FALSE;
-
- m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
- m_NotifyIconData.hWnd = m_hWnd;
- m_NotifyIconData.uID = uID;
- m_NotifyIconData.hIcon = hIcon;
- m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- m_NotifyIconData.uCallbackMessage = uCallbackMessage;
- lstrcpy(m_NotifyIconData.szTip, lpszToolTip);
-
- return Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
- }
- BOOL CSystemTray::SetIcon(UINT nIDResource)
- {
- HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
- m_NotifyIconData.uFlags = NIF_ICON;
- m_NotifyIconData.hIcon = hIcon;
- return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
- }
- BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd)
- {
- m_NotifyIconData.hWnd = pWnd->GetSafeHwnd();
- m_NotifyIconData.uFlags = 0;
- return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
- }
- LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam)
- {
-
- if (wParam != m_NotifyIconData.uID)
- return 0L;
- CWnd *pMainWnd = AfxGetMainWnd();
- switch(LOWORD(lParam))
- {
- case WM_RBUTTONUP:
- {
-
- CMenu menu, *pSubMenu;
- if (!menu.LoadMenu(m_NotifyIconData.uID))
- return 0;
-
- pSubMenu = menu.GetSubMenu(0);
- if (pSubMenu == NULL)
- return 0;
- CPoint pos;
- GetCursorPos(&pos);
-
- pSubMenu->TrackPopupMenu(0, pos.x, pos.y, pMainWnd, NULL);
- menu.DestroyMenu();
- break;
- }
-
- case WM_LBUTTONDBLCLK:
- {
-
- pMainWnd->ShowWindow(SW_SHOW);
- pMainWnd->SetForegroundWindow();
- break;
- }
- default:
- break;
- }
- return 1;
- }
- LRESULT CSystemTray::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- if (uMsg == m_NotifyIconData.uCallbackMessage)
- {
- return OnTrayNotification(wParam, lParam);
- }
- return CWnd::WindowProc(uMsg, wParam, lParam);
- }
|