MFC DialogBar Example

MFC DialogBar Explained With Example

1. Introduction to MFC DialogBar Control

In this example, we will see how to create and use MFC DialogBar Control. The DialogBar acts like a toolbar. Unlike toolbar, a dialog bar can have controls which we can place in it treating it as dialog. One can dock the DialogBar like a toolbar. To better visualize it, we can think of a tool bar with a radio button, a checkbox and a combo box in it. What we just now imagined can be easily achieved through a DialogBar control.

In this example we will design an MFC DialogBar, then we will place that in a Rebar Control so that we can dock it on the Main Frame window of the SDI application.

2. About The Example

The example that we will make is shown below:

MFC DialogBar Example
MFC DialogBar Example

It is a single document interface (SDI) application. Note that, for the end user the Dialog bar displayed on the example look like a toolbar having the Combo box and Edit Box. There is nothing more to explain here. Now we will start the implementation.

3. Preparing the Example App

First, we create the MFC SDI Application without document view support. In the wizard we can disable the Document view support, which is shown below:

Creating MFC SDI Application
Creating MFC SDI Application
  1. First we select ‘Single document’ option.
  2. Next, we uncheck ‘Document/View architecture’ support.
  3. Finally we uncheck the ‘use Unicode libraries’.

Next, we can accept all the default options in the wizard to create an SDI Application with no document/view support. Once you are in the IDE, build the project and launch it to make sure everything is perfect.

4. Adding DialogBar Resource

The next step is adding the dialog bar resource to the project. To add dialog bar resource, right-click on the project name in the resource pane (looking at the previous article’s video to add a toolbar, you will understand how to add dialog bar), then select add a new resource. From the displayed dialog, select the option IDD_DIALOGBAR and click New. This is shown below:

Add Dialog Bar Resource IDD_DIALOGBAR
Add Dialog Bar Resource IDD_DIALOGBAR

After adding the MFC DialogBar resource to the project, we double click the added resource and start placing the controls in it. The below screenshot helps in designing our DialogBar Control. After designing the DialogBar we will move to writing the code to display the dialog bar.

Setting-Up MFC DialogBar
Setting-Up MFC DialogBar

5. Displaying MFC DialogBar Control

5.1 Member Variables

First, we declare the dialog bar along with other required MFC Objects in the MainFrm.h. Note, we declared <var>CReBar</var> object also. Like the previous article, we will host the CDialogBar object in the CReBar control. Below is the declaration:

5.2 Comment Default Toolbar

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) is the function that we will change now. Since we want to display only the dialog bar on the Main window, we comment the code that display MFC’s default toolbar. The code is below:

Since we commented out the code which displays MFC’s default toolbar, the below-specified code also needs to be commented out:

5.3 Create MFC Rebar Control

First, we create the MFC’s ReBar Control by passing the this pointer as a parameter to the Create function. The parameter passed in to the parent to the Rebar Control is container window. Here, the Main Window of our application is the parent for the Rebar control. This code is below:

5.4 Create MFC DialogBar Control From Resource ID

Now, we create the MFC DialogBar control by making use of the resource edited template denoted by IDD_DLGBR_1. We used the Create() function of the CDialogBar to create it from the resource id IDD_DLGBR_1. The first parameter specifies that the CReBar control instance is the parent for the CDialogBar instance which we are creating now. The Flag CBRS_ALIGN_TOP specifies that DialogBar Control will be aligned on top of the Main Frame window. Last parameter is a number that acts as the command id. We used same resource id as command id. Code is below:

5.5 Add DialogBar to Rebar

Just like how we added the Toolbar controls to the Rebar in the previous example, the dialog bar m_dlgbar is added to it. You can refer the previous article to know more about the AddBar() function. But from here you can know that using the AddBar() function you can add CToolBar as well as CDialogBar to the CReBar.

Source Code : Download MFC DialogBar Example From Google Drive

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.