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

Delphi
Delphi实现窗体控件自由摆布
利用Delphi编制IP地址转换器
用Indy组件开发Socket应用程序
Delphi模拟最小化恢复关闭按纽
简析XML及其在Delphi中的应用
Delphi开发基于DCOM的聊天室
Delphi实现远程串口的数据采集
Delphi托盘编程实战演练
在Delphi中使用电子邮件
Delphi开发98屏幕保护预览程序
Delphi实现同类型文档自动合并
Delphi 8 For .NET 抢先预览
Delphi图像存取另类解决方案
用Delphi实现动态获取版本信息
用Delphi客户端访问EJB组件
Delphi中数据网格DBGrid应用
Delphi数据库控件使用入门
Delphi下的COM编程技术简介
用Delphi轻松实现背景播放
也谈TTreeView、TListView用法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 82 ::
收藏到网摘: 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环境下运行通过。