Delphi to make graphical combobox

Source: Internet
Author: User
Tags integer textout

Starting with Delphi5, the Object Inspector in the Delphi Integrated development environment uses a graphical style to display certain attributes. Attributes such as cursors, Colors, fonts, and image list are this type. The first time to see this effect, it is really amazing, the names of various fonts can be directly displayed in this font style, in the choice of fonts is very convenient. How this effect is achieved, in fact, is the use of the component of the "ower-drawing" method. In Delphi to realize such a function is very convenient, now we start a magical "graphical components" tour bar, let's draw!

To create a custom ComboBox component, we first set its Style property to Cs_ownerdrawfixed or cs_ownerdrawvariable, if all elements in the ComboBox component are equal in height, such as characters or icons, Then use cs_ownerdrawfixed if the individual elements in the ComboBox component are not equal, such as bitmaps of different sizes, use the Cs_ownerdrawvariable property. The ComboBox component receives the WM_MEASUREITEM message, triggering the OnMeasureItem event. Windows no longer paints the component, and instead we redraw it by sending Wm_drawitem.

Here we use two examples to illustrate the complete drawing process:

1, display the color of the ComboBox:

(Figure I)

As a first step, we add the name of the color to the ComboBox Item property (This step is done in the Form.oncreate event), and the name of all the colors will be added to a constant (Colors) with the following code:

const Colors:array[0..17] of TColor=
(clAqua, clBlack, ..., clWhite, clYellow) ;//一部分颜色的名称被省略了

The second step is to draw the elements, its code is not complex, we can associate the color name with the elements, with this color in the ComboBox draw a rectangle and coloring, the code is as follows:

procedure TForm1.ColorComboDrawItem (Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
 with Control as TComboBox do
 begin //填充矩形
   Canvas.Brush.Color := TColor(Colors[Index]);
   Canvas.FillRect(Rect);
   Canvas.TextOut(Rect.Left,Rect.Top,ColorToString(Colors[Index]))
 end;
end;

2, what you see is the resulting font combobox:

(Figure II)

Although this may seem complicated, even one might think that a picture of a single font can be used to achieve it. We must remember that there is a Tscreen class in Delphi, this time will use it.

The first step, the system font is not as small as the color name, if you use the above to deal with the color of the font, may have to do a whole day, especially those who art fonts fans, we use the following program to populate:

for i := 0 to Screen.Fonts.Count-1 do
FontCombo.Items.Add(Screen.Fonts.Strings[i]);

The second step is to 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 "Self painting" method above can be used not only on ComboBox, but also on other windows public components, such as ListView, TreeView, TabControl, StatusBar, and so on, as long as you are imaginative, In the field of programming there is no absolute exclusion zone, plus Delphi this handle against the sword, there really will be such a sigh, "No it can not do, only you do not think!"

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.