11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1. Tool Bar and Dialog Bar<br />

Class CControlBar has two member functions dealing with control bar layout: CControlBar::<br />

CalcFixedLayout(…) and CControlBar::CalcDynamicLayout(…). The first function returns the fixed size<br />

of a control bar, which is determined from the resource of a control bar. The second function is designed<br />

for implementing dynamic layout, however in class CControlBar, this function does noting but calling<br />

CControlBar::CalcFixedLayout(…). So actually CControlBar does not have dynamic layout feature.<br />

Class CToolBar overrides function CControlBar::CalcDynamicLayout(…), which adjusts the size of<br />

control bar according to its current docking state and the size of buttons. Whenever its docking state is<br />

changed, this function will be called to obtain the information of a tool bar before its layout is changed<br />

accordingly. With this implementation, a tool bar can always have a balanced appearance.<br />

Unlike CToolBar, CDialogBar does not override this function, so when the docking state of a dialog<br />

bar is changed, the default CControlBar::CalcDynamicLayout(…) is called, which of course, will not<br />

perform any dynamic layout for the dialog bar. If we want to add any dynamic layout feature, we must<br />

override this member function.<br />

Sample 1.9\Bar demonstrates how to build a dialog bar that can be resized dynamically. The<br />

application is a standard SDI application generated from Application Wizard, with four classes named<br />

CBarApp, CMainFrame, CBarDoc, and CBarView. In the sample, a dialog bar with an edit control is added to<br />

the application. This dialog bar will support dynamic layout feature: when we float or dock it to different<br />

borders of the mainframe window, the size of the dialog bar will change accordingly.<br />

Deriving New Class from CDialogBar<br />

To implement dialog bar, first we need to add a dialog-template resource. In the sample, the ID of the<br />

new resource is IDD_DIALOGBAR, which contains an edit box. The ID of this edit box is IDC_EDIT, it<br />

supports multiple-line editing. To enable this style, first we can open “Edit Properties” property sheet of the<br />

edit control (To invoke this property sheet, when editing the dialog template, we can double click on the<br />

edit control or right click on it and move to “Properties” menu item), then we need to click “Styles” tab and<br />

check “Multiline” check box.<br />

To override the default function, first we need to derive a new class from CDialogBar. The following<br />

code fragment shows the derived class MCDialogBar, which resides in file “MDlgBar.h”:<br />

class MCDialogBar : public CDialogBar<br />

{<br />

public:<br />

MCDialogBar();<br />

//{{AFX_DATA(MCDialogBar)<br />

//}}AFX_DATA<br />

virtual CSize CalcDynamicLayout(int, DWORD);<br />

//{{AFX_VIRTUAL(MCDialogBar)<br />

//}}AFX_VIRTUAL<br />

protected:<br />

//{{AFX_MSG(MCDialogBar)<br />

afx_msg void OnSize(UINT nType, int cx, int cy);<br />

//}}AFX_MSG<br />

DECLARE_MESSAGE_MAP()<br />

};<br />

Besides constructor, the only things included in this class are two functions. As we already know,<br />

MCDialogBar::CalcDynamicLayout(…) will be used to support dynamic layout. Another afx_msg type<br />

function MCDialogBar::OnSize(…) is a message handler, which will be used to resize the edit control<br />

contained in the dialog bar. By doing this, we can see that whenever the size of the dialog bar is adjusted,<br />

the size of the edit box will also change accordingly. This will let the edit box fit well within the dialog bar.<br />

The new class can be added by opening new files (“.h” and “.cpp” files) and typing in the new class<br />

and function implementations. Then we can execute Project | Add To Project | Files… command to add<br />

the newly created files to the project. However, if we do so, we can not use Class Wizard to add member<br />

variables and functions to the class. In this case, we need to implement message mapping manually. If this<br />

is our choice, we must make sure that DECLARE_MESSAGE_MAP() macro is included in the class, and<br />

BEGIN_MESSAGE_MAP, END_MESSAGE_MAP macros are included in the implementation file (“.cpp” file) so that<br />

the class will support message mapping.<br />

25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!