@Petevick
- Create a new program
- Add BGRABitmapPack to the required packages
- Drop a TComboBox on the form.
The use clause of the interface and the declaration of the class
TForm1 should look like this:
uses
Classes , SysUtils , Forms , Controls , Graphics , Dialogs , StdCtrls, Types,
BGRABitmapTypes, BGRABitmap;
type
{ TForm1 }
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
private
Procedure LoadDaysOfTheWeek;
public
end;
Basically, there are two steps to manage here:
- Loading the items in the combo box: that's handled by LoadDaysOfTheWeek which is called from FormCreate
- Displaying the items in the combo box which is managed by ComboBox1DrawItem
To load the items, you would call a method from the
FormCreate event handler (or the constructor of a frame). The method might look something like this:
procedure TForm1.LoadDaysOfTheWeek;
begin
With ComboBox1 do
begin
Style := csOwnerDrawEditableVariable;
With Items do
begin
Clear;
Add('Sunday');
Add('Monday');
Add('Tuesday');
Add('Wednesday');
Add('Thursday');
Add('Friday');
Add('Saturday');
end;
end;
end;
Note that we set the style of the combobox to
csOwnerDrawEditableVariable so that the elements are properly drawn.
Next, in the DrawItem even handler, we draw these items. This might look like this:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
var aColor : TBGRAPixel;
B : TBGRABitmap;
S : String ;
//If the item is selected or focused, draw it in bold white on a purple background,
//otherwise draw it with its own color as indicated below.
begin
S := ComboBox1.Items[Index];
B := TBGRABitmap.Create(aRect.Width, aRect.Height, BGRAWhite);
B.FontHeight := 12;
If [odSelected, odFocused] * State <> []
then begin
B.FillRect(1,1,aRect.Width,aRect.Height,VGAPurple);
B.FontStyle := [fsBold];
B.TextOut(1, 1 ,S,BGRAWhite);
end
else begin
case Index of
0 : aColor := VGABlue ;
1 : aColor := VGARed ;
2 : aColor := VGAGreen ;
3 : aColor := VGAFuchsia ;
4 : aColor := VGAMaroon ;
5 : aColor := VGANavy ;
6 : aColor := VGAOlive ;
end;
B.TextOut(1,1,S,aColor);
end;
B.Draw(ComboBox1.Canvas,aRect);
B.Free;
end;
Note that in this case, you do not invoke the FormPaint event since the painting will take place after the DrawItem event handler has been called.
Cheers,