.NET ListView行填充

2 c# listview

似乎没有办法更改.NET ListView中所有行的填充(或行高).有人有优雅的黑客攻击吗?

Qui*_*hns 8

我知道这篇文章相当陈旧,但是,如果你找不到最好的选择,我有一个可能有用的博客文章,它涉及利用LVM_SETICONSPACING.

根据我的博客,

最初,您需要添加:

using System.Runtime.InteropServices;
Run Code Online (Sandbox Code Playgroud)

接下来,您需要导入DLL,以便可以使用SendMessage来修改ListView参数.

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
Run Code Online (Sandbox Code Playgroud)

完成后,创建以下两个函数:

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));      
} 
Run Code Online (Sandbox Code Playgroud)

然后使用该函数,只需传入ListView,然后设置值.在该示例中,64像素是图像宽度,32像素是我的水平间距/填充,100像素是图像高度,16像素是我的垂直间距/填充,并且两个参数都需要最少4个像素.

ListViewItem_SetSpacing(this.listView1, 64 + 32, 100 + 16);
Run Code Online (Sandbox Code Playgroud)

  • `LVM_SETICONSPACING`仅适用于图标视图中的ListView。在“详细信息”视图中更改行填充(而不是行高,正如 @Joel Lucsy 所说可以使用图像列表来完成)时,有什么方法可以做等效的事情吗? (2认同)

Joe*_*csy 5

一种解决方法是使用与您希望的项目一样高的 ImageList。只需用背景颜色填充空白图像即可。您甚至可以使图像 1 变宽,以便在水平方向上不占用太多空间。