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 function has two parameters, the second parameter dwMode indicates what kind of size is being<br />

retrieved. It can be the combination of many flags, in this section, we need to know only two of them:<br />

Flag<br />

LM_HORZDOCK<br />

LM_VERTDOCK<br />

Meanings<br />

The horizontal dock dimension is being retrieved<br />

The vertical dock dimension is being retrieved<br />

What we need to do in the overridden function is examining the LM_HORZDOCK bit and LM_VERTDOCK bit<br />

of dwMode parameter and setting the button information correspondingly.<br />

To override the member function of CToolBar, we must first derive a new class from it, then<br />

implement a new version of this function in the newly created class. Sample 1.7\Bar demonstrates how to<br />

change the button’s style dynamically, it is based on sample 1.6\Bar.<br />

First, we need to declare the new class, in the sample, this class is named CColorBar:<br />

class CColorBar : public CToolBar<br />

{<br />

public:<br />

CColorBar();<br />

BOOL AddComboBox();<br />

BOOL ShowComboBox();<br />

BOOL HideComboBox();<br />

virtual ~CColorBar();<br />

virtual CSize CalcDynamicLayout(int , DWORD);<br />

//{{AFX_VIRTUAL(CColorBar)<br />

//}}AFX_VIRTUAL<br />

protected:<br />

CComboBox m_wndComboBox;<br />

};<br />

//{{AFX_MSG(CColorBar)<br />

//}}AFX_MSG<br />

DECLARE_MESSAGE_MAP()<br />

Instead of declaring a CComboBox type variable in class CMainFrame, here we implement the declaration<br />

in the derived class. This is a better implementation because the combo box should be made the child<br />

window of the tool bar. By embedding the variable here, we can make it a protected variable so that it is not<br />

accessible from outside the class.<br />

Three functions are added to change the tool bar’s style. Function CColorBar::AddComboBox()<br />

changes the blue button to a separator and creates the combo box window:<br />

BOOL CColorBar::AddComboBox()<br />

{<br />

CRect rect;<br />

}<br />

GetItemRect(2, rect);<br />

rect.bottom=rect.top+150;<br />

if<br />

(<br />

!m_wndComboBox.Create<br />

(<br />

WS_CHILD | CBS_DROPDOWN | CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS,<br />

rect,<br />

this,<br />

ID_BUTTON_BLUE<br />

)<br />

)return FALSE;<br />

else return TRUE;<br />

This is the same with what we did in function CMainFrame::OnCreate(…) in the previous section. The<br />

only difference is that when creating the combo box within the member function of CMainFrame, the combo<br />

21

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

Saved successfully!

Ooh no, something went wrong!