procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
i,j, ColPos: Integer;
ColRect : TRect;
begin
with ListView1 do begin
Canvas.TextOut(Rect.Left, Rect.Top, Item.Caption);
for i := 0 to Item.SubItems.Count-1 do begin
ColPos := 0;
for j := 0 to i do
ColPos := ColPos + Columns[j].Width;
ColRect := Rect;
ColRect.Left := Rect.Left+ColPos;
if i in [1] then begin
Canvas.Brush.Color := clYellow;
Canvas.FillRect(ColRect);
Canvas.TextOut(ColRect.Left, ColRect.Top, Item.SubItems[i]);
end
else begin
Canvas.TextOut(ColRect.Left, ColRect.Top, Item.SubItems[i]);
end;
end;
end;
end;
ListView의 속성 OwnerDraw를 True하세요
아래는 서브아이템중 2번째인 인덱스1번에 노란색을 칠했습니다
procedure TForm1.ListView1DrawItem(Sender: TCustomListView;
Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
i,j, ColPos: Integer;
ColRect : TRect;
begin
with ListView1 do begin
Canvas.TextOut(Rect.Left, Rect.Top, Item.Caption);
for i := 0 to Item.SubItems.Count-1 do begin
ColPos := 0;
for j := 0 to i do
ColPos := ColPos + Columns[j].Width;
ColRect := Rect;
ColRect.Left := Rect.Left+ColPos;
if i in [1] then begin
Canvas.Brush.Color := clYellow;
Canvas.FillRect(ColRect);
Canvas.TextOut(ColRect.Left, ColRect.Top, Item.SubItems[i]);
end
else begin
Canvas.TextOut(ColRect.Left, ColRect.Top, Item.SubItems[i]);
end;
end;
end;
end;