11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Before modifying source code to add the second tool bar, we need to prepare the tool bar resource. In<br />

order to do this, we need the following steps to create a tool bar resource that contains four bitmap buttons:<br />

1) Load the application project into Developer Studio.<br />

1) Execute Insert | Resource… command from the menu (or press CTRL+R keys). We will be prompted<br />

to select resource type from a dialog box. If we highlight “toolbar” node and click the button labeled<br />

“New”, a new blank tool bar resource “IDR_TOOLBAR1” will be added to the project. Since default ID<br />

doesn’t provide us much implication, usually we need to modify it so that it can be easily understood.<br />

In the samples, the newly added tool bar resource ID is changed to IDR_COLOR_BUTTON. This can be<br />

implemented by right clicking on “IDR_TOOLBAR1” node in WorkSpace window, and selecting<br />

“Properties” item from the popped up menu. Now a property sheet whose caption is “Toolbar<br />

properties” will pop up, which contains an edit box that allows us to modify the resource ID of the tool<br />

bar.<br />

1) Using the edit tools supplied by the Developer Studio, add four buttons to the tool bar, paint bitmaps<br />

with red, green, blue and yellow colors, change their IDs to ID_BUTTON_RED, ID_BUTTON_GREEN,<br />

ID_BUTTON_BLUE, ID_BUTTON_YELLOW. The tool bar bitmap window could be activated by double<br />

clicking on the tool bar IDs contained in the WorkSpace window, the graphic tools and color can be<br />

picked from “Graphics” and “Colors” windows. If they are not available, we can enable them by<br />

customizing the Developer Studio environment by executing Tools | Customize… command from the<br />

menu.<br />

Declaring New Member Variable<br />

After the resource is ready, we can add code to implement the tool bar.<br />

The first step is to declare a new variable using CToolBar in class CMainFrame:<br />

……<br />

……<br />

class CMainFrame : public CFrameWnd<br />

{<br />

protected:<br />

CStatusBar m_wndStatusBar;<br />

CToolBar m_wndToolBar;<br />

CToolBar m_wndColorButton;<br />

};<br />

The new variable is m_wndColorButton, it is added right after other two variables that are used to<br />

implement the default tool bar and status bar.<br />

Next, we can open file “MainFrm.cpp” and go to function CMainFrame::OnCreateClient(…). In<br />

Developer Studio, the easiest way to locate a member function is to right click on the function name in<br />

“WorkSpace” window, then select “Go to Definition” menu item from the popped up menu. Let’s see how<br />

the default tool bar is created:<br />

……<br />

……<br />

if (!m_wndToolBar.Create(this) ||<br />

!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))<br />

{<br />

TRACE0("Failed to create toolbar\n");<br />

return -1;<br />

}<br />

Function CToolBar::Create(…) is called first to create the tool bar window. Then<br />

CToolBar::LoadToolBar(…) is called to load the bitmaps (contained in tool bar resource IDR_MAINFRAME).<br />

When calling function CToolBar::Create(…), we need to specify the parent window of the tool bar by<br />

providing a CWnd type pointer (Generally, a tool bar must be owned by another window). Because this<br />

function is called within the member function of class CMainFrame, we can use “this” as the pointer of<br />

parent window.<br />

The following code fragment shows how the styles of the default tool bar are set:<br />

4

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

Saved successfully!

Ooh no, something went wrong!