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 />

Sample 1.11\Bar supports this new feature, it is based on sample 1.10\Bar. For both tool bar and dialog<br />

bar, a new command is added to the main menu, which can be used to toggle the control bar between on<br />

and off state.<br />

The following shows necessary steps for implementing the new commands:<br />

1) Add two menu items View | Color Bar and View | Dialog Bar to the mainframe menu IDR_MAINFRAME,<br />

whose IDs are ID_VIEW_COLORBAR and ID_VIEW_DIALOGBAR respectively.<br />

1) Use Class Wizard to add WM_COMMAND and UPDATE_COMMAND_UI type message handlers for the above<br />

IDs in class CMainFrame. The newly added functions are CMainFrame::OnViewColorBar(),<br />

CMainFrame:: OnViewDialogBar(), CMainFrame::OnUpdateViewColorBar(…) and CMainFrame::<br />

OnUpdateViewDialogBar(…).<br />

1) Implement four WM_COMMAND type message handlers. The function used to handle WM_COMMAND message<br />

for command ID_VIEW_COLORBAR is implemented as follows:<br />

void CMainFrame::OnViewColorBar()<br />

{<br />

BOOL bShow;<br />

}<br />

bShow=m_wndColorButton.IsWindowVisible() ? FALSE:TRUE;<br />

ShowControlBar(&m_wndColorButton, bShow, FALSE);<br />

To indicate the status of control bars, it is desirable to check the corresponding menu item when the<br />

control bar is available, and remove the check when it becomes hidden. This is exactly the same with the<br />

behavior of the default tool bar IDR_MAINFRAME. In the sample, the menu item states are handled by<br />

trapping message UPDATE_COMMAND_UI and the check is set or removed by calling function<br />

CCmdUI::SetCheck(…). The following is the implementation of one of the above message handlers (see<br />

Chapter 2 for more about menu customization):<br />

void CMainFrame::OnUpdateViewColorBar(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->SetCheck(m_wndColorButton.IsWindowVisible());<br />

}<br />

It is exactly the same with setting or removing check for a tool bar button.<br />

With the above implementations, the application can be executed again. We can dismiss the control bar<br />

either by executing menu command or by clicking “X” button located at the upper-right corner of the<br />

control bar when it is floating. In both cases, the control bar can be turned on again by executing<br />

corresponding menu command. We can also dock or float the control bar and turn it off, and see if the<br />

original state will remain unchanged after it is turned on later.<br />

Summary:<br />

1. To add an extra tool bar, first we need to add a tool bar resource, then declare a CToolBar type variable<br />

in CMainFrame class. Within function CMainFrame::OnCreate(…), we can call the member functions of<br />

CToolBar and CMainFrame to create the tool bar window and set docking styles.<br />

1. The dialog bar can be added in the same way, however, we need to use dialog-template resource and<br />

class CDialogBar to implement it.<br />

1. We can trap WM_COMMAND message for executing command and trap UPDATE_COMMAND_UI for updating<br />

button state. Use ON_COMMAND and ON_UPDATE_COMMAND_UI macros to implement message mapping.<br />

1. We can use ON_COMMAND_RANGE and ON_UPDATE_COMMAND_UI_RANGE macros to map a contiguous range<br />

of command IDs to one member function.<br />

1. When the size of a tool bar is fixed, we can set TBBS_WRAPPED flag for a button to let the tool bar wrap<br />

after that button.<br />

1. To customize the dynamic layout feature of tool bar and dialog bar, we need to override function<br />

CalcDynamicLayout(…).<br />

31

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

Saved successfully!

Ooh no, something went wrong!