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

……<br />

}<br />

m_wndComboBox.ShowWindow(SW_SHOW);<br />

Function CToolBar::GetItemRect(…) is called in the second statement of above code to retrieve the<br />

size and position of the separator. After calling this function, the information is stored in variable rect,<br />

which is declared using class CRect.<br />

A drop down combo box contains two controls: an edit box and a list box. Normally the list box is not<br />

shown. When the user clicks on the drop down button of the combo box, the list box will be shown.<br />

Because the size of the combo box represents its total size when the list box is dropped down (Figure 1-8),<br />

we need to extend the vertical dimension of the separator before using it to set the size of the combo box.<br />

The third statement of above code sets the rectangle’s vertical size to 150. So when our combo box is<br />

dropped down, its width and the height will be roughly the same.<br />

The fourth statement of above code creates the combo box. Here a lot of styles are specified, whose<br />

meanings are listed below:<br />

Style Flag<br />

WS_CHILD<br />

CBS_DROPDOWN<br />

CBS_AUTOHSCROLL<br />

WS_VSCROLL<br />

CBS_HASSTRINGS<br />

Meanings<br />

Indicates that the window (combo box) being created is a child window. We must<br />

specify this flag in order to embed the combo box in another window<br />

The combo box has a list control that can be dropped down by clicking its drop down<br />

button<br />

When the user types text into the edit control, the text will be automatically scrolled<br />

to the left if it is too long to be fully displayed<br />

If too many items are added to the list controls and not all of them can be displayed at<br />

the same time, a vertical scroll bar will be added to the list control<br />

The items in the list control contains strings<br />

The above styles are the most commonly used ones for a combo box. For details about this control,<br />

please refer to chapter 5.<br />

x<br />

y<br />

Figure 1-8. The size of a combo box is the<br />

total size when the list box is dropped down<br />

Because the blue button will not be pressed to execute command anymore, we use ID_BUTTON_BLUE as<br />

the ID of the combo box. Actually, we can specify any other number so long as it is not used by other<br />

controls.<br />

Finally, we must call function CWnd::ShowWindow(…) and pass SW_SHOW flag to it to show any window<br />

created dynamically.<br />

By compiling and executing the sample at this point, we will see that the blue button has been changed<br />

to a combo box.<br />

19

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

Saved successfully!

Ooh no, something went wrong!