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

One thing to mention here is that CCmdUI has a public member variable m_nID, which stores the ID of<br />

the control that is about to be updated. We can compare it with variable CBarDoc::m_uCurrentBtn and set<br />

the appropriate state of the control.<br />

With the above implementation, the red button will be checked from the beginning. We need to change<br />

the value of variable m_uCurrentBtn in order to check another button. This should happen when the user<br />

clicks on any of the four buttons, which will cause the application to receive a WM_COMMAND message. In the<br />

sample, this will cause the message handlers CBarDoc::OnButtonRed(), CBarDoc::OnButtonBlue()… to be<br />

called. Within these member functions, we can change the value of m_uCurrentBtn to the coresponding<br />

command ID in order to check that button:<br />

void CBarDoc::OnButtonBlue()<br />

{<br />

m_uCurrentBtn=ID_BUTTON_BLUE;<br />

}<br />

void CBarDoc::OnButtonGreen()<br />

{<br />

m_uCurrentBtn=ID_BUTTON_GREEN;<br />

}<br />

void CBarDoc::OnButtonRed()<br />

{<br />

m_uCurrentBtn=ID_BUTTON_RED;<br />

}<br />

void CBarDoc::OnButtonYellow()<br />

{<br />

m_uCurrentBtn=ID_BUTTON_YELLOW;<br />

}<br />

The message box implementation is removed here. By executing the sample application and clicking<br />

on any of the four color buttons, we will see that at any time, one and only one button will be in the<br />

checked state.<br />

1.3. Check Box Implementation<br />

Using Boolean Type Variables<br />

Using the method discussed in section 1.2, it is very easy to implement check-box-like buttons. We can<br />

declare Boolean type variables for each control, and toggle their values between FALSE and TRUE in the<br />

WM_COMMAND message handlers. Within UPDATE_COMMAND_UI message handlers, we can set check for any<br />

button according to the corresponding value of the Boolean type variable.<br />

Sample 1.3-1\Bar is implemented in this way. In this sample, first variable m_uCurrentBtn is removed,<br />

then WM_COMMAND and UPDATE_COMMAND_UI message handlers are made empty. Four new Boolean type<br />

variables are declared in class CBarDoc:<br />

……<br />

……<br />

class CBarDoc : public CDocument<br />

{<br />

protected:<br />

BOOL m_bBtnRed;<br />

BOOL m_bBtnGreen;<br />

BOOL m_bBtnBlue;<br />

BOOL m_bBtnYellow;<br />

}<br />

Their values are initialized in the constructor:<br />

11

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

Saved successfully!

Ooh no, something went wrong!