关于listbox中ondrawitem问题(50分)

  • 主题发起人 主题发起人 zhukairu
  • 开始时间 开始时间
Z

zhukairu

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在LISTBOX中添加图象,
我写了一段代码,可是没发实现我需要的东西,我在窗体中添加了TLISTBOX,还有IMAGELIST
在IMAGELIST 中添加了两张图片
程序如下:procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
bitmap1,bitmap2:tbitmap;
begin
bitmap1:=tbitmap.create;
bitmap2:=tbitmap.create;
imagelist1.getbitmap(0,bitmap1);
imagelist2.getbitmap(1,bitmap2);
listbox1.canvas.fillrect(rect);
if odselected in state then
ListBox1.Canvas.Draw(Rect.Left, Rect.Top,bitmap1)
else
ListBox1.Canvas.Draw(Rect.Left, Rect.Top, bitmap2);
ListBox1.Canvas.TextOut(Rect.Left + bitmap1.Width div 2, Rect.Top + 2, ListBox1.Items[Index]);
finally
bitmap1.Free;
bitmap2.Free;
end;
end;
我用上面的方法没发在LISTBOX中实现图象。不知道错误出在那里,请高手多多指教,如果要改应该在那里
进行修改。在线等待[:)]

 
你往里加记录的时候,根本就没触发这个事件,不信你跟踪看看,我刚试的
 
我写了个测试程序测了一下
procedure TForm1.Button1Click(Sender: TObject);
var
bitmap:TBitmap;
ARect:TRect;
begin
bitmap := TBitmap.Create;
try
ImageList1.GetBitmap(0,bitmap);
ListBox1.Canvas.Brush.Bitmap := bitmap;
ARect := Rect(ListBox1.Left,ListBox1.top,ListBox1.Left+Bitmap.Width,ListBox1.Top+bitmap.Height);
ListBox1.Canvas.FillRect(ARect);
ListBox1.Canvas.TextOut(ListBox1.left+bitmap.width+2,ListBox1.top,'test');
finally
bitmap.free;
end;
end;

没有问题,画上去了
 
我还没试过,我先试试看了[:)][:)]
 
我已经测试过,这样不行,我没法让LISTBOX显示图片[:(]
 
不会吧,我这里可以呀?是不是你的图片有问题?
 
我想不应该是我的图片问题,想想也不太可能是这个事情了。我想应该是程序出错在哪里了[:)]
 
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;
用delphi的帮助为什么没法实现在listbox中添加图片呢。希望高手解释一下原因
 
listbox的Style属性设为lbOwnerDrawFixed
 
多人接受答案了。
 
后退
顶部