Custom wtl ccombobox Control

Source: Internet
Author: User
Tags droplist

This self-painting ccombobox control took a long time. I didn't find a suitable example on the Internet, but I finally got it. The following is a summary:

1. It still inherits the ccombobox and cownerdraw classes. the usage of other ownerdraw is basically the same (1) Add reflect_configurations () to msg_map of the parent window (2) Add chain_msg_map_alt (cownerdraw, 1) to msg_map of the new class)
Default_reflection_handler () and reload the drawitem function the re-painting code you wrote in The drawitem function. After doing this, run the program, you find that ccombobox is not changed (assuming that the dropdown-style ccombobox is to be created and other attributes have been set for you, And the ccombobox is subclass ready ). the drawitem message is triggered only when you click the drop-down button. here, we need to add that in the program design, there are quite a few combo boxes, but most programmers only know about the structure of the combo box, that is, the general combo box is composed of the edit box + ListBox box. Is the actual situation true? If the above view is correct, you can use the edit pointer and ListBox pointer to directly operate the ccombobox control. How can you get the edit pointer and ListBox pointer of the combo box. of course the situation is not that simple. ccombobox has three styles: simple dropdown and droplist. We usually use the last two forms. The difference is that the dropdown editing area is an editable control, droplist is a static control. that is to say, the dropdown style contains an edit, while the droplist style contains static. By the way, The ListBox In the combo box is in the drop-down mode, that is, when the ListBox is popped up (that is, when the dropdown mode is used) and destroyed when ListBox is combined. In the above view, the control to be self-painted must be divided into two major blocks, and The ListBox is drawn in the drawitem function, in the parent window, you need to draw edit in the ctlcoloredit message processing function (similarly, static needs to be drawn in the corresponding message processing function ). by the way, you have set the attribute of ccombobox to ownerdraw fixed. When you use the member function addstring () of ccombobox, the items added by insertstring will not be automatically displayed, you must handle the positions and other attributes of these items in the drawitem function by yourself. If you find garbled characters, it may be the has string attribute of ComboBox, which you have not set. the ctlcoloredit message processing function must be divided into two parts: ccombobox rendering and its sub-control painting. The code I wrote is pasted below, for reference only, lresult clogindlg: onctlcoloredit (uint, wparam, lparam, bool &)
{
HDC hdcedit = (HDC) wparam;
Hwnd hwndedit = (hwnd) lparam;
If (hwndedit = m_combox.getwindow (gw_child). m_hwnd)
{
// Draw edit. Here I just changed the background color.
Hbrush m_hbrush = NULL;
M_hbrush =: createsolidbrush (RGB (216,216,216 ));
Setbkmode (hdcedit, transparent );
// Setbkcolor (hdcedit, m_username.bkcol); // This statement is useless if it is set to transparent.
Settextcolor (hdcedit, RGB (245 ));
Return (lresult) m_hbrush );
}
Else if (hwndedit = m_combobox.m_hwnd)
{
// Draw a ComboBox
Hbrush m_hbrush = NULL;
M_hbrush =: createsolidbrush (RGB (216,216,216 ));
Cdchandle Dc = hdcedit;
CDC dcmem;
Dcmem. createcompatibledc (DC );
DC. savedc ();
Dcmem. savedc ();
Dcmem. selectbitmap (m_dropdown );
Crect rect;
: Getclientrect (hwndedit, & rect );
DC. fillsolidrect (& rect, RGB (216,216,216 ));
// Draw the drop-down button
DC. bitblt (rect. Right-19, 2, 16, 17, dcmem, 0, 0, srccopy );
Return (lresult) m_hbrush );
}
Return 0;
} If you do not need to process edit, you can try it. You will find that the edited area is still white after you have drawn it. the following is a program: this image is obtained from the Internet. If it is inappropriate, please send me an email to notify me. After verification, I will immediately delete the drawing of the cedit control, like editing in ComboBox, Check controls are replaced by buttons.

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.