当前位置: 首页 > 图文教程 > 开发语言 > Delphi > 具有不同字体的列表框

Delphi
回调函数与Delphi的事件模型
Delphi中如何实现透明按钮
用Delphi编写打印程序的窍门
自己制作网页特效软件
Delphi+Cell全攻略
开发基于DCOM的局域网聊天室(一)
用简单的Tracer类来为应用写入跟踪
快捷方式/删除项/EXE自删除DIY
排序Select中Option项的一个示例
QQ聊天记录器演示程序(二)
QQ聊天记录器演示程序(一)
Delphi单元文件详解
Delphi学习:在Listbox加背景图
Delphi7目录结构----初学者参考
Delphi学习:在流中查找任意字串
制作一个IPhunter
播放自定义的声音
利用Dephi5编写控制面板程序
用Delphi编写Win2000服务程序
Spcomm串口控件的例程

Delphi 中的 具有不同字体的列表框


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 91 ::
收藏到网摘: n/a

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

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


procedure TForm1.ListBox2DrawItem(Control:TWinControl; Index: Integer;Rect: TRect; State: TOwnerDrawState);beginwith ListBox2.Canvas dobeginFillRect(Rect);Font.Size := 12;if Index mod 2 =0 ThenbeginFont.Name := '宋体';Font.Color := Clred;endelsebeginFont.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);beginwith ListBox1.Canvas dobeginFont.Size := 12;if Index mod 2 =0 ThenbeginFont.Name := '黑体';Font.Color := Clred;endelsebeginFont.Name := '隶书';Font.Color := Clgreen;end;Height := TextHeight('Wg') + 2;end;end;

 


---- 此程序在Windows95、Delphi4.0环境下运行通过。