May 27, 2013

[ MFC ] - CDialogbar Example: Diaplaying combobox in Dialog Bar Control

1. Introduction to DialogBar example

In this example, we are going to see how do we create and use DialogBars. The dialog bar acts like a toolbar. Unlike toolbar, a dialog bar can have controls that can be placed in a normal dialog. The dialog bar can be docked like a toolbar. To better explain, think of a toolbar with a radio button, a checkbox and a combo box in it. What you just now imagined can be easily achieved through a dialog bar control.

In this example we are going to design a dialog bar, then we will place that in a Rebar control that will be docked on the Main Frame window of the SDI application.

2. About the Example

The example that we are going to make is shown below:

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. Let us start the implementation.

3. Create the SDI application

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

1. First “Single document” option is picked
2. Next, the check mark in the “Document/View architecture” support is removed
3. Then “use Unicode libraries” also un-checked.

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

4. Add 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:

Once dialog bar resource is added to the project, double click the added resource and start placing the controls. The below given screenshot helps in designing your dialog bar control. Once this designing is done, we will move to writing the code to display the dialog bar.

5. Source Code - Implementation

1) First, declare the dialog bar along with other required MFC Objects in the MainFrm.h. Note that we have declared CReBar instance also. Like the previous article, we are going to host the dialog bar object the CReBar control. Below is the declaration:
//Sample 01: Required declarations
       CToolBar m_toolbar;
       CDialogBar m_dlgbar;
       CReBar m_rebar;

2) int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) is the function that we are going to modify now. Since we want to display only the dialog bar on the Main window, the code that display MFC’s default toolbar is commented out as shown below:

//Sample 02: No need to create Main Toolbar.
//                  You can even remove it from the
//                  Resource
//if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
//     !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
//     TRACE0("Failed to create toolbar\n");
//     return -1;      // fail to create

3) Since we commented out the code that displays MFC’s default toolbar, the below-specified code also need to be commented out.

//Sample 03: I will take care of displaying the Dialogbar in the Toolbar
//// TODO: Delete these three lines if you don't want the toolbar to be dockable

4) First, create the MFC’s ReBar Control by passing the “this” pointer as a parameter to the Create Function. The parameter passed in the parent to the rebar control. So the Main Window of our application is the parent for the Rebar control. This code is shown below:

//Sample 04: Create the rebar control. Specify MainFrame window as it's Parent      

5) Next, the Dialog Bar control is created by making 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 we are creating now. The Flag CBRS_ALIGN_TOP specifies that Dialog bar control will be aligned on top of the Main Frame window. The last parameter can be a number that acts as the command id. I used same resource id as command id.

//Sample 05: Create the Dialog Bar and specify that m_rebar as it's Parent
m_dlgbar.Create(&m_rebar, IDD_DLGBR_1, CBRS_ALIGN_TOP, IDD_DLGBR_1);

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

//Sample 06: Add the dialog bar control to the Rebar
m_rebar.AddBar(&m_dlgbar, RGB(255,255,0), RGB(0,0,255), "Dbar 1"


Source Code : Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Friend :)