Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamic Menu and ToolBar from XML Configuration File

14 Sep 2008 1  
This article is about an application that parses a XML Configuration File and creates Menus and ToolBar dynamically.

Introduction

This article's main objective is to show another aspect of dynamically creation of Menu and ToolBar by parsing XML configuration file. In MFC, whenever there is a requirement to make Menu and Toolbar control dynamic, we often get hung up on the point of defining methods for that Menu and ToolBar.

This article is going to show you a sample of code in VC++ where I have created dynamic Menus and ToolBar using MFC and GDI. I choose the configuration file for this application to be XML file. The command messages associated with the Menu items and ToolBar items can be mapped with a function as per the job selected by user.

Background

As MFC developer, you may know that for creating dynamic controls you need to assume constant(Resource_ID). You also have to specify ON_COMMAND(RESOURCE_ID, methodName) and ON_BN_CLICKED (RESOURCE_ID, methodName) in BEGIN_MESSAGE_MAP(). As the item from menu or toolbar is clicked, the corresponding function will be invoked.

You will get an opportunity to work on my code in CMainFrame::CmdMap() function to redefine and map the events using delegates for a more dynamic environment. If you know .NET, then you must be familiar with delegates -- i.e. pointers to functions -- that have a significant role in defining any particular method for any control. You can use delegates to a function, to handle the corresponding menu and toolbar item event.

This time, I have covered only the ON_COMMAND() and ON_BN_CLICKED() aspect of MFC with my message map. I have defined my own message map and assigned value to the controls, as required. In the future I'll try to show the example for delegates for the events of MFC so that it will become flexible for events.

Program Description

The program in this article is named as XmlMenuApp. XmlMenuApp is a program that draws menu and toolbar in the current window by parsing the configuration for the items from a XML file. The program is developed in Visual Studio 2005 using VC++ language.

In this application, configuration file name: "MenuToolBarConf.xml"

User can perform necessary changes in the xml file according to users requirements for menu and toolbar items.

The menu & submenu items are followed by "menuentry" tag

The toolbar button names are followed by "toolbarentry" tag

The toolbar image path is followed by "imgpathentry" tag

Note: Bitmap image name should be same as the toolbar button names. Users must specify image path entry for toolbar buttons. If one image is not

found for any toolbar button then the program halts.

e.g. Bitmap image path example: "E:\\Projects_VS\\Dyn_image\\"

Here we used "Dyn_image" folder for the toolbar button images of the application.

Using the code

I have tried to make my code as much readable and clean as possible. Following hints will help you to understand the code very clearly. Please download the code and go through according to following architecture.

The following classes are the main and controller of the program
---------------------------------------------------------------
1. CXmlMenuToolBarApp
2. CMainFrame

The following classes are the Xml file parser of the program
---------------------------------------------------------------
3. CXmlElement
4. CXmlDocument

The following classes creates the menu and toolbar of the program
---------------------------------------------------------------
5. CXmlMenu
6. CXmlToolBar

The following class is to view the toolbar buttons of the program
---------------------------------------------------------------
7. CXmlToolBarView
---------------------------------------------------------------

The Model classes are not implemented as the action are not defined for menu and toolbar items. The actions can be mapped from the controller to Model class's member functions.

Note: The attached compressed file contains the followings,

  1. Source Code
  2. Image folder for the bitmap used in this application
  3. ReadMe.txt
You need to change the image path in the “MenuToolBarConf.xml” file for the toolbar buttons before running the program.
Please rebuild the program to run.
For information:
If you want to add new images then the following path of Visual Studio installed drive might help,
"\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary”
You are recommended to use 16 bit and 24 bit bitmap images for toolbar button.
Please also note:
Users of this project may find some unexpected (but not severe) memory leaks. I would like to request users to handle the circumstances as home work. Moreover, I am fixing the issues and I will update this article as soon as possible.

Points of Interest

The project developed based on MFC and GDI. In CXmlToolBarView::OnPaint() function, I have used TransparentBlt() function to draw the toolbar button image transparrently and transparent color is hardcoded here as the RGB(255,0,255).

// CXmlToolBarView painting bitmap from file into dest dc

void CXmlToolBarView::OnPaint(CDC *dc, CDC *dcMem, LPCTSTR szFilename, int x, int y)
{
	CBitmap *bmpSrc = new CBitmap;
	CBitmap *bmpSrcOld = new CBitmap;
	CDC dcMemSrc;

	//load bitmap from file
	!this->LoadBmp(bmpSrc, szFilename);

	dcMemSrc.CreateCompatibleDC(dc);
	bmpSrcOld = dcMemSrc.SelectObject(bmpSrc);

	// transparent blit to the dest. as the megenda has been the transparent color
	dcMem->TransparentBlt(x,y,16,16,&dcMemSrc,0,0,16,16,RGB(255,0,255));

	//holds previous state of the dc
	dcMemSrc.SelectObject(bmpSrcOld);
	dcMemSrc.DeleteDC();
	
	//if image not loaded successfully then follwing lines prevent some illeagal operation
	if(bmpSrcOld)
		bmpSrcOld->DeleteObject();

}
		

The site is still under construction...

Acknowledgement

For Acknowledgement, I am obliged to Mr. Shahadatul Hakim and Mr. Mukit for advising me the topic to work with.

I want to thank Mr. Tom Thomas from KTS INFOTECH PVT LTD. for helping me with some issues in this project.

For Parsing XML file I want to thank John Melas. The code for XML file handling is taken from Pablo van der Meer. Some customization took place for porting the code into the project

My gratitude also goes for Rodolfo Ortega for his contribution to Code project with the Article “How to Create a Dynamic ToolBar” as I have customized his code to make it more dynamic.

For GDI code used here, I took help from Chris Becke's Bitmap Basics - A GDI tutorial.

License

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