View 的 ListView 间距问题:WinForms 中的列表

ListView spacing issue with View: List in WinForms

我在视图:列表模式下有一个 ListView

我的问题是间距。我只有图像,我想要水平滚动。

问题是这样的:

图像之间存在巨大差距。当我使用 View: LargeIcons 或 Tile 模式时,也有间隙,但是,使用 LVM_SETICONSPACING 解决了这个问题。但是 tile/largeicons 是垂直滚动,我不想要。

所以我可以做两件事,但不确定如何实现。 1.弄清楚如何在列表模式下固定间距。 2. 使 LargeIcons/Tile 模式水平滚动。

我将附上一些处理列表视图的代码:

这是适用于 tile/large 图标的图标间距:

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)>
Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Param) As Int32
End Function

<StructLayout(LayoutKind.Explicit)>
Private Structure Param
    <FieldOffset(0)> Public LoWord As Int16
    <FieldOffset(2)> Public HiWord As Int16
End Structure

Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_SETICONSPACING As Integer = LVM_FIRST + 53

Public Sub SetListviewIconSpacing(lv As ListView, ByVal x As Int16, ByVal y As Int16)
    ' The LOWORD specifies the distance, in pixels, to set between icons on the x-axis. 
    ' The HIWORD specifies the distance, in pixels, to set between icons on the y-axis.
    Dim lparam As Param = New Param With {.LoWord = x, .HiWord = y}
    SendMessage(lv.Handle, LVM_SETICONSPACING, 0, lparam)
    lv.Refresh()
End Sub

这是使用图像列表添加图像的一般编码:

Dim img As Image
Dim imgList As ImageList = New ImageList()
Dim listItem As ListViewItem
'''SetListviewIconSpacing(lstViewAH, 101, 103)
imgList.ColorDepth = ColorDepth.Depth16Bit
imgList.ImageSize = New Size(92, 92)
lstViewAH.StateImageList = imgList
lstViewAH.LargeImageList = imgList
lstViewAH.SmallImageList = imgList
For i As Integer = 0 To arrX.Count - 1
    img = Image.FromFile(CharactersFolder & "000" & arrX(i).ToString() & ".jpg")
    imgList.Images.Add("itemImageKey" & i, img)
    listItem = New ListViewItem("", "itemImageKey" & i)
    listItem.UseItemStyleForSubItems = False
    listItem.Tag = arrX(i)
    lstViewAH.Items.Add(listItem)
Next

有什么想法吗?

使用 FlowLayoutPanel 的简单方法是:

    FlowLayoutPanel1.SuspendLayout()
    FlowLayoutPanel1.AutoScroll = False
    For i As Integer = 0 To arrX.Count - 1
        Dim newImage As New DoubleBufferImage
        newImage.Load(CharactersFolder & "000" & arrX(i).ToString() & ".jpg")
        newImage.Tag = arrX(i).ToString()
        newImage.Width = 96
        newImage.Height = 96
        FlowLayoutPanel1.Controls.Add(newImage)
    Next
    FlowLayoutPanel1.AutoScroll = True
    FlowLayoutPanel1.ResumeLayout()

您也可以使用 AddRange 加载图像,但我假设保存数组会占用更多内存。速度非常快,所以这不是问题。我禁用了 AutoScroll,因为 autoscroll 需要时间重新计算并在之后启用它。似乎运行良好并且具有我想要的水平滚动条。

注意:在调试模式下添加大量控件很慢。 VS中的一个non-debug/release模式的EXE就不是这样了。