All of you will be pretty familiar with System Tray icons. Today let’s see how we can create one of our own and handle notifications from it.
The following code snippet can be used to create a icon in the system tray:
#define MY_SYSTEM_TRAY_MSG WM_USER + 50
#define TRAY_ICON_01 1
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = GetSafeHwnd();
nid.uID = TRAY_ICON_01;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = MY_SYSTEM_TRAY_MSG;
nid.hIcon = LoadIcon( AfxGetInstanceHandle(), MAKEINTRESOURCE( IDR_MAINFRAME )); // Use any icon you want
lstrcpy( nid.szTip, _T( "Trayed my application!"));
Shell_NotifyIcon(NIM_ADD, &nid);
Now comes the chore of handling the notifications from the icon. For this first add the following entry in the message map:
ON_MESSAGE (MY_SYSTEM_TRAY_MSG, OnSystemTrayIconClick)
Now implement OnSystemTrayIconClick() as follows:
LONG MyClass::OnSystemTrayIconClick (WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case TRAY_ICON_01:
switch (lParam)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
AfxMessageBox( _T( "You may implement a context menu here" ));
break ;
}
break ;
default:
AfxMessageBox("invalid icon id from tray\n");
break ;
}
return 1;
}
No comments:
Post a Comment