1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Déclarations privées }
List: TList;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TCustomItem = class
Text: string;
constructor Create(const AText: string);
procedure Draw(Canvas: TCanvas; const Rect: TRect; State: TOwnerDrawState); virtual;
end;
TGras = class(TCustomItem)
procedure Draw(Canvas: TCanvas; const Rect: TRect; State: TOwnerDrawState); override;
end;
TItalic = class(TCustomItem)
procedure Draw(Canvas: TCanvas; const Rect: TRect; State: TOwnerDrawState); override;
end;
constructor TCustomItem.Create(const AText: string);
begin
Text := AText;
end;
procedure TCustomItem.Draw(Canvas: TCanvas; const Rect: TRect; State: TOwnerDrawState);
begin
with Canvas do
begin
FillRect(Rect);
Brush.Style := bsClear;
end;
end;
procedure TGras.Draw(Canvas: TCanvas; const Rect: TRect; STate: TOwnerDrawState);
begin
Canvas.Font.Style := [fsBold];
Canvas.TextOut(Rect.Left, Rect.Top, Text);
Canvas.Font.Color := clRed;
Canvas.TextOut(Rect.Left + 50, Rect.Top, 'Gras');
end;
procedure TItalic.Draw(Canvas: TCanvas; const Rect: TRect; STate: TOwnerDrawState);
begin
Canvas.Font.Style := [fsItalic];
Canvas.TextOut(Rect.Left, Rect.Top, Text);
Canvas.Font.Color := clBlue;
Canvas.TextOut(Rect.Left + 50, Rect.Top, 'Italic');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
List := TList.Create;
List.Add(TGras.Create('hello'));
List.Add(TItalic.Create('there'));
ListBox1.Count := List.Count;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
TCustomItem(List[Index]).Draw(ListBox1.Canvas, Rect, State);
end;
end. |
Partager