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:
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:
- 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:
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.
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:
1 2 3 4 5 |
//Sample 01: Required declarations private: CToolBar m_toolbar; CDialogBar m_dlgbar; CReBar m_rebar; |
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:
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 //} |
Since we commented out the code which displays MFC’s default toolbar, the below-specified code also needs to be commented out:
1 2 3 4 5 6 7 |
//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.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:
1 2 3 |
//Sample 04: Create the rebar control. Specify MainFrame window //as it's Parent m_rebar.Create(this); |
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:
1 2 3 |
//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); |
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
.
1 2 3 |
//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: AddBar, CDialogBar, CDialogBar::Create, CRebar, IDD_DIALOGBAR