哪个组件能够像Listbox添加string那样添加bitmap?
我想做个列表装图像,但listbox好像只能添加string类型的,有哪个组件能加bitmap类型的?或者有什么方法么? 问题点数:20、回复次数:4Top
1 楼unsigned(僵哥(发站内消息,请附上链接或问题说明,否则不予回复))回复于 2006-03-04 22:20:24 得分 0
ListBox也可以,它有一个方法AddObject是可以用来利用的,要不建议看看ImageList或者仿其样做一个.Top
2 楼luyang821109(菜鸟)回复于 2006-03-04 22:36:19 得分 0
我试了,listbox没有addobject这个方法,我想动态的添加bitmap,用不上ImageListTop
3 楼saien(精益求精)回复于 2006-03-04 23:17:12 得分 10
unit Unit1;
interface
uses
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.
Top
4 楼unsigned(僵哥(发站内消息,请附上链接或问题说明,否则不予回复))回复于 2006-03-05 01:07:11 得分 10
//下面的代码没有释放对象,仅作参考.
------------------------
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;Top




