When I use a TListView (ViewStyle = vsReport) I can autofit the width of the columns setting the LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER values in the Width property of each column, now I start to use the Listview in virtual mode, but the width of the columns is not modified according to these values. So the question is : How I can adjust the width of the columns to fit to the content or header, when the lisvtiew is in virtual mode?
When I use a TListView (ViewStyle = vsReport) I can autofit the width of
Share
Since the list view in virtual mode don’t know the item captions in advance (because it asks only for data of the visible area) it also cannot know the width of the widest one, so that’s the reason why the autosize flag of the
LVM_SETCOLUMNWIDTHbehaves this way.Thus the only way is to write a custom function which will query all your data, measure the text widths of all future captions and set the column width to the value of the widest one.
The following example shows how to do it. It uses the
ListView_GetStringWidthmacro for the text width calculations (it seems to be the most natural way to do this). However the problem is the value of the text padding. As it’s stated in the documentation:But they didn’t mention there how to get the padding value (and seems
they won'tto do so). Some people say (e.g.here) that it’s enough to use 6 px for item’s padding and 12 px for subitem’s padding, but it isn’t (at least for this example on Windows 7).