DELPHI基础教程
第三章 字符串列表及应用(二) 3.1.4 往字符串列表中加入对象 字符串列表除了能在 Strings 属性中贮存字符串外,还可以在 Objects
在应用程序使用列表中的字符串与列表中是否有对象没有多大关系。除非程序特地访问对象,否则 Objects
虽然程序可分配任何类型的对象到列表中,但最常用的是在自画式控制中把位图与字符串联系起来,注意位图与字符串成对使用。
3.1.4.1 操作字符串列表中的对象 对于字符串的每一种操作方法,列表中的对象均有相应的方法。例如,应用程序可利用对象的索引来访问对象。与字符串不同的是,不能省略 Objects
表 3.1 中总结了字符串对字符串和对象操作的方法。 表 3.1 TStrings
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
操 作 字 符 串 对 象 ───────────────────────────────
访 问 Strings 属性
Objects
项目定位
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Delete,Clear,More 操作整个项目,即删除字符串时把相应的对象也删除了。但 LoadFromFile,SaveToFile
3.1.4.2 加入对象 如果把对象与已存在的字符串联系起来, Delphi
另一种方法是调用列表的 AddObject
Fruits AddObject('Apple',AppleBitmap); 3.2 字符串列表应用 Delphi
列表框、组合框、 Tabset
通常,创建自画式控制有以下三个步骤:
2. 把图像对象加入字符串列表中; 3. 绘制自画项目。 3.2.1 设置自画风格 每个能进行自画式控制的部件都有一个叫 Style
对于列表框和组合框,也有自画式风格选项,表 3.2
表 3.2 Style
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Style
含 义 举 例 ───────────────────────────────── Fixed
每个项目有相同的高度
Varible
每个项目有不同的高度
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ tab-set 与字符串网格的 Style
在 Strlist 程序中,列表框与 tab-set
表 3.3 列表框与 tab-set
━━━━━━━━━━━━━━━━━━━━━━━━━━ 名称 style ────────────────────────── ListBox1
lbOwnerDrawVariable Tabset1
tsOwnerDrawVariable ━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2.2 把图像加入字符串列表 上节已介绍如何把对象加入字符串列表,例程把位图对象加入 Tabset1
procedure TForm1.FormCreate(Sender: TObject); var Bitmap: TBitMap; begin Listbox1.Items := Screen.Fonts; Bitmap := TBitmap.Create; Bitmap.LoadFromFile('PHONE.BMP'); Tabset1.Tabs.AddObject('phone',Bitmap); Bitmap := TBitmap.Create; Bitmap.LoadFromFile('PRINTER.BMP'); Tabset1.Tabs.AddObject('printer ',Bitmap); end; 3.2.3 绘制自画项目 当部件的 Style 属性是自画式时, Windows
通常由 Windows 决定项目显示的大小,但应用程序可以处理这个事件并自己选择显示区域。例如,程序要用位图代替文本显示,则需要把区域设置成位图的大小。测量项目事件的名称随部件的名称不同而不同,对于列表框和组合框,该事件叫 OnMeasureItem
OnMeasureItem 的声明如下: ListBox1 MeasureItem(Control: TwinControl;Index: Integer; var Height:
Integer); 例程中响应 OnMeasureItem
procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer; var Height: Integer); begin with ListBox1.Canvas do begin Font.Name := ListBox1.Items[Index]; Height := TextHeight('A'); end; end; procedure TForm1.TabSetMeasureTab(Sender: TObject; Index: Integer; var TabWidth: Integer); var BitmapWidth: Integer; begin BitmapWidth := TBitmap( TabSet1.Tabs.Objects[Index]).Width; Inc(TabWidth, 2 + BitmapWidth); end; 在 OnMeasureItem
DrawItem( Control: TWinControl;
Index: integer; Rect: TRect; State: TOwnerDraw); 其中 Control 是包含项目的部件引用 Index 是项目的索引号 Rect
在例程的列表框中,所列项目是屏幕支持的各种字体名称,当列表框发生 OnDrawItem
procedure TForm1.DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin with ListBox1.Canvas do begin FillRect(Rect); Font.Name := ListBox1.Items[Index]; TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]); end; end; 在 Tabset 部件中,则把位图与文本同时输出,其代码如下: procedure TForm1.TabSet1DrawTab(Sender: TObject; TabCanvas: TCanvas; R: TRect; Index: Integer; Selected: Boolean); var Bitmap: TBitmap; begin Bitmap := TBitmap(TabSet1.Tabs.Objects[Index]); with TabCanvas do begin Draw(R.Left, R.Top + 4, Bitmap); TextOut(R.Left + 2 + Bitmap.Width, R.Top + 2, TabSet1.Tabs[Index]); end; end; |