Ajouter une image sur une ListBox

type
TForm1 = class(TForm)
ListBox1: TListBox;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer);
private
{...}
public
{...}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
with ListBox1.Items do
begin
clear;
ListBox1.style:=lbOwnerDrawVariable;
AddObject('Bitmap1',Image1.Picture.Bitmap);
AddObject('Bitmap2',Image2.Picture.Bitmap);
AddObject('Bitmap3',Image3.Picture.Bitmap);
end;
end;

Procedure CenterText(Cnv: TCanvas; Rect: TRect; S: String);
Var X, Y: Integer;
begin
X := (Rect.Right + Rect.Left - Cnv.TextWidth(S)) Div 2;
Y := (Rect.Bottom + Rect.Top - Cnv.TextHeight(S)) Div 2;
Cnv.TextOut(X,Y,S);
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var Bitmap : TBitmap;
begin
with ListBox1 do
begin
Canvas.FillRect(Rect);
if Items.Objects[Index] <> nil then
begin
Bitmap := Items.Objects[Index] as TBitmap;
Canvas.BrushCopy(Bounds(Rect.Left+ 2, Rect.Top + 2,
Bitmap.Width,Bitmap.Height), Bitmap,Bounds(0,0,Bitmap.Width,
Bitmap.Height),Bitmap.Canvas.Pixels[0,Bitmap.Height-1]);
end;
Rect.Left := Rect.Left + Bitmap.Width + 4;
Rect.Bottom := Rect.Top + Bitmap.Height + 4;
CenterText(Canvas, Rect, Items.Strings[Index]);
end;
end;

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer);
begin
If Index = 0 Then Height := Image1.Height + 4;
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour30/11/2001
VisualisationVu 1233 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Objet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur