将ListBox的Style设为lbOwnerDrawFixed,然后再OnDrawItem事件中画。

解决方案 »

  1.   

    随便写了一些代码,由于没有考虑State,所以出来的结果比较难看,但是基本符合你的需要。
    代码没有效率,所以还需要优化一下。另,我的ImageList的Width是16。
    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    var
      a: TBitmap;
      b, c: TRect;
    begin
      a := TBitmap.Create;
      try
        ImageList1.GetBitmap(Index, a);
        b := classes.Rect(0, 0, 16, 16);
        c := classes.Rect(0, Rect.Top, 16, Rect.Bottom);
        ListBox1.Canvas.CopyRect(c, a.Canvas, b);
        ListBox1.Canvas.TextOut(18, Rect.Top, ListBox1.Items[Index]);
      finally
        a.Free;
      end;
    end;
      

  2.   

    你能确定GetIco(ListView1.Items[i].Caption)的图标全都不一样吗?
    问题可能出在这里。你先别动态为IMGLIST赋值,选几个不一样的图标。看看行不行。
    如果行GetIco可能有问题。
      

  3.   

    TO wangzh(独孤求问):GETICO没有问题,我测试过的取得的图标都是不一样的。TO chechy(chechy):你的方法我试过了,还是不行,画出来的还是样。我发现LISTBOX每执行一次ListBox.Items.Add('test');就用当前IMAGELIST里的指定图标把所有的ITEM全部重新画了一遍,所以造成这种结果
    高手,快来吧。
      

  4.   

    重化是没有错。但是我的代码,重绘是没有问题的啊。我刚才试过了。你确认ListBox的Style是OwnerDraw吗?当然我没有用GetICO,我的IMagelist中的图是固定的。
      

  5.   

    我当然确定啊
    不然的话图标是画不出来的
    如果你方便的话,把你的测试代码发到我的信箱?
    [email protected]  谢谢
      

  6.   

    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;Rect:TRect;State: TOwnerDrawState);
    var
    Bitmap: TBitmap;      { temporary variable for the item抯 bitmap }
    Offset: Integer;      { text offset width }
    begin
    with (Control as TListBox).Canvas do  { draw on control canvas, not on the form }
    begin
    FillRect(Rect);       { clear the rectangle }
    Offset := 2;          { provide default offset }
    Bitmap := TBitmap((Control as TListBox).Items.Objects[Index]);  { get the bitmap }if Bitmap <> nil then
    begin
    BrushCopy(Bounds(Rect.Left + 2, Rect.Top, Bitmap.Width, Bitmap.Height),
    Bitmap, Bounds(0, 0, Bitmap.Width, Bitmap.Height), clRed);  {render bitmap}
    Offset := Bitmap.width + 6;    { add four pixels between bitmap and text}
    end;
    TextOut(Rect.Left + Offset, Rect.Top, (Control as TListBox).Items[Index])  { display the text }
    end;
    end;
      

  7.   

    to pandazy(小熊) :你的代码和上面还是差不多啊,哎~~等我回去测试吧不过我想还是不能成功的
      

  8.   

    TO chechy(chechy):没有收到,麻烦你再发到[email protected]一次,谢谢
      

  9.   

    没有收到啊
    麻烦你再发到 [email protected]一次!谢谢
      

  10.   

    首先设置为ownerdraw,然后在向imagelist中加入图标,最后在DrawItem中画imagelist中的图标到listbox
      

  11.   

    TO CHECHY,谢谢,我已经搞定了,开始的错误原因在于我使用同一个IMAGELIST画了两个LISTBOX的ITEM,所以出现错误。。分数给你加上:)