如何在虚拟模式下自动调整列表视图的列宽?
当我使用 TListView (ViewStyle = vsReport) 时,我可以自动调整列的宽度,设置 LVSCW_AUTOSIZE
或 LVSCW_AUTOSIZE_USEHEADER
中的值每列的宽度属性,现在我开始在虚拟模式下使用Listview,但是列的宽度没有根据这些值进行修改。所以问题是:当 lisvtiew 处于虚拟模式时,如何调整列的宽度以适应内容或标题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于虚拟模式下的列表视图事先不知道项目标题(因为它只要求可见区域的数据),它也无法知道最宽的标题的宽度,所以这就是 < 的 autosize 标志的原因a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb761163%28v=vs.85%29.aspx" rel="noreferrer">
LVM_SETCOLUMNWIDTH
的行为方式如下。因此,唯一的方法是编写一个自定义函数来查询所有数据,测量所有未来字幕的文本宽度并将列宽设置为最宽的值。
以下示例显示如何执行此操作。它使用
ListView_GetStringWidth
宏用于文本宽度计算(这似乎是最自然的方法)。然而问题是文本填充的值。正如文档中所述:但他们没有提到如何获取填充值(似乎
他们不会
这样做)。有些人说(例如此处
)使用 6 px 作为项目的填充,使用 12 px 作为子项目的填充就足够了,但事实并非如此(至少对于 Windows 7 上的这个示例)。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_SETCOLUMNWIDTH
behaves 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_GetStringWidth
macro 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't
to 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).考虑这个由 < 编写的辅助功能单元 a href="https://stackoverflow.com/users/91299/rruz">RRUZ。
辅助函数摘录:
模式(参数)可以是:
我希望它将作为您的要求的一个良好起点。
Consider this helper function unit written by RRUZ.
Excerpt of the helper functions:
Mode (Parameter) could be:
I hope it will serve as a good starting point for your requirement.
这是避免列太窄的另一种可能的解决方案。但是,它需要一些有关需要显示的数据的知识,因此它不是通用解决方案。
使用最长/最宽的数据项构建 ListViewItem。切换到非虚拟模式并仅添加此最大 ListViewItem。根据最大项自动调整列宽,然后删除最大项,并切换回虚拟模式。例如:
根据您的示例格式值,某些列现在可能有点太宽,但至少没有列会太窄..
Here is another possible solution to avoid too narrow columns. However, it requires some knowledge about the data you need to display, so it is not a general solution ..
Build a ListViewItem using the longest/widest data items. Switch to non-virtual mode and add just this maximum ListViewItem. Auto-adjust the column width based on the max item, then delete the max item, and switch back to virtual mode. E.g.:
Depending on your sample format values, some columns now might be a bit too wide, but at least no column will be too narrow ..