Create a Dialog based application and add a menu.
First you need to declare some variables in the the header file of your Dialog.
Now put the following code in the OnInitDialog function
Add user-defined message handler
Now define the body of onTrayNotify as follows
Now if you want to add the functionality that whenever someone hits the cross it should be minimized to the system tray then add the WM_CLOSE event handler and remove the call to the CDialog::OnClose(); and add the following code.
Now in the WM_DESTROY message of the dialog add the following lines to delete the icon from the system tray and clean up the tray.
The sample application has been done in VC++ 7.1 compiler and if you wish to transfer that and view them in older version 7.0 then you need to do the following
First you need to declare some variables in the the header file of your Dialog.
Code: CPP
#define MYWM_NOTIFYICON (WM_USER+1)
//User defined messages.
CMenu m_TrayMenu;
//Displaying menu when right clicked on the system tray. Should be inside the class definition
NOTIFYICONDATA tnd;
//Icon Data
afx_msg LRESULT onTrayNotify(WPARAM, LPARAM);
//Function to handle the user messages
Code: CPP
CString sTip(_T("Hello System Tray"));
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = this->GetSafeHwnd();;
tnd.uID = IDR_MAINFRAME;
//ICON RESOURCE ID
tnd.uFlags = NIF_MESSAGE|NIF_ICON;
tnd.uCallbackMessage = MYWM_NOTIFYICON;
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE (IDR_MAINFRAME));
//ICON RESOURCE ID
lstrcpyn(tnd.szTip, (LPCTSTR)sTip, sizeof(tnd.szTip));
DWORD dwMessage = NIM_ADD;
Shell_NotifyIcon(dwMessage, &tnd);
m_TrayMenu.LoadMenu(IDR_MENU);
//MENU RESOURCE ID.
Code: CPP
ON_MESSAGE(MYWM_NOTIFYICON,onTrayNotify)
Code: CPP
LRESULT CWallpaperDlg::onTrayNotify(WPARAM wParam,LPARAM lParam)
{
UINT uMsg = (UINT) lParam;
switch (uMsg )
{
case WM_LBUTTONDBLCLK:
this->ShowWindow(SW_SHOW);
break;
case WM_RBUTTONUP:
CPoint pt;
GetCursorPos(&pt);
m_TrayMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this);
break;
}
return TRUE;
}
Code: CPP
ShowWindow(SW_HIDE);
Code: CPP
Shell_NotifyIcon(NIM_DELETE,&tnd);
Quote:
1. In SLN files, the 8.0 must be replaced with 7.0.
2. In vcproj files (and only vcproj), the 7.10 part must be replaced by 7.0.

