Table of Contents

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

  • First we select ‘Single document’ option.
  • Next, we uncheck ‘Document/View architecture’ support.
  • 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:

    //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 // | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || // !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) // TRACE0("Failed to create toolbar\n"); // return -1; // fail to create
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //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
    //     | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    //     !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    //{
    //     TRACE0("Failed to create toolbar\n");
    //     return -1;      // fail to create
    //}
    //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 //m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); //EnableDocking(CBRS_ALIGN_ANY); //DockControlBar(&m_wndToolBar);

    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

    //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", RBBS_GRIPPERALWAYS | RBBS_CHILDEDGE );

    Source Code : Download MFC DialogBar Example From Google Drive

    Categories: MFC

    Tags: , , , ,

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