咨询电话:010-82823766

具有不同字体的列表框
  • 2007-12-26 13:08:25
  • 发表时间:
  • 浏览次数:
  • 本站原创
  • 文章来源:
  • 佚名
  • 作者:

具有不同字体的列表框

一般情况下,列表框内所有项的字体、大小、颜色等属性都是一样的。但是我们可以通过编写简单的程序使得每一项都能有自己的字体、大小和颜色。为了对比,我们用了两个Listbox构件,Listbox1按一般情况显示,Listbox2显示的每一项都可以有自己的字体、大小、颜色。

    首先把Listbox2的style属性改为lbOwnerDrawVariable。然后分别编写它的OnDrawItem事件和OnDrawItem事件。下面就是Listbox2 的OnDrawItem事件和OnDrawItem事件的代码: 

procedure TForm1.ListBox2DrawItem(Control:
 TWinControl; Index: Integer;
  RectTRect; State: TOwnerDrawState);
begin
  with ListBox2.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,
 ListBox2.Items[Index]);
  end;
end;

procedure TForm1.ListBox2MeasureItem
(Control: TWinControl; Index: Integer;
  var Height: Integer);
begin
  with ListBox1.Canvas do
  begin
    Font.Size := 12;
    if Index mod 2 =0 Then
    begin
      Font.Name := 黑体;
      Font.Color := Clred;
    end
    else
    begin
      Font.Name := 隶书;
      Font.Color := Clgreen;
    end;
    Height := TextHeight(Wg) + 2;
  end;
end;

top
推荐导读
推荐导读
bottom
top
热门文章
热门文章
bottom