with Modal Application
Environment: VC5, Windows 95, Windows NT
Introduction
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.
Method
- 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_COMMAND
andON_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)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_POPUP_OPTIONS, OnPopupOptions)
…
ON_COMMAND(ID_POPUP_EXIT, OnClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
- 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
afx_msg void OnExecuteDynamicMenu( UINT nID );
DECLARE_MESSAGE_MAP()
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:
//}}AFX_MSG_MAP
ON_COMMAND_RANGE( ID_POPUP_DynCmd01, ID_POPUP_DynCmd01 +
CDynamicMenu::GetNbMaxItems(),
OnExecuteDynamicMenu )
END_MESSAGE_MAP()
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.
- 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 );
AfxMessageBox(strMsg);
}
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 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!
- Sample Code:
There are two sample codes:
- 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
- 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…
- A sample code that uses the system tray:
Conclusion
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:dexcoffier@yahoo.fr
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
Acknowledgements
Thanks to Lionel Grenier (lionel.grenier@lebios.com) for ideas on improving this code.
Thanks also to Chris Maunder (cmaunder@mail.com) for his CSystemTray class used in this sample code.
Downloads
Download demo project (system tray) 65 Kb
Download demo project (modal app) 46 Kb
Download source 4 Kb