Author: William Gerbert
Combobox with colors
Answer:
It is quite easy to create a combobox that shows a list of colors. You need to set
the property "Style" to "csOwnerDrawFixed". This causes a call of "OnDrawItem" for
each item in your combobox. The DrawItem routine draws a color bar..
1 // in FormCreate:2 with ComboBox1.Items do3 begin4 Add(IntToStr(clRed));
5 Add(IntToStr(clFuchsia));
6 Add(IntToStr(clBlue));
7 Add(IntToStr(clGreen));
8 Add(IntToStr(clYellow));
9 end;
10 11 procedure TForm1.ComboBox1DrawItem(Control: TWinControl;
12 Index: Integer; Rect: TRect; State: TOwnerDrawState);
13 begin14 with Control as TComboBox, Canvas do15 begin16 // fill the rectangle first with white17 Brush.Color := clWhite;
18 FillRect(Rect);
19 // then reduce it and fill it with the color20 InflateRect(Rect, -2, -2);
21 Brush.Color := StrToInt(Items[Index]);
22 FillRect(Rect);
23 end;
24 end;