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

box’s parent window needs to be set to m_wndColorButton. Here, since the combo box variable is<br />

embedded in the parent window’s class, we need to use this pointer to indicate the parent window.<br />

Function CColorBar::ShowComboBox() and CColorBar::HideComboBox() change the combo box to<br />

the button and vice versa. They should be called just before the default layout is about to be carried out:<br />

BOOL CColorBar::ShowComboBox()<br />

{<br />

CRect rect;<br />

SetButtonInfo(2, ID_BUTTON_BLUE, TBBS_SEPARATOR, 150);<br />

if(m_wndComboBox.m_hWnd != NULL)<br />

{<br />

m_wndComboBox.ShowWindow(SW_SHOW);<br />

}<br />

}<br />

return TRUE;<br />

BOOL CColorBar::HideComboBox()<br />

{<br />

SetButtonInfo(2, ID_BUTTON_BLUE, TBBS_BUTTON, 2);<br />

if(m_wndComboBox.m_hWnd != NULL)m_wndComboBox.ShowWindow(SW_HIDE);<br />

}<br />

return TRUE;<br />

Finally, function CalcDynamicLayout(…) is overridden as follows:<br />

CSize CColorBar::CalcDynamicLayout(int nLength, DWORD dwMode)<br />

{<br />

if(dwMode & LM_HORZDOCK)ShowComboBox();<br />

else HideComboBox();<br />

}<br />

return CToolBar::CalcDynamicLayout(nLength, dwMode);<br />

Before calling the base class version of this function, we examine LM_HORZDOCK bit of dwMode<br />

parameter, if it is set, we call function CColorBar::ShowComboBox() to change the button to the combo<br />

box. If not, we call function CColorBar::HideComboBox() to change the combo box back to the default<br />

button.<br />

It is relatively easy to use this class: we just need to include the header file of CColorBar class in file<br />

“MainFrm.h”, then change the prototype of m_wndColorBar from CToolBar to CColorBar. Because the<br />

combo box is embedded in CColorBar class, we need to remove variable wndComboBox declared in the<br />

previous section. In function CMainFrame::OnCreate(), instead of creating the combo box by ourselves,<br />

we can just call the member function of CColorBar. Here is how the combo box is created using this new<br />

method:<br />

……<br />

……<br />

m_wndColorButton.AddComboBox();<br />

m_wndColorButton.ShowComboBox();<br />

We can see that the original five statements have been reduced to two statements.<br />

Now we can compile and execute the sample again to see the behavior of the combo box.<br />

1.8. Dialog Bar<br />

As we have noticed, the limitation of the tool bar is that when we design a tool bar from resource, only<br />

bitmap buttons with the same size can be included. If we try to modify the size of one bitmap button, the<br />

size of all other buttons will be automatically adjusted. If we want to include controls other than buttons,<br />

we need to write code to add them dynamically.<br />

22

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

Saved successfully!

Ooh no, something went wrong!