10

I need to add just 2px :) to a height of a row in a list view (a custom drawn progress bar is too narrow now).

There are two good answers Change Listview item height, http://www.delphipages.com/forum/showthread.php?t=49939, but I couldn't do it.

I know that it is possible to do with an image list, but I have already 16x16 images :)

Can anybody help me? I'll appreciate it.

Community
  • 1
  • 1
maxfax
  • 4,281
  • 12
  • 74
  • 120

2 Answers2

16

Respond to the CN_MEASUREITEM control notification message, as follows:

type
  TListView = class(ComCtrls.TListView)
  private
    procedure CNMeasureItem(var Message: TWMMeasureItem); message CN_MEASUREITEM;
  end;

  TForm1 = class(TForm)
    ...

procedure TListView.CNMeasureItem(var Message: TWMMeasureItem);
begin
  inherited;
  Inc(Message.MeasureItemStruct.itemHeight, 2);
end;

Note: this message will only be send if the OwnerDraw property is true.

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • Wow! Thanks!!! I saw a source code of TMS' TAdvListView and this message `CNDrawItem(var Message: TWMDrawItem);` is handled too (the link delphipages has the same code as in TMS). Why? – maxfax Aug 15 '11 at 00:53
  • Dunno TAdvListView. The default TListView has the OnDrawItem event already. – NGLN Aug 15 '11 at 00:56
  • id doesn't matter much :) It works!!! You are a good programmer & as a person! Thanks! – maxfax Aug 15 '11 at 01:03
  • One "small" and funny bug :) -> `ListView1.Align:=alClient;` please add items, resize a window and see what will happen :) How to fix? Thanks! – maxfax Aug 17 '11 at 19:48
  • Yeah, you probably are better off with `Message.MeasureItemStruct.itemHeight := 21;`. – NGLN Aug 18 '11 at 03:23
  • 1
    This doesn't work with a `TCustomListView` - can't seem to find the way of doing this. – James Oct 31 '14 at 15:14
  • @James Strange. Maybe the why is worth its own question. – NGLN Nov 03 '14 at 17:53
  • great solution! is there any way to change the header row's height? – tcxbalage Feb 15 '20 at 16:44
  • @tcxbalage Not easy. The fun starts by retrieving the header control with the [ListView_GetHeader](https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-listview_getheader) macro, but simply setting its height won't do. You have to respond to all the messages resetting the height as well as adjusting the list view's client rect. If you are really interested, then you should ask a new question linking to this one, because a complete answer would not fit here. – NGLN Mar 09 '20 at 16:32
8

A quick and dirty alternative without writing any code would be to add a TImageList, set its width to 1 and its height to whatever you want the lines height to be and assign it to the SmallImages of the listview.

CodeX
  • 717
  • 7
  • 23
  • 1
    I don't think it's a "dirty" alternative, works perfectly without side effects! – Guybrush Aug 14 '18 at 16:19
  • I am using actually SmallImages property, thus it is not a good solution for me. But, as found at https://stackoverflow.com/a/6564492/1960514, you can use also StateImages for this purpose! – Jacek Krawczyk Sep 04 '20 at 14:31