Advanced processing of ComboBox

Source: Internet
Author: User

I admit that the title of the article is favored. The so-called advanced processing is just a way to subclass the edit and ListBox child controls of ComboBox. I want to know more about ComboBox, and there are many application scenarios, so there will be more demands for its special customization. ComboBox Chinese name is called the combo box, msdn has a detailed introduction to its http://msdn.microsoft.com/zh-cn/library/12h9x0ch (V = vs.71 ). aspx, the so-called combination is that this control actually contains two sub-controls, one is a ListBox, And the other may be a static control or edit according to different styles. The original text of msdn is as follows:

A combo box consists of a list box combined with either a static control or edit control. the list-box portion of the control may be displayed at all times or may only drop down when the user selects the drop-down arrow next to the control.

For more details, refer to the link mentioned above.

In specific applications, we often cannot meet the standard functions provided by MFC. In this case, we generally reload and customize controls, and ComboBox is no exception. However, ComboBox is a little complicated. It also includes sub-controls. To completely reload the sub-controls, you must also customize them. Experienced users must know that we need to subclass these child controls and use our own classes to replace them. Microsoft also thought of this for us, in the above-mentioned links, we will introduce how to reload ComboBox through how to subclass clistbox and cedit inside of ccombobox. This article uses onctlcolor to subclass child controls. It should be said that this method is clever but not elegant, in addition, the article also mentions that this method can only take effect after the ComboBox is drawn at least once. It is not applicable to requirements that must be replaced before this. The original Article is as follows:

Note that for subclassing to occur, the dialog box must be painted at least once. there are cases when the dialog box doesn't paint at all (for example, closing the dialog box before it is displayed, hidden dialog boxes ). this method may not be suitable when access to the subclassed windows are needed in these cases.

So is there any better way to implement ComboBox subclass of its child controls? This article is to solve this problem. To implement subclass, we only need to solve one problem, that is, the handle of the control to be subclass in the past. We know that the ComboBox information is encapsulated in the comboboxinfo structure and can be obtained through the ComboBox member function getcomboboxinfo. Let's take a look at the definition of this structure,

/* <Br/> * ComboBox Information <br/> */<br/> typedef struct tagcomboboxinfo <br/>{< br/> DWORD cbsize; <br/> rect rcitem; <br/> rect rcbutton; <br/> DWORD statebutton; <br/> hwnd hwndcombo; <br/> hwnd hwnditem; <br/> hwnd hwndlist; <br/>} comboboxinfo, * pcomboboxinfo, * lpcomboboxinfo;

We found that hwnditem and hwndlist should be what we want. Then we need to select a proper time for subclass. For the control, processing in the presubclasswindow function is no longer appropriate. In this way, we can solve the problem of subclass-based approaches and timing. Let's give it a try. I have reloaded ccombobox for testing, to verify whether the subclass is successful, I did not initialize ComboBox, but completed it through a new subclass control. The core code is as follows:

Void cexcombobox: presubclasswindow () <br/>{< br/> ccombobox: presubclasswindow (); <br/> comboboxinfo comboinfo; <br/> // obtain control information <br/> comboinfo. cbsize = sizeof (comboboxinfo); <br/> getcomboboxinfo (& comboinfo); <br/> // subclass edit box <br/> If (comboinfo. hwnditem! = NULL) <br/>{< br/> m_editreplace.subclasswindow (comboinfo. hwnditem); <br/> m_editreplace.setwindowtext (_ T ("the editing box has been classified as quilt ")); <br/>}< br/> // subclass list box <br/> If (comboinfo. hwndlist! = NULL) <br/>{< br/> m_listboxreplace.subclasswindow (comboinfo. hwndlist); <br/> m_listboxreplace.addstring (_ T (""); <br/>}< br/>}

The program is successfully run. I have compiled a small example. If you are interested, you can download and study it. However, you can also see that the functions and implementations are relatively simple, so the value of this instance is not very great.

I personally think this solution is relatively reasonable. For dynamically created controls, the oncreate function can be used to subtype them. By finding a proper subclass, we can better customize and expand ComboBox to implement richer functions.

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.