Delphi makes graphical ComboBox

zhaozj2021-02-16  114

Starting with Delphi5, the Object Inspector in the Delphi integrated development environment uses a graphical style to display certain properties. For example, Cursors, Colors, Fonts, and Image List are this type. The first time I saw this effect, it is indeed magically, the name of various fonts can be displayed directly in this font, it is very convenient to select the font. This effect is achieved, in fact, is the "Ower-Drawing method" of the components. It is very convenient to implement such a function in Delphi, now we start a magical "graphical component" trip, Let's draw!

To create a self-painted ComboBox component, we must first set its style at CS_OWNERDRAWFIXED or CS_OWNERDRAWVARIABLE, if all elements in the ComboBox component are equal, such as characters or icons, then use cs_ownerdrawfixed; if in ComboBox Each element in the component is not equal, such as bitmaps of different sizes, then use the CS_OWNERDRAWVARIABLE attribute. The ComboBox component will receive a WM_MeasureItem message to trigger an OnMeasureItem event. Windows no longer draws the components, replaced it is to redraw it in a way to send WM_DRAWITEM.

Below we use two instances to explain the complete drawing process:

1, display color's ComboBox:

(Figure 1)

The first step, we add color's name into the item properties of ComboBox (this step is done in the form.oncreate event), all colors will be added to a constant (Colors), the code is as follows:

Const Colors: Array [0..17] of tcolor = (Claqua, CLBLACK, ..., CLWHITE, CLYELOW); // Part of the color name is omitted

In the second step, draw each element, its code is not complicated, we can associate the color name with each element, use this color to draw a rectangle in ComboBox, color, the code is as follows:

procedure TForm1.ColorComboDrawItem (Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin with Control as TComboBox do begin // filled rectangle Canvas.Brush.Color: = TColor (Colors [Index]); Canvas. FillRect (Rect); Canvas.TextOut (Rect.left, Rect.top, ColorToString (Colors [Index])) End; End;

2. The font combobox you have obtained is:

(Figure II)

Although this looks complicated, even some people may think that it is necessary to use a picture of a piece of font to achieve, it is not true. Everyone must still remember that there is a TSCreen class in Delphi. This time you use it.

The first step is that the system font is not like the color name. If you deal with the color of the color, you may have to do a whole day, especially those who are artificial fonts, we use the procedures below to fill :

For i: = 0 to screen.fonts.count-1 DOFONTCOMBO.ITEMS.ADD (Screen.Fonts.Strings [i]); second step, draw 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;

The above "self-painted" method is not only available on ComboBox, you can also be used on other Windows public components, such as ListView, TreeView, TabControl, Statusbar, etc., as long as you play imagination, there is nothing in programming. The penalty area, plus Delphi this sly sword, I really have such sighs, "I don't do it, only you can't think of"!

转载请注明原文地址:https://www.9cbs.com/read-10266.html

New Post(0)