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

However, because tool bar resource does not let us add controls other than buttons, dynamic method is<br />

the only way we can pursue to implement combo box on the tool bar. The question here is: because the<br />

buttons are positioned side by side, where can we put a combo box that will definitely take up a larger area?<br />

To prevent the controls from interfering with each other, one control should not overlap another. So<br />

first, we must find an inactive area on the tool bar where we could create the combo box.<br />

On the tool bar, separator is an inactive control. If we click mouse on it, there will be no response. We<br />

already know that we can call function CToolBar::SetButtonInfo(…) to change a button to a separator.<br />

Also, when doing this, we can specify the width of the separator by using iImage parameter (when we pass<br />

TBBS_SEPARATOR to nStyle parameter, the meaning of iImage parameter becomes the width of the<br />

separator).<br />

On top of the separator, we can create any controls using dynamic method.<br />

Sample 1.6\Bar demonstrates how to add combo box to the tool bar. This sample is based upon sample<br />

1.4\Bar. In the new sample, the third button (blue button) is changed to a combo box. The following lists<br />

necessary steps of implementing this:<br />

1) Change the blue button to a separator with a width of 150 after the tool bar is created. For this purpose,<br />

the following statement is added to function CMainFrame::OnCreate(…):<br />

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

Here the first parameter indicates that we want to modify the third button. The second parameter is the<br />

blue button’s resource ID. The fourth parameter specifies the width of the separator. If we compile and<br />

execute the sample at this point, we will see that the blue button does not exist anymore. Instead, a<br />

blank space with width of 150 is added between the third and fourth button. This is the place where we<br />

will create the combo box.<br />

2) Use CComboBox to declare a variable m_wndComboBox in class CMainFrame as follows:<br />

……<br />

……<br />

class CMainFrame : public CFrameWnd<br />

{<br />

}<br />

protected:<br />

CStatusBar m_wndStatusBar;<br />

CToolBar m_wndToolBar;<br />

CToolBar m_wndColorButton;<br />

CComboBox m_wndComboBox;<br />

3) Use the newly declared variable to call function CComboBox::Create(…) in CMainFrame::OnCreate(…)<br />

after the blue button has been changed to separator.<br />

Function CComboBox::Create(…) has four parameters. We must specify combo box’s style, size &<br />

position, parent window, along with the control ID in order to call this function. The following is the format<br />

of this function:<br />

BOOL CComboBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);<br />

We can use the first parameter to set the styles of a combo box. A combo box can have different styles,<br />

in our sample, we just want to create a very basic drop down combo box (For other types of combo boxes,<br />

see Chapter 5). The following code fragment shows how this function is called within CMainFrame::<br />

OnCreate(…):<br />

……<br />

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

m_wndColorButton.GetItemRect(2, rect);<br />

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

if(!m_wndComboBox.Create(WS_CHILD | CBS_DROPDOWN |<br />

CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS,<br />

rect, &m_wndColorButton, ID_BUTTON_BLUE))<br />

{<br />

return -1;<br />

18

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

Saved successfully!

Ooh no, something went wrong!