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

Figure 1-7. Fixing the layout this way will let a tool bar with<br />

fixed size take less area when it is docked to any border<br />

We can call function CToolBar::SetButtonStyle(…) to implement the wrapping. This function has<br />

been discussed in section 1.3. However, there we didn’t discuss the flag that can be used to wrap the tool<br />

bar from a specific button. This style is TBBS_WRAPPED, which is not documented in <strong>MFC</strong>.<br />

Sample 1.5\Bar is based on sample 1.4\Bar that demonstrates this feature. The following shows the<br />

changes made to the original CMainFrame::OnCreate(…) function:<br />

1) Replace CBRS_SIZE_DYNAMIC with CBRS_SIZE_FIXED when setting the tool bar style. The following<br />

statement shows this change:<br />

m_wndColorButton.SetBarStyle<br />

(<br />

m_wndColorButton.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED<br />

);<br />

1) Add the following statement after this:<br />

m_wndColorButton.SetButtonStyle<br />

(<br />

1, m_wndColorButton.GetButtonStyle(1) | TBBS_WRAPPED<br />

);<br />

To avoid losing default styles, in the second step, function CToolBar::GetButtonStyle(…) is first<br />

called to retrieve the original styles, which are bit-wise ORed with the new style before calling function<br />

CToolBar::SetButtonStyle(…).<br />

1.6. Adding Combo Box to Tool Bar<br />

By default, a tool bar can have only buttons and separators, and all the buttons must have the same<br />

size. This prevents us from adding other types of controls to the tool bar. However, by using some special<br />

properties of tool bar, we can still manage to add other types of common controls such as combo box to it.<br />

Remember that all controls are actually different type of windows in essence. When we design a dialog<br />

template and add different common controls, we are given an impression that these controls are<br />

implemented “Statically”. In fact, we can create any type of common controls by calling function<br />

CWnd::Create(…) at any time. This member function is supported by all the classes that are derived from<br />

CWnd. We can use it to create a control and put it anywhere on the tool bar.<br />

Dynamically creating window is rarely used in normal programming because in this case the<br />

programmer has to calculate the size and position of the window carefully. If we implement this from a<br />

dialog template, we can see the visual effect immediately after a new control is added. If we implement this<br />

through function calling, we have to compile the project before we can see the final result.<br />

17

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

Saved successfully!

Ooh no, something went wrong!