扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:陈立平 来源:yesky 2007年11月1日
关键字:
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; |
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--右对齐 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者