In the MFC architecture CDialogBar can provide more flexible ways to create a control bar in the application than traditional toolbars using CToolBar. Apart from standard button like controls of toolbar, a dialog bar can include other windows controls (such as combobox ) and OCX controls as well. They also provide very flexible layout since we can use dialog editor to create a dialog template for the dialog bar.
Limitations of CDialogBar:
However, one limitation of CDialogBar is it lacks suitable initialisation function such as OnInitDialog() which is available in all standard CDialog derived classes. This creates trouble in subclassing windows controls created through dialog template. We need to subclass windows controls with suitable MFC class objects to send messages to the control and to alter their behaviour. Without the help of MFC supplied classes, changing the property of Active X controls will require complex code, not so easy to comprehend.
With the help of Class Wizard, subclassing the controls created from dialog template, becomes very easy as the Class Wizard automatically adds DDX_Control( CDataExchange* pDX, int nIDC, CWnd& rControl ); statements in DoDataExchange() override of CDialog derived class. DoDataExchange is called by UpdateData( BOOL bSaveandValidate) function, which in turn is called by OnInitDialog() in response to WM_INITDIALOG message.
All these steps of initialising does not automatically occur in the CDialogbBar as it is derived from CControlBar and not from CDialog.
We write a class which does the job of initialising the dialog bar, which can be reused whenever we want to create a dialog bar. Let's call this class CDialogBarEx which would provide functionality of initialising the dialog bar.
The function which initialise the dialog bar should normally be called immediately after the dialog bar is created. We can not use OnCreate() override alone, for this purpose as OnCreate is called during the window creation ( before CreateEx returns ) and not after that. We so we just post a custom WM_INITDIALOGBAR message ( WM_USER + 1)) to the dialog bar itself from our override of OnCreate. This assures that handler for WM_INITDIALOGBAR will called immediately after the dialog bar is fully created and ready to use. We also write handler for WM_INITDIALOG bar ( lets call it OnInitHandler() ) which should call a virtual function OnInitDialogBar() and from the same handler we call UpdateData(FALSE) before we call OnInitDialogBar() ;
The bottom line:
- Users of this class can put DDX_Control statements in side DoDataExchange, as done by Class Wizard for CDialog and its derived classes.
- Users can override OnInitDialogBar to perform other custom initialisation.
How to use CDialogBarEx:
- Add files DialogBarEx.h and DialogBarEx.cpp to your Visual C++ project
- Derive your own class ( for example say "CYourClass" ) from CDialogBarEx ;
- Add DDX_Control statements in your override of DoDataExchange as done by class wizard, to subclass the dialog bar controls with MFC classes.
- Override OnInitDialogBar() function to do any further initialisation of this class.
Other steps to create the dialog bar are the same as recommended by MFC documentation..
- Create dialog resource template . Set child property on, visible off.
- Declare one CDialogBarEx derived member in CMainFrame class;
- In the CMainFrame::OnCreate override call create function of CDialogBarEx derived class.
The demo contains both source and sample program. The demo shows how to set hyperlink in dialog bar using CLabel and how to set initial null ( null means not yet defined and not NULL of C++) value in SysDateTime32 control.
To make this demo I have used following classes from www.codeguru.com
- CDateTimeCtrl class written by Chris Maunder
- CLabel class downloaded from codeguru with modifications.
- I have also incorporated a modification to CLabel class suggested to me by Ed Dixon of Mountain System Inc. via e-mail.