Create a graphical ComboBox using Delphi

Source: Internet
Author: User

Since delphi5, the object inspector in the integrated development environment of Delphi uses a graphical style to display certain attributes. For example, cursors, colors, fonts, and image list attributes are of this type. The first time I saw this effect, it was amazing. The names of various fonts can be displayed directly in this font style, which is very convenient when I select the font. How can this effect be achieved? In fact, the "ower-drawing" method of the component is used. It is very convenient to implement this function in Delphi. Now let's start a magical "Graphical component" Journey, let's draw!

To create a self-painted ComboBox component, set its style attribute to cs_ownerdrawfixed or cs_ownerdrawvariable. If all elements in the ComboBox component are of equal height, such as characters or icons, cs_ownerdrawfixed is used. If the elements in the ComboBox component are not of the same height, for example, bitmaps of different sizes, the cs_ownerdrawvariable attribute is used. The ComboBox component receives the wm_measureitem message to trigger the onmeasureitem event. In Windows, the component is no longer drawn. Instead, the component is re-painted by sending wm_drawitem.

The following two examples are used to describe the complete rendering process:

1. ComboBox of the displayed color:

 
(Figure 1)

Step 1: add the color name to the item attribute of ComboBox (this step is in form. the name of all colors will be added to a constant (colors). The Code is as follows:

Const colors: array [0 .. 17] of tcolor =
(Claqua, clblack,..., clwhite, clyellow); // some color names are omitted.

Step 2: draw each element. Its code is not complex. We can associate the color name with each element. Use this color to draw a rectangle and color it in ComboBox. The Code is as follows:

Procedure tform1.colorcombodrawitem (Control: twincontrol; index: integer;
Rect: trect; State: townerdrawstate );
Begin
With control as tcombobox do
Begin // fill the rectangle
Canvas. Brush. Color: = tcolor (colors [Index]);
Canvas. fillrect (rect );
Canvas. textout (rect. Left, rect. Top, colortostring (colors [Index])
End;
End;

2. Wysiwyg font ComboBox:

 
(Figure 2)

Although this may seem complicated, some people may think that only images in one font can be used. In fact, this is not the case. You must remember that there is a tscreen class in Delphi and it will be used this time.

The first step is that the system font is not as small as the color name. If we still use the above method to deal with the font, we may have to spend the whole day, especially those who are interested in the art font, we use the following program to fill in:

For I: = 0 to screen. fonts. Count-1 do
Fontcombo. Items. Add (screen. fonts. Strings [I]);

Step 2: Draw the font:

Procedure tform1.fontcombodrawitem (Control: twincontrol; index: integer;
Rect: trect; State: townerdrawstate );
Begin
With (control as tcombobox). Canvas do
Begin
Font. Name: = screen. fonts. Strings [Index];
Fillrect (rect );
Textout (rect. Left, rect. Top, pchar (screen. fonts. Strings [Index])
End;
End;

The above "self-painting" method can be used not only on ComboBox, but also on other Windows public components, such as listview, Treeview, tabcontrol, statusbar, etc, there is no absolute restricted area in the field of programming. Coupled with Delphi, there will be such a sigh: "You can't do it without it, but you can't think of it "!


This article from: programming entry network http://www.bianceng.cn/Programming/Delphi/jc/200802/7106.htm

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.