上一页 1 2 3 4 下一页 二、 特殊效果的实现
在窗体(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); 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;
| 具体横向滚动区域的宽度可通过具体计算得出,在此从略。
上一页 1 2 3 4 下一页 |