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

The Application Wizard does an excellent job in adding a very powerful tool bar. Nevertheless, as a<br />

programmer, we are kept from knowing what makes all these happen. If we need to make changes to the<br />

default tool bar (for example, we want it to be docked to the bottom border instead of top border at the<br />

beginning), which part of the source code should we modify? Obviously, we need to understand the<br />

essentials of tool bar implementation in order to customize it.<br />

Like menu, generally tool bar is implemented in the mainframe window. When creating a mainframe<br />

menu, we need to prepare a menu resource, use class CMenu to declare a variable, then use it to load the<br />

menu resource. Creating a tool bar takes similar steps: we need to prepare a tool bar resource, use class<br />

CToolBar to declare a variable, which can be used to load the tool bar resource. After the tool bar resource<br />

is loaded successfully, we can call a series of member functions of CToolBar to create the tool bar and<br />

customize its styles.<br />

After creating a standard SDI or MDI application using Application Wizard (with “Docking toolbar”<br />

check box checked in step 4, see Figure 1-1), we will find that a CToolBar type variable is declared in class<br />

CMainFrame:<br />

Check here to let<br />

Application Wizard<br />

add a default<br />

dockable tool bar<br />

for us<br />

Figure 1-1: Let Application Wizard add a default dockable tool bar<br />

……<br />

……<br />

class CMainFrame : public CFrameWnd<br />

{<br />

protected: // control bar embedded members<br />

CStatusBar m_wndStatusBar;<br />

CToolBar m_wndToolBar;<br />

}<br />

The newly declared variable is m_wndToolBar. By tracing this variable, we will find out how the tool<br />

bar is implemented.<br />

Tool Bar Implementation<br />

Tool bar creation occurs in function CMainFrame::OnCreate(…), where the mainframe window is<br />

being created. Tool bar is created after function CFrameWnd::OnCreate(…) is called, which creates the<br />

default mainframe window. Creating a tool bar takes following steps:<br />

1) Call CToolBar::Create(…) to create tool bar window.<br />

1) Call CToolBar::LoadToolBar(…) to load the tool bar resource. We need to pass a tool bar resource ID<br />

to this function.<br />

2

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

Saved successfully!

Ooh no, something went wrong!