Delphi中ListBox控件的六种特效
点击次数:73 次 发布日期:2008-11-09 08:39:17 作者:源代码网
|
源代码网推荐 本文就Delphi5中的TListBox控件,通过多种手段实现了它的七种特殊视觉效果,以期对广大程序爱好者在界面设计上有所启发与帮助。 源代码网推荐 源代码网推荐 一、 基础知识 源代码网推荐 源代码网推荐 涉及TListBox自定义重绘的属性和事件: 源代码网推荐 源代码网推荐 属性: 源代码网推荐 源代码网推荐 Style: 取值为lbStandard(标准风格),lbOwnerDrawFixed(所有者固定绘制风格),lbOwnerDrawVariable(所有者可变绘制风格) 源代码网推荐 源代码网推荐 说明: 源代码网推荐 源代码网推荐 1. 当Style = lbStandard时,使用控件默认的绘制风格。 源代码网推荐 源代码网推荐 2. 当Style = lbOwnerDrawFixed时,用户只能在控件默认大小的区域绘图。 源代码网推荐 源代码网推荐 3. 当Style = lbOwnerDrawVariable时,用户可改变控件默认的绘图区域大小并决定如何绘图。 源代码网推荐 源代码网推荐 事件: 源代码网推荐 源代码网推荐 OnMeasureItem:当Style = lbOwnerDrawVariable时计算TListBox中某项的高度时调用。 源代码网推荐 源代码网推荐 OnDrawItem :当Style = lbOwnerDrawVariable时由用户例程确定如何绘制TlistItem。 源代码网推荐 源代码网推荐 由此,可以看出,要实现定制界面风格的TListBox,首先,需要设置TlistBox的Style 属性为lbOwnerDrawVariable,其次,需要写自定义的重绘事件。 源代码网推荐 源代码网推荐 二、 特殊效果的实现 源代码网推荐 源代码网推荐 在窗体(Form1)上放置5个ListBox,名称分别为ListBox1……ListBox5,将所有ListBox的Style属性设置为lbOwnerDrawVariable;在Form1上添加两个TImageList控件,命名为ImageList1,ImageList2;在ImageList1中装入两个16X16大小的图标;添加两个TButton控件,命名为Button1,Button2;再添加一个TImage控件,命名为Image1。其它操作,见下。 源代码网推荐 源代码网推荐 1. 具有图标及热链接效果的列表框 源代码网推荐 源代码网推荐 在ListBox1的Items属性中添加几个字符串,并在ListBox1的OnDrawItem事件中编写代码如下: 源代码网推荐 源代码网推荐 源代码网推荐 procedure TForm1.ListBox2DrawItem(Control: TWinControl; Index: Integer; 源代码网推荐 Rect: TRect; State: TOwnerDrawState); 源代码网推荐 var 源代码网推荐 AIcon, BIcon: TIcon; 源代码网推荐 begin 源代码网推荐 try 源代码网推荐 file://从上述ImageList1中装入两个图标 源代码网推荐 AIcon := TIcon.Create; 源代码网推荐 BIcon := TIcon.Create; 源代码网推荐 file://装入图标到AIcon, BIcon 源代码网推荐 ImageList1.GetIcon(0, AIcon); 源代码网推荐 ImageList1.GetIcon(1, BIcon); 软件开发网 www.mscto.com 源代码网推荐 file://填充绘图区 源代码网推荐 ListBox1.Canvas.FillRect(Rect); 源代码网推荐 file://判断ListBox1中的当前重绘项是否被选中,根据状态装入不同的图标 源代码网推荐 if odSelected in State then 源代码网推荐 ListBox1.Canvas.Draw(Rect.Left, Rect.Top, AIcon) 源代码网推荐 else 源代码网推荐 ListBox1.Canvas.Draw(Rect.Left, Rect.Top, BIcon); 源代码网推荐 file://输出文字 源代码网推荐 ListBox1.Canvas.TextOut(Rect.Left AIcon.Width div 2, Rect.Top 2, ListBox1.Items[Index]); 源代码网推荐 finally 源代码网推荐 AIcon.Free; 源代码网推荐 BIcon.Free; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 注:也可在OnMeasureItem事件中改变列表项的高度。 源代码网推荐 源代码网推荐 2. 具有横向滚动条效果的列表框 源代码网推荐 源代码网推荐 在Form1上Button1的Click事件中书写如下代码: 源代码网推荐 源代码网推荐 procedure TForm1.Button1Click(Sender: TObject); 源代码网推荐 begin 源代码网推荐 SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, ListBox1.Width 30, 0); 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 具体横向滚动区域的宽度可通过具体计算得出,在此从略。 源代码网推荐 源代码网推荐 3. 具有图标,背景图片及透明文字效果的列表框 源代码网推荐 源代码网推荐 说明: 源代码网推荐 源代码网推荐 1. 要使TListBox具有指定位图的背景,须考虑到以下问题: 源代码网推荐 源代码网推荐 如果TListBox的Items足够多,那么,在TListBox的OnDrawItem事件的Rect区域输出位图即可使整个TListBox的Canvas充满位图背景;反之,则会出现TListBox中上半部分有Item的地方有背景,下半部分没有Item的部分仍然为白色,影响视觉效果。 源代码网推荐 源代码网推荐 2. TListBox的Color属性决定了文本输出时的背景,通常为clWindow,这样用TextOut时就会出现不协调的白色文字背景。因此,要实现透明文字输出效果,可以通过设置ListBox.Canvas.Brush.Style := bsClear,这样,绘制的文字没有背景色,从而实现文字透明输出效果。 源代码网推荐 源代码网推荐 操作: 源代码网推荐 源代码网推荐 在ListBox2的Items属性中添加几个字符串;设置Form1上的Image1的Picture属性为一指定图片。在ListBox2的OnDrawItem事件中书写如下代码: 源代码网推荐 源代码网推荐 procedure TForm1.ListBox2DrawItem(Control: TWinControl; Index: Integer; 源代码网推荐 Rect: TRect; State: TOwnerDrawState); 源代码网推荐 var 源代码网推荐 AIcon: TIcon; 源代码网推荐 I, K : Integer; 源代码网推荐 ARect, BRect: TRect; 源代码网推荐 H : Integer; 源代码网推荐 AStyle: TBrushStyle; 源代码网推荐 begin 源代码网推荐 try 源代码网推荐 file://计算Item数量 源代码网推荐 I := ListBox2.Items.Count-1; 源代码网推荐 AIcon := TIcon.Create; 源代码网推荐 file://装入图标 源代码网推荐 ImageList1.GetIcon(0, AIcon); 源代码网推荐 file://填充区域 源代码网推荐 ListBox2.Canvas.FillRect(Rect); 源代码网推荐 file://计算Rect绘图区的高度 源代码网推荐 H := Rect.Bottom - Rect.Top; 源代码网推荐 file://如果当前项是Item的最后一项,则在Canvas上没有Item的空白区绘制背景 源代码网推荐 if Index = I then 源代码网推荐 begin 源代码网推荐 K := 1; 源代码网推荐 ARect := Rect; 源代码网推荐 file://如果当前绘图项的底部小于ListBox2的Canvas的底部,有空白区域 源代码网推荐 While ARect.Bottom < ListBox2.Canvas.ClipRect.Bottom do 源代码网推荐 begin 源代码网推荐 file://一次计算下一个绘图区域 源代码网推荐 ARect.Top := Rect.Top K * H; 源代码网推荐 ARect.Bottom := ARect.Top H; 源代码网推荐 ListBox2.Canvas.stretchDraw(ARect, Image1.Picture.Bitmap); 源代码网推荐 Inc(K); 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 file://绘制当前项 源代码网推荐 ListBox2.Canvas.stretchDraw(Rect, Image1.Picture.Bitmap); 源代码网推荐 file://绘制图标 源代码网推荐 ListBox2.Canvas.Draw(Rect.Left, Rect.Top, AIcon); 源代码网推荐 ARect := Rect; 源代码网推荐 ARect.Left := Rect.Left AIcon.Width div 2; 源代码网推荐 ARect.Top := ARect.top 2; 源代码网推荐 file://保存当前画笔的风格 源代码网推荐 AStyle := Listbox2.Canvas.Brush.Style; 源代码网推荐 file://当前选中的Item要填充蓝色背景 源代码网推荐 if odSelected in State then 源代码网推荐 begin 源代码网推荐 ListBox2.Canvas.Brush.Style := bsSolid; 源代码网推荐 Listbox2.Canvas.Brush.Color := clBlue; 源代码网推荐 end 源代码网推荐 else 源代码网推荐 begin 源代码网推荐 file://未选中项透明背景,前景色为黑色 源代码网推荐 ListBox2.Canvas.Brush.Style := bsClear; 源代码网推荐 Listbox2.Font.Color := clBlack; 源代码网推荐 end; 源代码网推荐 file://输出文字 源代码网推荐 ListBox2.Canvas.TextOut(ARect.Left, ARect.top, ListBox2.Items[Index]); 源代码网推荐 file://恢复当前画笔的风格 源代码网推荐 ListBox2.Canvas.Brush.Style := AStyle; 源代码网推荐 finally 源代码网推荐 AIcon.Free; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 以上方法实现了TListBox即具有背景图片,又具有图标和透明文字效果,极大的改善了TListBox的显示效果。 源代码网推荐 源代码网推荐 4. 具有图标,背景图片,透明文字及文字对齐方式效果的列表框 源代码网推荐 源代码网推荐 要实现文字对齐效果,可通过Windows Api函数:DrawText实现。 源代码网推荐 源代码网推荐 操作: 源代码网推荐 源代码网推荐 将ListBox2的OnDrawItem事件中的代码复制到ListBox3的OnDrawItem事件中,并将复制代码中所有的ListBox2改为ListBox3。 源代码网推荐 源代码网推荐 将上述修改后代码中的ListBox3.Canvas.TextOut(Rect.Left AIcon.Width div 2, Rect.Top 2, ListBox3.Items[Index]); 语句删除,并在该处添加以下语句: 源代码网推荐 源代码网推荐 file://计算除掉图标所占区域后的区域,用于确定绘制文字的区域范围 源代码网推荐 源代码网推荐 ARect := Rect; 源代码网推荐 ARect.Left := Rect.Left AIcon.Width div 2; 源代码网推荐 ARect.Top := ARect.top 2; 源代码网推荐 file://Windows Api函数调用 源代码网推荐 DrawText(ListBox3.Canvas.Handle, PChar(ListBox3.Items[Index]), Length(ListBox3.Items[Index]), ARect, 0); file://0-左对齐, 1---居中, 2--右对齐 软件开发网 www.mscto.com 源代码网推荐 源代码网推荐 源代码网推荐 注:通知ListBox3重绘可通过命令ListBox3.Refresh实现 源代码网推荐 源代码网推荐 5. 照片列表框效果 源代码网推荐 源代码网推荐 在ListBox4的Items属性中添加几个字符串;设置ImageList2的Width为148,Height为58;在ImageList2中装入与ListBox4中Items相同字符串数量的图片,大小148 X 58像素单位。 源代码网推荐 源代码网推荐 在ListBox4的OnMeasureItem事件中书写如下代码: 源代码网推荐 源代码网推荐 procedure TForm1.ListBox4MeasureItem(Control: TWinControl; Index: Integer; 源代码网推荐 var Height: Integer); 源代码网推荐 begin 源代码网推荐 file://控制图片的高度 源代码网推荐 Height := 59; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 在ListBox4的OnDrawItem事件中书写如下代码: 源代码网推荐 源代码网推荐 procedure TForm1.ListBox4DrawItem(Control: TWinControl; Index: Integer; 源代码网推荐 Rect: TRect; State: TOwnerDrawState); 源代码网推荐 var 源代码网推荐 ABmp: TBitmap; 源代码网推荐 begin 源代码网推荐 try 源代码网推荐 ABmp := TBitmap.Create; 源代码网推荐 ImageList2.GetBitmap(Index, ABmp); 源代码网推荐 ListBox4.Canvas.FillRect(Rect); 源代码网推荐 ListBox4.Canvas.Draw(Rect.Left, Rect.Top, ABmp); 源代码网推荐 finally 源代码网推荐 ABmp.Free; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 这种利用TListBox实现的照片框效果,对于照片,商品图片的显示有一定价值。 源代码网推荐 源代码网推荐 6. 以缩略图方式浏览某个文件夹下图片效果的列表框 源代码网推荐 源代码网推荐 在ListBox5的OnMeasureItem事件中书写如下代码: 源代码网推荐 源代码网推荐 procedure TForm1.ListBox5MeasureItem(Control: TWinControl; Index: Integer; 源代码网推荐 var Height: Integer); 源代码网推荐 begin 源代码网推荐 file://控制图片的高度 源代码网推荐 Height := 59; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 在ListBox5的OnDrawItem事件中书写如下代码: 源代码网推荐 源代码网推荐 procedure TForm1.ListBox5DrawItem(Control: TWinControl; Index: Integer; 源代码网推荐 Rect: TRect; State: TOwnerDrawState); 源代码网推荐 var 源代码网推荐 file://图片文件名 源代码网推荐 Fn: string; 源代码网推荐 ABmp: TBitmap; 源代码网推荐 begin 源代码网推荐 try 源代码网推荐 ABmp := TBitmap.Create; 源代码网推荐 Fn := ListBox5.Items[Index]; 源代码网推荐 ABmp.LoadFromFile(ListBox5.Items[Index]); 源代码网推荐 Dec(Rect.Bottom); 源代码网推荐 ListBox5.Canvas.FillRect(Rect); 源代码网推荐 ListBox5.Canvas.StretchDraw(Rect, ABmp); 源代码网推荐 finally 源代码网推荐 ABmp.Free; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 设置Button2的Caption为"预览",在其Click事件中书写如下代码: 源代码网推荐 源代码网推荐 var 源代码网推荐 sr: TSearchRec; 源代码网推荐 Dir: string; 源代码网推荐 begin 源代码网推荐 Dir := ""; 源代码网推荐 file://选择目录对话框,需要在Uses中加入对FileCtrl单元的引用声明 源代码网推荐 if SelectDirectory("选择图片目录", "", Dir) then 源代码网推荐 begin 源代码网推荐 ListBox5.Items.Clear; 源代码网推荐 file://搜索该目录下的所有bmp文件 源代码网推荐 if FindFirst(Dir "*.bmp", faReadOnly, sr) = 0 then 源代码网推荐 begin 源代码网推荐 ListBox5.Items.Add(Dir "" Sr.Name); 源代码网推荐 while FindNext(sr) = 0 do 源代码网推荐 begin 源代码网推荐 ListBox5.Items.Add(Dir "" Sr.Name); 源代码网推荐 end; 源代码网推荐 FindClose(sr); 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网供稿. |
