Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC
Article

Dynamic Menu and ToolBar from XML Configuration File

Rate me:
Please Sign up or sign in to vote.
2.76/5 (12 votes)
14 Sep 2008CDDL4 min read 38.6K   800   31   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, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Team Leader UNIQA Bangladesh Design Centre Ltd.
Bangladesh Bangladesh
I like to be driven by my passion.

Interests: Software product development, Advanced Networking, Robotics, Sports Physics, Cricket.

Should you have any interest to change or update on this project, please let me know the result at toufiqbd@gmail.com.

Comments and Discussions

 
GeneralAmazing code Pin
mevoid21-Sep-08 1:22
mevoid21-Sep-08 1:22 

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.