如何调整列表视图中图像之间的间距

发布于 2024-12-22 13:05:16 字数 1608 浏览 0 评论 0原文

我正在使用图像列表在列表视图中显示图像。到目前为止,我能够显示图像列表中的所有图像,但每个图像之间的间距非常大。所以我使用了 发送消息方法< /a> 这引起了另一个问题。现在,当我单击或将鼠标移到(启用热跟踪)任何图像时,图像将变得不可见。我该如何解决这个问题?

Imports System.Runtime.InteropServices

Public Class Form1

    <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 Int32) As Int32
    End Function

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

    Public Sub SetSpacing(ByVal x As Int16, ByVal y As Int16)
        SendMessage(Me.ListView1.Handle, LVM_SETICONSPACING, 0, x * 65536 + y)
        Me.ListView1.Refresh()
    End Sub


    Private Sub Display()
        For i As Integer = 0 To ImageList1.Images.Count - 1
            Dim item As New ListViewItem()
            item.ImageIndex = i
            Me.ListView1.Items.Add(item)
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Display()
        SetSpacing(200, 16)
    End Sub

End Class

鼠标移动之前:

在此处输入图像描述

鼠标移动之后:

在此处输入图像描述

尽管列表视图边距设置为 All = 3,但我的左边距也很大

图像的第一列未显示! !

在此处输入图像描述

I am using a image-list to show images in a list-view. So far I am able to display all the images in the image-list but spacing between each image is very big. So I used the Send Message method which gave rise to another problem. Now when I click or move my mouse over(hot tracking enabled) any image the image becomes invisible. How can I solve this problem ?

Imports System.Runtime.InteropServices

Public Class Form1

    <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 Int32) As Int32
    End Function

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

    Public Sub SetSpacing(ByVal x As Int16, ByVal y As Int16)
        SendMessage(Me.ListView1.Handle, LVM_SETICONSPACING, 0, x * 65536 + y)
        Me.ListView1.Refresh()
    End Sub


    Private Sub Display()
        For i As Integer = 0 To ImageList1.Images.Count - 1
            Dim item As New ListViewItem()
            item.ImageIndex = i
            Me.ListView1.Items.Add(item)
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Display()
        SetSpacing(200, 16)
    End Sub

End Class

Before Mouse-Move:

enter image description here

After Mouse-Move:

enter image description here

Also I have this big left margin although listview margin is set to All = 3

The First column of Images is not being displayed !!!

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水溶 2024-12-29 13:05:16

在 x 和 y 参数中,必须包含图标的宽度和高度。

MSDN (http://msdn .microsoft.com/en-us/library/windows/desktop/bb761176(v=vs.85).aspx):

lParam 的值相对于图标的左上角
位图。因此,要设置不重叠的图标之间的间距,
lParam 值必须包括图标的大小以及金额
图标之间所需的空白空间。不包括的值
图标的宽度会导致重叠。

并且您需要反转:

x * 65536 + y -> x + y * 65536

y 在 HIWORD 中,x 在 LOWORD 中

In your x and y arguments, you have to include the width and the height of the icons.

MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/bb761176(v=vs.85).aspx):

Values for lParam are relative to the upper-left corner of an icon
bitmap. Therefore, to set spacing between icons that do not overlap,
the lParam values must include the size of the icon, plus the amount
of empty space desired between icons. Values that do not include the
width of the icon will result in overlaps.

And you need to invert:

x * 65536 + y -> x + y * 65536

y is in the HIWORD, x is in the LOWORD

橘香 2024-12-29 13:05:16

这是我与 LargeIcon 一起使用并起作用的东西,从这里开始:
https://qdevblog.blogspot.com/2011/11/ c-listview-item-spacing.html

using System.Runtime.InteropServices;

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

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));      
}

然后你像这样使用它:

ListViewItem_SetSpacing(this.listView1, 96 + 16, 96 + 16); // width image + H spacing, height image + V spacing

This is something that I used with LargeIcon and works, from here:
https://qdevblog.blogspot.com/2011/11/c-listview-item-spacing.html

using System.Runtime.InteropServices;

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

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));      
}

Then you use it like this:

ListViewItem_SetSpacing(this.listView1, 96 + 16, 96 + 16); // width image + H spacing, height image + V spacing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文