Dynamic Items

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

Environment: VC5, Windows 95, Windows NT


Creating menus can be done by building the menu statically in resources, or creating it fully dynamicaly by using the CMenu creation operation. But how can we dynamically add items at runtime to a menu created in a resource? And how can we 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 elsewhere, and modify it at runtime all items that 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).

    For example, 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 following:

      #include “DynamicItems.h”

    to use DynamicItems class manager, and

      CDynamicItems m_DynamicItems;

    Still in “MainFrame.h”, modify the message map as follows:

    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 fewer 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:

    OnExecuteDynamicMenu )


    The CDynamicMenu::GetNbMaxItems() method returns the maximum numbers of items that 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 that 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,
    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 decorelated the process of physically updating data by reading a file, and the process of displaying menu for 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-clicks an icon in the system-tray, and so forth… Whenever you want!

  6. Sample Code:

    There are two sample codes:

    1. A sample code that uses the system tray:

      It checks a file every X minutes using a timer to update the menu (the file to check and the duration between each check is customizable). You can modify the selected file at runtime, or use the two files given with the sample named “TestFile.txt” and “TestFile2.txt”. The headers of these test 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/E-mail of present people connected to the Internet

    2. A modal application that allows 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 that can be enhanced just by modifying the menu, or shareware where you can add more menu items when user is regged by sending him the regged menu file, and so forth…


That’s all. Your menu has now the ability to add and manage very easily dynamic items. Just have a look to the sample code to have an idea of 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.

E-mail:[email protected]
My Official Web Site:http://www.excoffier.org
Participate in my marriage! Make a (small) donation and drop me a line to wish me success in my new life: http://www.sendmeaneuro.com


Thanks to Lionel Grenier ([email protected]) for ideas on improving this code.

Thanks also to Chris Maunder ([email protected]) for his CSystemTray class used in this sample code.


Download demo project (system tray) 65 Kb
Download demo project (modal app) 46 Kb
Download source 4 Kb

Posting History

More by Author

Must Read