In the MFC framework, add ComboBox in the toolbar and set the ComboBox font.

Source: Internet
Author: User
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
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.