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

Usually we use a Boolean type variable as the flag, which represents “certain situations” in the above<br />

if statement. We can toggle this flag in other functions, this will cause the button state to be changed<br />

automatically. By doing this, the button’s state is synchronized with the variable.<br />

To set check state for a button, all we need to do is calling function CCmdUI::SetCheck(…) instead of<br />

CCmdUI::Enable(…) in the message handler.<br />

Sample<br />

Sample 1.2\Bar demonstrates how to make the four color buttons behave like radio buttons. At any<br />

time, one and only one button will be set to checked state (it will recess and give the user an impression<br />

that it is being held down).<br />

To implement this feature, a member variable m_uCurrentBtn is declared in class CBarDoc. The value<br />

of this variable could be set to any ID of the four buttons in the member functions (other values are not<br />

allowed). In the user-interface update command message handler of each button, we check if the value of<br />

m_nCurrentBtn is the same with the corresponding button’s ID. If so, we need to set check for this button,<br />

otherwise, we remove its check.<br />

The following lists the steps of how to implement these message handlers:<br />

1) Open file “BarDoc.h”, declare a protected member variable m_uCurrentBtn in class CBarDoc:<br />

……<br />

……<br />

class CBarDoc : public CDocument<br />

{<br />

protected:<br />

UINT m_uCurrentBtn;<br />

};<br />

2) Go to file “BarDoc.cpp”, in CBarDoc’s constructor, initialize m_uCurrentBtn red button’s resource ID:<br />

CBarDoc::CBarDoc()<br />

{<br />

m_uCurrentBtn=ID_BUTTON_RED;<br />

}<br />

This step is necessary because we want one of the buttons to be checked at the beginning.<br />

3) Implement UPDATE_COMMAND_UI message mapping for four button IDs. This is almost the same with<br />

adding ON_COMMAND macros, which could be done through using Class Wizard. The only difference<br />

between two implementations is that they select different message types from “Message” window (see<br />

step 5 previous section). Here we should select “UPDATE_COMMAND_UI” instead of<br />

“COMMAND”.<br />

1) Implement the four message handlers as follows:<br />

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

{<br />

pCmdUI->SetCheck(pCmdUI->m_nID == m_uCurrentBtn);<br />

}<br />

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

{<br />

pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);<br />

}<br />

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

{<br />

pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);<br />

}<br />

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

{<br />

pCmdUI->SetRadio(pCmdUI->m_nID == m_uCurrentBtn);<br />

}<br />

10

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

Saved successfully!

Ooh no, something went wrong!