|
|
Title |
Creating an icon in the tool tray.
|
Summary |
How to create an icon and popup menu for your application in the system tool tray. |
Contributor |
John McTainsh
|
Published |
29-Sep-2000 |
Last updated |
29-Sep-2000 |
|
|
Download Tool Tray class files - 3 Kb
Description.
Placing an icon in the tool tray is quite a simple task and can
make it easier for the user to access your application without wasting screen space.
This class is just a light wrapper around the Windows 95 system
tray icon notification facility. The constructor requires a CWnd
object pointer in order to deliver the user-defined notification
message, which must also be provided. It is up to the caller to
verify that a valid notification index is provided.
How to setup.
Add the following definition to StdAfx.h and add the two files in
the zip file to the project.
#define WM_USER_TOOLTRAY (WM_USER+1) //Tool tray message
In the header of the window to recieve messages from the tool tray add the following
#include "TrayNot.h" //CToolTray class def.
Define in the private section of the class.
//Attributes
private:
CTrayNot *m_pTrayNot; //Access to the tool tray icon
At the end of the message map add out handler.
//}}AFX_MSG
afx_msg LONG OnMyTrayNotify( WPARAM wParam, LPARAM lParam );
DECLARE_MESSAGE_MAP()
Back in the Implementation file for the window, in the Constructor of the window object
, m_pTrayNot( NULL )
{
...
At the end of the message map add our message handler
//}}AFX_MSG_MAP
ON_MESSAGE(WM_USER_TOOLTRAY, OnMyTrayNotify)
END_MESSAGE_MAP()
In the Create or InitDialog functions
create the object as follows;
//Setup the tool tray
m_pTrayNot = new CTrayNot( this, WM_USER_TOOLTRAY,
_T("My words here") );
Finally add the hendler for the popup menu that is to be displayed. Note the message ID's
will need to be created along with the menu suitable for your application.
/////////////////////////////////////////////////////////////////////
// Message to Handle the notification message from the Tray
// Either show the dialog OR a menu
LONG CDlgAWWStuffer::OnMyTrayNotify( WPARAM wParam, LPARAM lParam )
{
TRACE( _T("CDlgAWWStuffer::OnMyTrayNotify( %d, %ld )\n"),
wParam, lParam );
switch ( lParam )nd the
{
//Do nothing
case WM_LBUTTONDBLCLK:
MessageBeep( -1 );
case WM_LBUTTONDOWN:
MessageBeep( -1 );
break;
//Activate the pop up menu
case WM_RBUTTONDOWN:
//Create the popup menu with mini icons
CMenu menu;
VERIFY( menu.CreatePopupMenu() );
menu.AppendMenu(MF_STRING, ID_FILE_SEARCHFORITEM,
_T("Show &Find") );
menu.AppendMenu(MF_STRING, ID_FILE_SELECTBYCATEGORY,
_T("Show &Category") );
menu.AppendMenu(MF_STRING, ID_APP_REALLY_EXIT,
_T("E&xit") );
//Position and Display
POINT pt ;
GetCursorPos ( &pt ) ;
SetForegroundWindow();
int nSelection = menu.TrackPopupMenu (
TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON,
pt.x, pt.y, AfxGetMainWnd(), NULL );
menu.DestroyMenu();
switch( nSelection )
{
case ID_FILE_SEARCHFORITEM:
case ID_FILE_SELECTBYCATEGORY:
//OnHotKey();
//OnSelectView( nSelection );
break;
case ID_APP_REALLY_EXIT:
OnAppReallyExit();
break;
}
}
break;
}
return 0;
}
|