1

I would like to draw an icon/bmp into a subitem of a TListView using delphi. But I don't know how to accomplish that. It works fine for the first item on the list, but having problems with the subitems.

4 Answers 4

5

You could use the CustomDrawSubItem event.

The example below ignores the text and draws rectangles. Unfortunately it is a bit of a hassle to get the rectangle for the right column, but this approach works:

procedure TForm.ListViewCustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  r : TRect;
  i : Integer;
begin
  r := Item.DisplayRect(drBounds);
  for i := 0 to SubItem-1 do begin
    r.Left := r.Left + ListView.Columns.Items[i].Width;
    r.Right := r.Left  + ListView.Columns.Items[i+1].Width;
  end;
  case SubItem of
     1 : ListView.Canvas.Pen.Color := clRed;
  else
    ListView.Canvas.Pen.Color := clBlue;
  end;

  ListView.Canvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom);
  DefaultDraw := False;
end;
1
  • Thanks, I tried and it worked partially. It is possible to draw anything within the subitem, but after that there is a problem drawing the column after resizing or moving the component (TListView).
    – tecnotalk
    Apr 6, 2009 at 9:58
1

Use a TImageList component to hold your images, assign it to the listviews's SmallImages property and set subitem's ImageIndex.

2
  • 1
    He's talking about a LIST view, not a TREE view. List views' subitems form other columns of data, and the Windows list view only accepts icons on the primary column. Apr 2, 2009 at 17:10
  • Thanks for your answers. As TOndrej says I'm using a TListView component, a small constraint is that I have to do this on Delphi 4. And yes, it exists already! :)
    – tecnotalk
    Apr 6, 2009 at 9:43
1

After tried Gamecat proposed solution, there are serious issues handling the redraw event after resizing the component, so at the end I came up with 2 possible workarounds:

  1. Since subitems are Strings I handled to change the Font Family and using Wingdings I used a triangle-like character, then I just changed the font color to make it look like a glyph. (I know, it is not very clean, but it worked for me from a time and effort perspective)

  2. Use a TDataGrid from the beginning, it knows the concept of cells and we can add practically anything and no need to worry with redrawing events. (Useless for me, cause the existing component already had a lot of functionality build on it).

0

OK, 13 years late, but this is a clean way to get the bounds.

uses Winapi.CommCtrl;

...

procedure TFrmMain.ListView1(
  Sender: TCustomListView;
  Item: TListItem; 
  SubItem: integer; 
  State: TCustomDrawState;
  var DefaultDraw: Boolean);    
var
  lv: TListView absolute Sender;
  SubItemRect: TRect;
begin
  ListView_GetSubItemRect(lv.Handle, Item.Index, SubItem, LVIR_BOUNDS, @SubItemRect);

  // Now you know the boundaries of the sub item...

  // So you could do something like:
  lv.Canvas.Draw(SubItemRect.Left, SubItemRect.Top, MyBitmap);

end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.