这是平铺视图的内置排列。如果您想要图像下方的标签,那么您必须设置 View = LargeIcon。如果这会产生不希望的图像间距,那么您可以 P/Invoke SendMessage() 以发送 LVM_SETICONSPACING 消息。这很好用:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class TileView : ListView {
public TileView() {
mSpacing = new Size(48, 48);
}
private Size mSpacing;
public Size IconSpacing {
get { return mSpacing; }
set {
mSpacing = value;
updateSpacing();
}
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
updateSpacing();
}
private void updateSpacing() {
if (this.IsHandleCreated) {
SendMessage(this.Handle, 0x1000 + 53, IntPtr.Zero, (IntPtr)((mSpacing.Height << 16) | mSpacing.Width));
}
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
在设计器中更改新的 IconSpacing 属性,使其与 ImageList 中的图像大小配合得很好。您会立即看到效果。
Public Class TileView
Inherits ListView
Public Sub New()
mSpacing = New Size(48, 48)
End Sub
Private mSpacing As Size
Public Property IconSpacing As Size
Get
Return mSpacing
End Get
Set(ByVal value As Size)
mSpacing = value
updateSpacing()
End Set
End Property
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
MyBase.OnHandleCreated(e)
updateSpacing()
End Sub
Private Sub updateSpacing()
If Me.IsHandleCreated Then
SendMessageW(Me.Handle, &H1000 + 53, IntPtr.Zero, CType((mSpacing.Height << 16) Or mSpacing.Width, IntPtr))
End If
End Sub
Private Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
End Class