except Raise; end; end;procedure TForm1.FormDestroy(Sender: TObject); begin bmp.Free; end;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var bmpRect,TxtRect:TRect ; begin //画图像 if Index < 0 then Exit; SetRect(BmpRect,Rect.Left,Rect.Top,(Rect.Right div 2),Rect.Bottom); SetRect(TxtRect,bmpRect.Right+1,Rect.Top,Rect.Right,Rect.Bottom); with TListBox(Control).Canvas do begin StretchDraw(bmpRect,TBitmap(TListBox(Control).Items.Objects[index])); TextOut(TxtRect.Left,((TxtRect.Bottom) div 2)-(TextHeight('####') div 2),TListBox(Control).Items.Strings[index]) end; //画文字 end;procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.AddItem('TestBmp',bmp); end;end.
//下面的代码没有释放对象,仅作参考. ------------------------procedure TForm1.Button1Click(Sender: TObject); var bmp:TBitmap; begin bmp:=TBitmap.Create; //try if OpenPictureDialog1.Execute then begin bmp.LoadFromFile(OpenPictureDialog1.FileName); ListBox1.Items.AddObject(ExtractFileName(OpenPictureDialog1.FileName),TObject(bmp)); end; //finally // bmp.Free; //end; end;procedure TForm1.ListBox1Click(Sender: TObject); begin if ListBox1.ItemIndex<0 then Exit; Image1.Picture.Assign(TBitmap(ListBox1.Items.Objects[ListBox1.ItemIndex]));end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
bmp:TBitmap ;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawVariable;
ListBox1.ItemHeight := 50;
bmp := TBitmap.Create;
try
bmp.LoadFromFile('c:\windows\Rhododendron.bmp');
except
Raise;
end;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
bmp.Free;
end;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
bmpRect,TxtRect:TRect ;
begin
//画图像
if Index < 0 then Exit;
SetRect(BmpRect,Rect.Left,Rect.Top,(Rect.Right div 2),Rect.Bottom);
SetRect(TxtRect,bmpRect.Right+1,Rect.Top,Rect.Right,Rect.Bottom); with TListBox(Control).Canvas do
begin
StretchDraw(bmpRect,TBitmap(TListBox(Control).Items.Objects[index]));
TextOut(TxtRect.Left,((TxtRect.Bottom) div 2)-(TextHeight('####') div 2),TListBox(Control).Items.Strings[index])
end;
//画文字
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.AddItem('TestBmp',bmp);
end;end.
------------------------procedure TForm1.Button1Click(Sender: TObject);
var
bmp:TBitmap;
begin
bmp:=TBitmap.Create;
//try
if OpenPictureDialog1.Execute then
begin
bmp.LoadFromFile(OpenPictureDialog1.FileName);
ListBox1.Items.AddObject(ExtractFileName(OpenPictureDialog1.FileName),TObject(bmp));
end;
//finally
// bmp.Free;
//end;
end;procedure TForm1.ListBox1Click(Sender: TObject);
begin
if ListBox1.ItemIndex<0 then Exit;
Image1.Picture.Assign(TBitmap(ListBox1.Items.Objects[ListBox1.ItemIndex]));end;