Assume that the reader already has a toolbar and has a m_wndtoolbar member in its parent class to mark the toolbar. To add a ComboBox, follow these steps.
1. Add an item in the toolbar resource and give an ID. We assume it is id_tools_combo.
2. Declare a ComboBox member and a cfont member in the parent Class header file:
Ccombobox m_tbcb; // indicates the ComboBox cfont m_combofont; // indicates the ComboBox font.
3. Add the following code to the oncreate function of its parent class:
int idx = 0;while(m_wndToolBar.GetItemID(idx) != ID_TOOLS_COMBO) idx++;m_wndToolBar.SetButtonInfo(idx,ID_TOOLS_COMBO,TBBS_SEPARATOR,80);//80--width of the comboboxm_wndToolBar.GetItemRect(idx,rc);rc.top += 1;//up pointrc.bottom += 150;//define the height of the combo box listif (!m_tbCB.Create(WS_EX_RTLREADING| CBS_DROPDOWNLIST |WS_CHILD |WS_VISIBLE |CBS_AUTOHSCROLL |CBS_HASSTRINGS , rc, &m_wndToolBar, ID_TOOLS_COMBO)){ TRACE0( "Failed to create combobox\n "); return -1; // fail to create } m_ComboFont.CreateFont(14, 0, 0, 0, FW_NORMAL,FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Arial");m_tbCB.SetFont(&m_ComboFont);m_tbCB.ShowWindow(SW_SHOW);
Using the above program, you can build a ComboBox with a specified index location, a specified width, a specified font, and a specified list length on the tool bar.
4. Finally, release the font resources at the exit of the parent class (or in the destructor:
m_ComboFont.DeleteObject();
As a result, a gorgeous ComboBox is displayed on the toolbar.
If you want to, you can also embed the above Code into your subclass ComboBox class, which is convenient to use. The specific method is not difficult. Let's talk about it as follows:
A. Subclass toolbar, that is, to create a class for your Toolbar resources. This class inherits from ctoolbar. In this class, add a ComboBox m_cbbox member;
B. Define a public member function to add a ComboBox to the tool bar,
For example, addcombobox (INT idx, int width, ctoolbar * pparent, int ID); in fact, this function is the packaging of the above Code.
C. during use, you only need to change the type of m_wndtoolbar member variable in the parent class to ctoolbar, and then call its members.
Addcombobox () can implement the same function. For detailed steps, see the link:
Http://www.codeproject.com/Articles/2726/Toolbars-with-embedded-Combo-Boxes