with System Tray
with Modal Application
<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (
Creating menus can be done by building the menu statically in resources, or creating it fully dynamically using
CMenu creation operation. But how dynamically add items at runtime to a menu created in resource? And how handle these items dynamically (if we don't know by advance how much they are)? Here is an easy solution.
My sample code shows you how to dynamically add and handle items to a menu in system tray, grabbing these items from an external file. The main advantage is that you can share this file on a network or else, and modify at runtime all items who are in this file.
- First of all, create your menu in resources, including only the static items :
If you want no static items, but a full dynamic menu, just create a menu in the resource with dummy item like this:
- Then, use the ClassWizard to create command handlers for the static items if they are some (
ON_UPDATE_COMMAND_UI if you want to do menu enabling).
e.g. from my sample code you have in the message map of the "MainFrame.cpp" file :
- In the "Mainframe.h" header file, add the followings:
#include "DynamicItems.h" to use DynamicItems class manager, and
Still in "MainFrame.h", modify the message map as follow:
afx_msg void OnExecuteDynamicMenu( UINT nID );
Now, imagine you want the ability to manage a maximum of 10 dynamic items.
(This number is a maximum, you can have less items if you want, but no more than 10 will be handled :
"Mainframe.cpp" file, Add the following line in the message map:
ON_COMMAND_RANGE( ID_POPUP_DynCmd01, <BR> ID_POPUP_DynCmd01 + CDynamicMenu::GetNbMaxItems(), <BR> OnExecuteDynamicMenu )
CDynamicMenu::GetNbMaxItems() method returns Maximum numbers of items which can be handled simultaneously on the menu.
You can increase this static value to be sure your menu will manage a very high number of items, but don't forget you'll be limited by the size of your screen !
This is a rarely-used but very powerful alternative message map macro, and this is the foundation of my dynamic items management.
- Then, in
CMainframe.cpp, add for example the following code:
void CMainFrame::OnExecuteDynamicMenu( UINT nID )
CString strItemText = m_DynamicItems.GetItemText(nID);
CString strItemComment = m_DynamicItems.GetItemComment(nID);
strMsg.Format("Cmd %d selected.\nItem Name: %s\nAssociated data: %s",
(nID -ID_POPUP_DynCmd01), strItemText, <BR> strItemComment );
}This method is called every time you click on a dynamic item in the menu.
So, you have to modify this method to handle menu as you wish.
In the sample code, you'll see that this method contains an easy but very powerful item management... Use it and enhance it as you want !
- When and where do I tell my app to load my items from a file?
Loading items from the file and updating menu in memory is done in one line of code :
UpdateDynamicItems(strFullFilePath);I have decorrellated the process of physically updating data by reading a file, and the process of displaying menu for a much easier use. You can call this method when you think it is necessary : using a timer to refresh items every X minutes, or when the user right-click on an icon in the system-tray, etc... Whenever you want !
- Sample Code:
There is two sample codes :
- A sample code which uses the system tray :
It checks a file every X minutes using a timer to update menu (the file to check and the duration between each check is customizable). You can modify at runtime the selected file, or using the 2 files given with the sample named "TestFile.txt" and "TestFile2.txt". The header of these tests files have comments on how to format datas. When you click on an item, a message box is displayed, telling you what is the name of the selected item, and what is its associated comment.
Usage example: An application which use a shared file containing Name/Email of present people connected to internet ;-)
- A modal application which allow you to load/remove menus when you want:
This sample code shows you how to load/remove menu items on a modal application. It is very easy to implement, and very easy to use, just have a look at the code...
Usage example: Plugin list applications which can be enhanced just by modifying the menu, or sharewares where you can add more menu items when user is regged by sending him the regged menu file, etc...
That's all. Your menu has now the ability to add and manage very easily dynamic items. Just have a look to the samples code to have an idea on how it works.
Use and enjoy. Please let me know of any bugs/mods/improvements that you have found/implemented and I will fix/incorporate them into this class in a future release.
Thanks to Lionel Grenier for ideas on improving this code.
Thanks Chris Maunder for his
CSystemTray class used in this sample code.
Version 1.1 (01/27/2003):
Enhanced and easiest way to manage dynamic items.
Added new sample code with modal app.
Now the menu is really dynamic !
Version 1.0 (05/17/2001):
First "public" version.