Click here to Skip to main content
13,457,971 members
Click here to Skip to main content
Add your own
alternative version


85 bookmarked
Posted 28 May 2001

Dynamic Items

, 24 Feb 2003
Rate this:
Please Sign up or sign in to vote.
A class and an easy way to dynamically add items stored in a file to a menu

with System Tray

without dynamic item with dynamic items read from a file With new items read from the same modified file

with Modal Application

without dynamic item with dynamic items read from a file With new items read from the same modified file

<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->


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.


  1. First of all, create your menu in resources, including only the static items :
    Menu in resources 
    If you want no static items, but a full dynamic menu, just create a menu in the resource with dummy item like this:
    Dummy Menu in resources 
  2. Then, use the ClassWizard to create command handlers for the static items if they are some (ON_COMMAND, and 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 :

    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)


  3. In the "Mainframe.h" header file, add the followings:
      #include "DynamicItems.h" to use DynamicItems class manager, and
      CDynamicItems m_DynamicItems;

    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 :

    In the "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 )

    The 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.

  4. Then, in CMainframe.cpp, add for example the following code:
    void CMainFrame::OnExecuteDynamicMenu( UINT nID )
            	// Retrieve the selected dynamic item Name
            	CString strItemText = m_DynamicItems.GetItemText(nID);
            	// Retrieve the selected dynamic item associated comment
            	CString strItemComment = m_DynamicItems.GetItemComment(nID);
            	// Display it.
            	CString strMsg="";
            	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 !

  5. 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 :

    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 !
  6. Sample Code:

    There is two sample codes :

    1. 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 ;-)

    2. 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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Excoffier
Team Leader
France France
No Biography provided

You may also be interested in...

Comments and Discussions

Generalafxwin1.inl line: 1038 debug assertion failed Pin
Rocky B.27-Aug-07 21:22
memberRocky B.27-Aug-07 21:22 
GeneralRe: afxwin1.inl line: 1038 debug assertion failed Pin
kimtaehee25-Mar-08 15:45
memberkimtaehee25-Mar-08 15:45 
GeneralDynamic change of MaxItems Pin
Vjys29-Jul-07 20:25
memberVjys29-Jul-07 20:25 
GeneralWindows 98: ON_UPDATE_COMMAND_UI_RANGE not called Pin
ibthas17-Jun-07 23:02
memberibthas17-Jun-07 23:02 
First if all, thx for this great class!

Unfortunately, there seem to be some issues under Win98: Neither the Modal-Dialog example nor my own dialog-based app shows the dynamic menu items there.

In your modal-based example, the LoadMenu command doesn't show the file dialog. In my own app, the dynMenu items are just "shrinked", they show no text at all and have a very small line height. Futhermore, the ON_UPDATE_COMMAND_UI_RANGE macro seems to not call the associated handler method at all.

Both apps work great under XP. The Systray example works on Win 98 and XP.

I found several potential reasons for these issues on the WWW: Resource IDs over 3267 can cause problems on Win98, Modal-Dialog-Apps have problems to update menus without an ON_WM_INITMENUPOPUP handler (which your dialog-based example doesn't have neither?!) the update-ui-range macro fails if it's menu IDs have no subsequent values (no problem here), and there could be unicode problems.

As your example seems to be compiled under VC6 with no unicode support and the resources are in the same range as in your systray example - why does the first example fail, while the other one works?

Does anybody have any ideas?

QuestionCan .Dat file be unicode Pin
nix32111-Jul-06 10:01
membernix32111-Jul-06 10:01 
QuestionHow to color the menu bar Pin
Alex Evans20-Jun-04 14:44
memberAlex Evans20-Jun-04 14:44 
GeneralMenu bar colouring Pin
Alex Evans20-Jun-04 14:24
memberAlex Evans20-Jun-04 14:24 
Generald:\down\2_code\DynItems_modal_demo\DynItems_modal_demo\DynamicItems.h(146): fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory Pin
haoshenghan17-Jun-04 2:05
memberhaoshenghan17-Jun-04 2:05 
GeneralRe: d:\down\2_code\DynItems_modal_demo\DynItems_modal_demo\DynamicItems.h(146): fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory Pin
Bamaco29-Feb-05 5:06
memberBamaco29-Feb-05 5:06 
AnswerRe: d:\down\2_code\DynItems_modal_demo\DynItems_modal_demo\DynamicItems.h(146): fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory Pin
Renny[RuS]10-Nov-05 22:46
memberRenny[RuS]10-Nov-05 22:46 
GeneralRe: d:\down\2_code\DynItems_modal_demo\DynItems_modal_demo\DynamicItems.h(146): fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory Pin
piotrmb25-Apr-06 4:44
memberpiotrmb25-Apr-06 4:44 
GeneralRe: d:\down\2_code\DynItems_modal_demo\DynItems_modal_demo\DynamicItems.h(146): fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory Pin
Renny[RuS]25-Apr-06 23:08
memberRenny[RuS]25-Apr-06 23:08 
QuestionMemory leak? Pin
silorenz10-Mar-04 16:41
membersilorenz10-Mar-04 16:41 
AnswerRe: Memory leak? Pin
silorenz12-Mar-04 1:39
membersilorenz12-Mar-04 1:39 
GeneralChange the &quot;Dyn Menu&quot; entry Pin
Michael Klim15-Jan-04 9:46
memberMichael Klim15-Jan-04 9:46 
GeneralRE: Location of TestFiles Pin
ltayyebi27-Aug-03 5:55
memberltayyebi27-Aug-03 5:55 
GeneralRe: RE: Location of TestFiles Pin
David Excoffier27-Aug-03 7:51
memberDavid Excoffier27-Aug-03 7:51 
GeneralMDI Bug/Assert Fix Pin
Sully27-Feb-03 11:10
memberSully27-Feb-03 11:10 
QuestionHot Coffee? Pin
Jörgen Sigvardsson24-Feb-03 21:36
memberJörgen Sigvardsson24-Feb-03 21:36 
AnswerRe: Hot Coffee? Pin
Andreas Saurwein24-Feb-03 23:12
memberAndreas Saurwein24-Feb-03 23:12 
GeneralGreat Job & little improvement Pin
lebios31-Dec-02 9:54
memberlebios31-Dec-02 9:54 
GeneralRe: Great Job & little improvement Pin
EPHERE6-Feb-03 11:45
memberEPHERE6-Feb-03 11:45 
GeneralRe: Great Job & little improvement Pin
David Excoffier7-Feb-03 0:57
memberDavid Excoffier7-Feb-03 0:57 
GeneralPerformed an illegal operation... Pin
King Coffee4-Nov-02 12:26
memberKing Coffee4-Nov-02 12:26 
GeneralCool mais Pin
Moz29-May-01 22:07
memberMoz29-May-01 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180323.1 | Last Updated 25 Feb 2003
Article Copyright 2001 by David Excoffier
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid