Do you want to see your application icon on the status area of task bar (we can call this area – “Tray”)?
I am offering a little help. In this post I will give you some code for
Adding your application icon on the tray.
Show balloon for your icon
Show menu on your icon
Remove the icon
To achieve it, we have the API Shell_NotifyIcon
Now add a member variable for the structure NOTIFYICONDATA and for CMenu
NOTIFYICONDATA m_TrayData;
CMenu ObjMenu;
Also define a message
#define WM_TRAY_MESSAGE (WM_USER + 1)
Add icon to tray
BOOL CTrayDlg::OnInitDialog()
{
…………………
// IDR_MENU1 – Required menu which is added in resource
ObjMenu.LoadMenu( IDR_MENU1 );
MoveToTray();
}
void CTrayDlg::MoveToTray()
{
m_TrayData.cbSize = sizeof(NOTIFYICONDATA);
CString cstemp = L“How is it?” ;
swprintf( m_TrayData.szTip, L“%s”, cstemp );
m_TrayData.hWnd = m_hWnd;
m_TrayData.uID = 1;
m_TrayData.uCallbackMessage = WM_TRAY_MESSAGE;
m_TrayData.hIcon = this->m_hIcon;
m_TrayData.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP ;
Shell_NotifyIcon( NIM_ADD, &m_TrayData );
}
Show balloon on the icon
void CTrayDlg::OnButtonShowBalloon()
{
ShowBalloon( L“I am running in this system”, L“Ajeesh”, 1 );
}
void CTrayDlg::ShowBalloon( LPCTSTR szText, LPCTSTR szTitle, UINT uTimeout )
{
m_TrayData.uFlags = NIF_INFO;
_tcsncpy( m_TrayData.szInfo, szText, 256 );
_tcsncpy( m_TrayData.szInfoTitle, szTitle, 64 );
m_TrayData.dwInfoFlags = NIIF_INFO;
m_TrayData.uTimeout = uTimeout;
Shell_NotifyIcon( NIM_MODIFY, &m_TrayData );
}
Show menu on your icon
For this we need a message mapped function
ON_MESSAGE(WM_TRAY_MESSAGE, OnTrayNotify)
void CTrayDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)
{
UINT uID;
UINT uMsg;
uID = (UINT) wParam;
uMsg = (UINT) lParam;
if (uID != 1)
{
return;
}
switch (uMsg )
{
// Show your application on LBtnDBClick on icon
case WM_LBUTTONDBLCLK:
::SetWindowPos( m_hWnd, HWND_TOPMOST,
0, 0, 0, 0,
SWP_NOMOVE|SWP_NOSIZE );
::SetWindowPos( m_hWnd, HWND_NOTOPMOST,
0, 0, 0, 0,
SWP_NOMOVE|SWP_NOSIZE );
ShowWindow( SW_SHOW );
ShowWindow( SW_RESTORE );
break;
// Show Menu on RBtnClick on icon
case WM_RBUTTONDOWN:
case WM_CONTEXTMENU:;
CPoint pt;
GetCursorPos(&pt);
ObjMenu.GetSubMenu(0)->TrackPopupMenu( TPM_BOTTOMALIGN|
TPM_LEFTBUTTON|
TPM_RIGHTBUTTON,
pt.x,pt.y,this );
}
return;
}
Remove icon from the tray
void CTrayDlg::OnCancel()
{
Shell_NotifyIcon( NIM_DELETE, &m_TrayData );
CDialog::OnCancel();
}