using drawitem and drawsubitem in tlistview lazarus / 31-LCL ---------------------------------------------------------------------- mangakissa · 2020-04-07 13:06 Using TListview with subitems is more difficult on Lazarus then Delphi. I have a Listview with vsreport. To color event rows I use this code (ownerdraw to true): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmBasisdata.LVBasisdataCustomDrawItem(Sender: TCustomListView;  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);var rt, r: TRect;begin  if Item.Index mod 2 = 0 then    TListView(Sender).Canvas.Brush.Color := clSilver  else    TListView(Sender).Canvas.Brush.Color := clWhite;  r := Item.DisplayRect(drBounds);  TListView(Sender).Canvas.FillRect(r);  rt := r;  rt.Left := rt.Left + 5;  rt.Top := rt.Top + 1;   TListView(Sender).Canvas.TextOut(Item.Left, Item.Top, Item.Caption);  DefaultDraw:=False;end;                     This gives a full length of row with the selected color. But subitems are not showed. AFAIK you have to use CustomDrawSubItem: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmBasisdata.LVBasisdataCustomDrawSubItem(Sender: TCustomListView;  Item: TListItem; SubItem: Integer; State: TCustomDrawState;  var DefaultDraw: Boolean);begin  TListView(Sender).Canvas.TextOut(Item.Left, Item.Top, Item.SubItems[subitem]);  DefaultDraw := false;end;     But it shows no subitems. How do I get them displayed? ---------------------------------------------------------------------- wp · 2020-04-07 14:09 I suppose you want to have alternating background colors of the rows. I do it this way: Keep OwnerDraw at its default, false.Add an event handler for OnCustomDrawItem: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);begin  if odd(Item.Index) then    Sender.Canvas.Brush.Color := clSilver;end;  Note that I only change the brush color, all the rest should be done by the built-in routines. There DefaultDraw is not changed and left at its default true. If you set it to false then you must do all the background fills, text output, icon drawing etc. [EDIT] As I noticed this procedure works only on Windows. ---------------------------------------------------------------------- jamie · 2020-04-07 21:25 The poster needs the use the correct display rectangle for the sub items... DisplayRectSubitem.left, top etc... ---------------------------------------------------------------------- mangakissa · 2020-04-08 08:41 Thank you jamie and wp. I created this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmBasisdata.LVBasisdataCustomDrawItem(Sender: TCustomListView;  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);var rt, r    : TRect;    index    : Integer;    mycanvas : TCanvas;    style    : TTextStyle; // Fit the rect used for TextRectProcedure PrepareTextRect;begin  rt := r;  rt.Left := rt.Left + 5;  rt.Top := rt.Top + 1;end; begin  if (cdsSelected in state) then  begin    mycanvas := Sender.Canvas;    mycanvas.Brush.Color := clInactiveCaption ;    // will get the rect for Item + Subitems in ViewStyle = vsReport    r := Item.DisplayRect(drBounds);    mycanvas.FillRect(r);    // set width to get fitting rt for tfEndEllipsis    r.Right := r.Left + TListView(Sender).Columns[0].Width;    PrepareTextRect;    style.SingleLine := true;    style.EndEllipsis := true;    mycanvas.TextRect(rt, rt.Left, rt.Top, Item.Caption, style);     if TListView(Sender).ViewStyle = vsReport then    begin // Paint the Subitems if ViewStyle = vsReport      for index := 0 to Item.SubItems.Count - 1 do      begin        r.Left := r.Left + TListView(Sender).Columns.Items[index].Width;        r.Right := r.Left + TListView(Sender).Columns.Items[index + 1].Width;        PrepareTextRect;        mycanvas.TextRect(rt,rt.Left, rt.Top,  Item.SubItems[index], style);      end;    end;    DefaultDraw := false;  end else  begin  if odd(Item.Index) then    myCanvas.Brush.Color := clSilver;  end;              end; procedure TFrmBasisdata.LVBasisdataCustomDrawSubItem(Sender: TCustomListView;  Item: TListItem; SubItem: Integer; State: TCustomDrawState;  var DefaultDraw: Boolean);begin  if odd(Item.Index) then    Sender.Canvas.Brush.Color := clSilver;end;