VCL下ListBox自画

发布时间 2023-06-15 16:22:49作者: 涂磊

最终效果图:

 ListBox的两个设置:

自画事件:LixtBox1DrawItem

procedure TForm1.LixtBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
    With LixtBox1.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(LixtBox1.Canvas, Rect.Left + 6, //画出图标
        Rect.Top ,
        index, true);

      Font.Color := $00A25155; //分别绘出各行文字
      TextOut(Rect.Left + ImageList1.Width + 10, Rect.Top + 4, '测试');
      Font.Color := $0F01630E;


      TextOut(Rect.Left + ImageList1.Width + 15, Rect.Top - (LixtBox1.Font.Height - 4) + 2, '语言: ' );
      TextOut(Rect.Left + ImageList1.Width + 10, Rect.Top - (LixtBox1.Font.Height - 4) * 2, '文章总数: ' );

      If (odFocused In State) Then LixtBox1.Canvas.DrawFocusRect(Rect);
    End;
End;

 ImageList1的宽和高