僅字體顏色將如您的代碼所示。 如果你想改變背景顏色,你必須自己繪製項目和子項目,並將DefaultDraw設置爲false。
這可能是這樣的:
procedure TMyForm.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem
; State: TCustomDrawState; var DefaultDraw: Boolean);
var
rt, r: TRect;
s: String;
i: Integer;
c:TCanvas;
// Fit the rect used for TextRect
Procedure PrepareTextRect;
begin
rt := r;
rt.Left := rt.Left + 5;
rt.Top := rt.Top + 1;
end;
begin
c := Sender.Canvas;
if (cdsSelected in State) then
begin
c.Brush.Color := clRed;
c.Font.Color := clYellow;
// will get the rect for Item + Subitems in ViewStyle = vsReport
r := Item.DisplayRect(drBounds);
c.FillRect(r);
// set width to get fitting rt for tfEndEllipsis
r.Right := r.Left + TListView(Sender).Columns[0].Width;
s := Item.Caption;
PrepareTextRect;
c.TextRect(rt, s, [tfSingleLine, tfEndEllipsis]);
if TListView(Sender).ViewStyle = vsReport then
begin // Paint the Subitems if ViewStyle = vsReport
for i := 0 to Item.SubItems.Count - 1 do
begin
r.Left := r.Left + TListView(Sender).Columns.Items[i].Width;
r.Right := r.Left + TListView(Sender).Columns.Items[i + 1].Width;
PrepareTextRect;
s := Item.SubItems[i];
c.TextRect(rt, s, [tfSingleLine, tfEndEllipsis]);
end;
end;
DefaultDraw := false;
end;
end;
![enter image description here](https://i.stack.imgur.com/df2ah.png)
你有沒有設置'OwnerDraw'屬性爲真? – TLama 2014-09-22 12:58:29
我運行此代碼,並在白色背景上獲得黃色文本。你看到了什麼。 – 2014-09-22 13:38:33
我看到了windows的默認顏色,就好像'CustomDraw'事件根本不存在。 (在Delphi 2009/Win7/Windows經典主題中測試過) – 2014-09-22 13:47:34