如何制作DialogBar

类别:VC语言 点击:0 评论:0 推荐:

 

  As we have noticed, the limitation of the tool bar is that when we design a tool bar from resource, only bitmap buttons with the same size can be included. If we try to modify the size of one bitmap button, the size of all other buttons will be automatically adjusted. If we want to include controls other than buttons, we need to write code to add them dynamically.

  If we want to implement a control bar that contains other types of common controls starting from resource editing, we need to use another type of control bar: dialog bar. In MFC, the class that can be used to implement this type of control bar is CDialogBar.

  Like tool bar, dialog bar is also derived from control bar. Both of them share some common features. For example, both types of control bars can be either docked or floated, and they all support tool tip and flyby implementation. The difference between tool bar and dialog bar is that they are designed to contain different types of controls: while tool bar is more suitable for containing a row of bitmap buttons with the same size, dialog bar can be implemented to contain any type of controls that can be used in a dialog box.

  Implementing dialog bar is similar to that of dialog box. The first step is to design a dialog-template resource. We can add buttons, edit boxes, combo boxes, even animate controls to a dialog bar.

  Dialog bar shares the same type of resource with dialog box. So when starting to create resource for dialog bar, we first need to add a “dialog” type resource to the application (In order to do this, we can execute Insert | Resource… command, then select “Dialog” from the popped up “Insert Resource” dialog box). When specifying the dialog properties, we must set “child” and “no border” styles. This can be customized in “Dialog Properties” property sheet.

  Adding a dialog bar to the application is similar to that of a tool bar. First we need to declare a variable in class CMainFrame. Then within function CMainFrame::OnCreate(), we can call the member functions of CDialogBar and CFrameWnd to create the dialog bar, set its styles and dock it.

  The following lists necessary steps of adding this dialog bar:

1)      Use CDialogBar to declare a new variable m_wndDialogBar in CMainFrame class:

class CMainFrame : public CFrameWnd

{

protected:

...

    CDialogBar m_wndDialogBar;

...

}; 

2) In function CMainFrame::Create(), call CDialogBar::Create() to create the dialog bar window. When doing this, we need to provide the pointer of its parent window, the dialog template ID, styles and the control ID of the dialog bar. Here, the control ID could be a different number from its template ID, so long as it is not being occupied by other resources. The following code fragment shows how this function is called in the sample:

if(!m_wndDialogBar.Create(this,IDD_DIALOG_COLORBAR,CBRS_BOTTOM | CBRS_TOOLTIPS | CBRS_FLYBY,IDD_DIALOG_COLORBAR))

{

TRACE0("Failed to create toolbar\n");

return -1;

}

3) Enable docking by calling function CDialogBar::EnableDocking(), dock the dialog bar by calling function CMainFrame::DockControlBar():

...

    m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);

    DockControlBar(&m_wndDialogBar); 

Because class CDialogBar is derived from CControlBar, in step 3, when we call function CDialogBar::EnableDocking() to enable docking for the dialog bar, we are actually calling function CControlBar::EnableDocking(). This is the same with that of tool bar. Because of this, both tool bar and dialog bar have the same docking properties.

By compiling and executing the sample at this point, we can see that the dialog bar is implemented, which is docked to the bottom border at the beginning. We can drag the dialog bar and dock it to other borders. As we do this, we may notice the difference between dialog bar and tool bar: while the size of the tool bar will be automatically adjusted when it is docked differently (horizontally or vertically), the size of dialog will not change under any condition. The reason for this is that a dialog bar usually contains irregular controls, so it is relatively difficult to adjust its size automatically. By default, dynamic size adjustment is not supported by class CDialogBar. If we want our dialog bar to support this feature, we need to override function CDialogBar::CalcDynamicLayout().

To prevent a dialog bar from taking up too much area when it is docked to left or right border, we can put restriction on the dialog bar so that it can only be docked to top or bottom border. To implement this, we can change the style flag from CBRS_ALIGN_ANY to CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM when calling function CDialogBar::EnableDocking() in step 3 discussed above.

 

 

本文地址:http://com.8s8s.com/it/it3158.htm