似乎没有办法更改 .NET ListView 中所有行的填充(或行高)。有人有优雅的破解方法吗?
我知道这篇文章已经相当老了,但是,如果您从未找到最佳选择,我有一篇博文可能会有所帮助,它涉及使用 LVM_SETICONSPACING。
根据我的博客,
最初,您需要添加:
1
| using System.Runtime.InteropServices; |
接下来,您需要导入 DLL,以便您可以利用 SendMessage 来修改 ListView 参数。
1 2
| [DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); |
完成后,创建以下两个函数:
1 2 3 4 5 6 7 8 9 10 11
| public int MakeLong(short lowPart, short highPart)
{
return (int)(((ushort)lowPart) | (uint)(highPart 16));
}
public void ListViewItem_SetSpacing(ListView listview, short leftPadding, short topPadding)
{
const int LVM_FIRST = 0x1000;
const int LVM_SETICONSPACING = LVM_FIRST + 53;
SendMessage(listview.Handle, LVM_SETICONSPACING, IntPtr.Zero, (IntPtr)MakeLong(leftPadding, topPadding));
} |
然后要使用该功能,只需传入您的 ListView,并设置值。在示例中,64 像素是图像宽度,32 像素是我的水平间距/填充,100 像素是图像高度,16 像素是我的垂直间距/填充,这两个参数都需要至少 4 像素。
1
| ListViewItem_SetSpacing(this.listView1, 64 + 32, 100 + 16); |
一种解决方法是使用与您希望项目一样高的 ImageList。只需用背景颜色填充空白图像。您甚至可以将图像设置为 1 宽,以便在水平方向上不占用太多空间。