很简单的ListBox自画
最终效果图:Lazarus也一样
ListBox的两个设置:
自画事件:ListBox1DrawItem
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin With ListBox1.Canvas Do Begin Brush.Color := $00FF9184; //设置背景颜色并填充背景 FillRect(Rect); Brush.Color := clBlack; //设置圆角矩形颜色并画出圆角矩形 Pen.Color := $00131315; RoundRect(Rect.Left + 3, Rect.Top + 3, Rect.Right - 3, Rect.Bottom - 3, 5, 5); Brush.Color := $00A2F9F0; Pen.Color := clInfoBk; RoundRect(Rect.Left + 3, Rect.Top + 3, Rect.Right - 6, Rect.Bottom - 6, 5, 5); //以不同的宽度和高度再画一次,实现立体效果 If (odSelected In State) Then {//如果是当前选中项} Begin Brush.Color := $00BBBBFF; //以不同的背景色画出选中项的圆角矩形 RoundRect(Rect.Left + 3, Rect.Top + 3, Rect.Right - 3, Rect.Bottom - 3, 5, 5); //选中项的文字颜色 End; ImageList1.Draw(ListBox1.Canvas, Rect.Left + 6, //画出图标 Rect.Top , index, true); Font.Color := $00A25155; //分别绘出各行文字 TextOut(Rect.Left + ImageList1.Width + 10, Rect.Top + 4,ListBox1.Items[index]); Font.Color := $0F01630E; TextOut(Rect.Left + ImageList1.Width + 15, Rect.Top - (ListBox1.Font.Height - 4) + 2, '语言: ' ); TextOut(Rect.Left + ImageList1.Width + 10, Rect.Top - (ListBox1.Font.Height - 4) * 2, '文章总数: ' ); If (odFocused In State) Then ListBox1.Canvas.DrawFocusRect(Rect); End; End;
先要设置 ImageList1的宽和高,再导入图片,否则,图片会被清空。
测试图标下载:
链接:https://pan.baidu.com/s/1eCLTHlpc06CwH1WL9AgHkw
提取码:83rn
Lazarus也一样
链接:https://pan.baidu.com/s/1teNb8puiVhn_aEKb67Op9g
提取码:chtl