VC Color Selection Combo box

Source: Internet
Author: User

This article executes the effect of the code:

We must be very familiar with Word, it is a lot of things inside, whether it is lively and lovely "Big Eye folder", or a variety of graphic modeling of Windows control I like. So this time, let's do a very common control: "Color combo box." If you are doing the word processing class software Now I think this dongdong will be useful to you, haha! Or less nonsense, let's turn to the point!

First, the basic process

(1) First, open VC + + to create a new space based on the dialog box project. I don't have to do more introductions.

(2) to the interface to pull a ComboBox control bar, and then do not forget to declare the corresponding class object, our future operations can all rely on it.

(3) Create a new class, which inherits from the CComboBox class, and the class name is: Cbscolorcombobox.

Next let's take a look at the basic principles and processes of the painting control, perhaps this is the original intention of this article. Everyone knows that all the visible things in windows are drawn, so what is the internal process of painting? Generally, the process of drawing windows controls is divided into three parts:

First: Set the size of the item currently being drawn in the Wm_measureitem message mapping function;

Second: Draw the Item (Icon/bitmap/string, etc.) according to the size of the item in the WM_DRAWITEM message mapping function;

Third: Constantly drawing the current control content in the WM_PAINT message mapping function. Below we will make a brief introduction to these several processes of the Cbscolorcombobox class.

(1) The size of the item is set in the Wm_measureitem message mapping function. Here we only need to set the height of the item. Here the height we set to twice times the height of the system small icon (with:: GetSystemMetrics (Sm_cxsmicon) obtained).

(2) To draw various information about the item according to the size of the item in the WM_DRAWITEM message mapping function. You can draw a variety of information in the rectangular area of the item, such as: Icon/bitmap/string, and so on. Then someone will question: "What do we use to draw?" Where Are we painting? And how to draw it? ”。 The answer is actually in this lpdrawitemstruct structure. The HDC member is the device context environment (HDC), and acquiring the device handle means that we have the ability to draw any bitmap/icon/text; Then the question is: Where are we going to draw? The answer is also simple: get the rectangular area of the item in the LPDRAWITEMSTRUCT structure (rcitem), then this is your space to display your talents, make full use of it! The final step is how to draw the question, which is how to allocate the space of each element, and how to draw what you want in their respective spaces. The rectangular area/bitmap area of the rectangular region/text occupied by the icon is generally computed separately, and if you have other elements, you should also calculate the rectangular area/bitmap of the element. Next I think everything is very simple, nothing more than the CDC class of several common functions: Draw the icon with DrawIcon; picture bitmap use: BitBlt, draw text with: DrawText and so on. If you don't feel cool enough, you can also set the text color of each item, the background color, and the projection of the icon and the visual effect of the dent.

Ii. issues to be noted:

First: The program in order to eliminate the flashing phenomenon caused by the continuous rendering, specially in the WM_ERASEBKGND message to do a special deal; in the WM_PAINT message, the client area of the combo box is constantly updated as a bitmap, rather than the icon area and the text area are redrawn separately.

Second: Whenever the user changes the current contents of the combo box, you must remember to clear the contents of the previous combo box before you draw the new item.

Third: If you want to choose more colors, then simply select the last item in the combo box (more Colors), which is set specifically for the user's custom color.

How to make your own control easy bar! In fact, the above process applies to the menu Menu/listbox/button/combobox and so on most of the self painting process. If you feel still a little bit unintelligible, then open the project to see it, I believe you will be at a glance! If you find any questions, please write: lixiaofei@geasp.com or mailto:devia@163.com.

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.