listbox的ondrawitem事件怎么用的?(60分)

  • 主题发起人 主题发起人 luckystar
  • 开始时间 开始时间
L

luckystar

Unregistered / Unconfirmed
GUEST, unregistred user!
Tlistbox 的ondrawitem是怎么用的?我把delphi的例子运行了一下,没有一点
反应,单步跟踪看不出该过程被执行.我是想在listbox 里放东西
 
Here is a typical handler for an OnDrawItem event. In the example,
a list box with the lbOwnerDrawFixed style draws a bitmap to the
left of each string.

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;
 
1.设置ListBox的Style等于lbOwnerDrawFixed或lbOwnerDrawVariable
2.OnDrawItem会在需要显示(刷新)Item的时候触发
3.例子:
r:=rect;
r.Left:=r.left+1;
r.top:=r.top+1;
r.bottom:=r.bottom-1;
r.right:=r.right-1;
listbox1.Canvas.TextOut(r.Left + 2, r.Top, Listbox1.Items[Index]);
listbox1.Canvas.Brush.Color:=0;
listbox1.Canvas.FrameRect(R);
 
listbox 的 style 还没设成 lbOwnerDrawFixed 活 lbOwnerDrawVariable
 
多人接受答案了。
 
后退
顶部