我想做个列表装图像,但listbox好像只能添加string类型的,有哪个组件能加bitmap类型的?或者有什么方法么?

解决方案 »

  1.   

    ListBox也可以,它有一个方法AddObject是可以用来利用的,要不建议看看ImageList或者仿其样做一个.
      

  2.   

    我试了,listbox没有addobject这个方法,我想动态的添加bitmap,用不上ImageList
      

  3.   

    unit Unit1;interfaceuses
      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.
      

  4.   

    //下面的代码没有释放对象,仅作参考.
    ------------------------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;