// TrayIcons.h // // Single icon per use; otherwise, you'll have to // rewrite the m_bTrayIsActivated logic to keep // track of which icon is activated. // // Revision Log // // Date Who SAR Notes // ========== === ======= ===================================== // 2001-10-12 mph Initial coding by Mark Henri of // MPH Software - markhenri@attbi.com // // // #include #include "TrayIcons.h" #include "icon.h" #include "WinException.h" // MyTaskBarAddIcon - adds an icon to the taskbar status area. // Returns TRUE if successful or FALSE otherwise. // hwnd - handle of the window to receive callback messages // uID - identifier of the icon // hicon - handle of the icon to add // lpszTip - ToolTip text // a_uCallbackMessageValue - the windows message that the tray // sends to this program. Usually WM_USER + n // CTrayIcons::CTrayIcons() : m_bTrayIsActivated( FALSE ), _hwnd( NULL ), _id( 0 ) { } CTrayIcons::~CTrayIcons() { if ( _icons.size() > 0 ) { for ( ICONS::iterator i=_icons.begin(); i != _icons.end(); i++ ) { rfc::SmallIcon* si = *i; delete si; } _icons.erase( _icons.begin(), _icons.end() ); } if ( m_bTrayIsActivated ) DeleteTaskBarIcon( _hwnd, _id ); } BOOL CTrayIcons::AddIcon( HINSTANCE i, int iconResId ) { try { rfc::SmallIcon* s = new rfc::SmallIcon( i, iconResId ); _icons.push_back( s ); } catch ( WinException e ) { return FALSE; } return TRUE; } BOOL CTrayIcons::AddTaskBarIcon( HINSTANCE instance, HWND hwnd, UINT uID, int i, LPSTR lpszTip, UINT a_uCallbackMessageValue ) { _hwnd = hwnd; _id = uID; if ( ! AddIcon( instance, i ) ) return FALSE; rfc::SmallIcon* si = _icons[0]; // assume this is only called once for the first one return AddTaskBarIcon( hwnd, uID, *si, lpszTip, a_uCallbackMessageValue ); } BOOL CTrayIcons::AddTaskBarIcon( HWND hwnd, UINT uID, LPSTR lpszTip, UINT a_uCallbackMessageValue ) { _hwnd = hwnd; _id = uID; if ( _icons.size() < 1 ) return FALSE; rfc::SmallIcon* si = _icons[0]; // assume this is only called once for the first one return AddTaskBarIcon( hwnd, uID, *si, lpszTip, a_uCallbackMessageValue ); } BOOL CTrayIcons::AddTaskBarIcon( HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip, UINT a_uCallbackMessageValue ) { _hwnd = hwnd; _id = uID; BOOL res; NOTIFYICONDATA tnid; tnid.cbSize = sizeof( NOTIFYICONDATA ); tnid.hWnd = hwnd; tnid.uID = uID; tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; tnid.uCallbackMessage = a_uCallbackMessageValue; tnid.hIcon = hicon; if ( lpszTip ) lstrcpyn( tnid.szTip, lpszTip, sizeof( tnid.szTip ) ); else tnid.szTip[0] = '\0'; res = Shell_NotifyIcon( NIM_ADD, &tnid ); m_bTrayIsActivated = TRUE; return res; } // To delete an icon from the taskbar status area, fill // a NOTIFYICONDATA structure and send it to the system // when you send a NIM_DELETE message. When deleting a // taskbar icon, specify only the cbSize, hWnd, and uID // members, as the following example shows. // MyTaskBarDeleteIcon - deletes an icon from the taskbar // status area. // Returns TRUE if successful or FALSE otherwise. // hwnd - handle of the window that added the icon // uID - identifier of the icon to delete BOOL CTrayIcons::DeleteTaskBarIcon( HWND hwnd, UINT uID ) { BOOL res; NOTIFYICONDATA tnid; tnid.cbSize = sizeof( NOTIFYICONDATA ); tnid.hWnd = hwnd; tnid.uID = uID; res = Shell_NotifyIcon( NIM_DELETE, &tnid ); m_bTrayIsActivated = FALSE; return res; } BOOL CTrayIcons::ChooseTaskBarIcon( int i ) { if ( i < 0 || i >= _icons.size() ) return FALSE; rfc::SmallIcon* si = _icons[i]; //assert( ModifyTaskBarIcon( _hwnd, _id, *si ); return TRUE; } BOOL CTrayIcons::NextIcon() { static int lastIndex = 0; // assume the first is with initialize lastIndex++; if ( lastIndex >= _icons.size() ) lastIndex = 0; return ChooseTaskBarIcon( lastIndex ); } BOOL CTrayIcons::ModifyTaskBarIcon( HICON hicon ) { return ModifyTaskBarIcon( _hwnd, _id, hicon ); } BOOL CTrayIcons::ModifyTaskBarIcon( HWND hwnd, UINT uID, HICON hicon ) { BOOL res; NOTIFYICONDATA tnid; tnid.cbSize = sizeof( NOTIFYICONDATA ); tnid.hWnd = hwnd; tnid.uID = uID; tnid.uFlags = NIF_ICON; tnid.hIcon = hicon; tnid.szTip[0] = '\0'; res = Shell_NotifyIcon( NIM_MODIFY, &tnid ); return res; } // Implement the following routine in your derived // dialog box. Add the message handler code #ifdef SAMPLE_CODE LRESULT CBRPrintManagerDlg::OnTrayMessage( WPARAM wParam,LPARAM lParam ) { //CListBox* p =( CListBox* )GetDlgItem( IDC_LIST1 ); //ASSERT( p != NULL ); CString cs; UINT uID =( UINT ) wParam; UINT uMouseMsg =( UINT ) lParam; switch ( uID ) { case 1: // arbitrary number assigned to the icon for this window switch ( uMouseMsg ) { case WM_MOUSEMOVE: cs.Format( "%u WM_MOUSEMOVE", uMouseMsg ); break; case WM_LBUTTONDOWN: cs.Format( "%u WM_LBUTTONDOWN", uMouseMsg ); break; case WM_LBUTTONUP: ShowWindow( SW_SHOW ); ShowWindow( SW_RESTORE ); cs.Format( "%u WM_LBUTTONUP", uMouseMsg ); break; case WM_RBUTTONDOWN: cs.Format( "%u WM_RBUTTONDOWN", uMouseMsg ); break; case WM_RBUTTONUP: ShowWindow( SW_MINIMIZE ); ShowWindow( SW_HIDE ); cs.Format( "%u WM_RBUTTONUP", uMouseMsg ); break; case WM_RBUTTONDBLCLK: cs.Format( "%u WM_RBUTTONDBLCLK", uMouseMsg ); break; default: cs.Format( "%u Unknown", uMouseMsg ); } //p->InsertString( 0, cs ); break; } return 1; } #endif