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

CBarDoc::CBarDoc()<br />

{<br />

m_bBtnRed=FALSE;<br />

m_bBtnGreen=FALSE;<br />

m_bBtnBlue=FALSE;<br />

m_bBtnYellow=FALSE;<br />

}<br />

Two types of message handlers (altogether eight member functions) are rewritten. The following<br />

shows the implementation of two member functions for button ID_BUTTON_RED:<br />

void CBarDoc::OnButtonRed()<br />

{<br />

m_bBtnRed=!m_bBtnRed;<br />

}<br />

void CBarDoc::OnUpdateButtonRed(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->SetRadio(m_bBtnRed);<br />

}<br />

If we execute the application at this point, we will see that the four color buttons behave like check<br />

boxes.<br />

Function CButton::SetButtonInfo(…)<br />

Although this is a simple way to implement “check box” buttons, sometimes it is not efficient. Suppose<br />

we have ten buttons that we expect to behave like check boxes, for every button we need to add a Boolean<br />

type variable and implement a UPDATE_COMMAND_UI message handler. Although this is nothing difficult, it is<br />

not the most efficient way of doing it.<br />

Class CToolBar has a member function that can be used to set the button styles. The function allows us<br />

to set button as a push button, separator, check box, or the start of a group of check boxes. We can also use<br />

it to associate an image with a button contained in the tool bar. The following is the format of this function:<br />

void CToolBar::SetButtonInfo(int nIndex, UINT nID, UINT nStyle, int iImage);<br />

To use this function, we need to provide the information about the button, the style flags, and the<br />

image information. Parameter nIndex indicates which button we are gong to customize. It is a zero-based<br />

index, and button 0 is the left most button or separator on the tool bar (a separator is also considered a<br />

button). Parameter nID specifies which command ID we want to associate with this button. Parameter<br />

nStyle could be one of the following values, which indicates button’s style:<br />

Flag<br />

TBBS_BUTTON<br />

TBBS_SEPARATOR<br />

TBBS_CHECKBOX<br />

TBBS_GROUP<br />

TBBS_CHECKGROUP<br />

Meaning<br />

push button<br />

separator<br />

check box<br />

start of a group<br />

start of a check box group<br />

The last parameter iImage indicates which image will be used to create the bitmap button. This is also<br />

a zero-based number, which indicates the image index of the tool bar resource. In our case, the tool bar<br />

resource contains four images, which are simply painted red, green, blue and yellow. The images are<br />

indexed according to their sequence, which means the red image is image 0, the green image is image 1,<br />

and so on.<br />

When we create a tool bar resource, it seems that a button’s command ID and the associated image are<br />

fixed from the beginning. Actually both of them can be modified through calling the above function. We<br />

can assign any command ID and image to any button. Also, we can change a button to a separator. In a<br />

12

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

Saved successfully!

Ooh no, something went wrong!