ListBox的字体颜色问题(100分)

  • 主题发起人 主题发起人 jarm
  • 开始时间 开始时间
J

jarm

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样设置ListBox中某一行的颜色为某种颜色(即不为默认的),我是想在程序中设置的
 
TListBox.OnDrawItem
 
有没有相关的例子呀。。。。。。
 
sigh,
下面是Delphi5.0的帮助中的例子,我一点也没改动
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;

要使其生效,需先设置listBox的style为lbOwnerDrawFixed
 
如果你只是想改变颜色,
可根据State来设置不同颜色的画刷
 
procedure TForm1.ListBox1DrawItem(Control:
TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with ListBox1.Canvas do
begin
FillRect(Rect);
Font.Size := 12;
if Index mod 2 =0 Then
begin
Font.Name := '宋体';
Font.Color := Clred;
end
else
begin
Font.Name := '隶书';
Font.Color := Clgreen;
end;
TextOut(Rect.Left+1, Rect.Top+1,
ListBox1.Items[Index]);
end;
end;

 
最后一个问题,答过后就给分了,怎样调用ListBox的OnDrawItems事件,参数怎样设置呀
 
把ListBox1 的Style属性改一下,不是lbStandard就行。
 
以前DFW上的回答,建议你去下载一份CHM格式的大富翁理想浏览看看

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if index=2 then
begin
ListBox1.Canvas.Brush.Color := clRed;
ListBox1.canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[index]);
end
else
begin
ListBox1.Canvas.Brush.Color := clGreen;
ListBox1.canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[index]);
end;

end;

listbox1.style是lbOwnerDrawFix,

 
多人接受答案了。
 
后退
顶部