listview tile layout问题(vb.net)

时间:2010-05-02 15:51:47

标签: vb.net listview

我有一个listview,它显示(最终)一个itunes播放列表的专辑封面,其下面有专辑名称。我遇到的问题是我无法获得专辑封面上的专辑封面(目前是空白方块)。它总是站在一边......我该怎么做?我已经尝试添加列标题和alsorts ......

设置列表视图的代码

    Dim myImageList As ImageList

    albumList.View = View.Tile
    albumList.TileSize = New Size(120, 150)

    ' Initialize the item icons. 
    myImageList = New ImageList()

    myImageList.Images.Add(Image.FromFile("c:/test.jpg"))
    myImageList.ImageSize = New Size(80, 80)
    albumList.LargeImageList = myImageList

然后我循环显示使用

的每个专辑名称
        Dim item0 As New ListViewItem(New String() _
                {Albums(i).Name}, 0)

        albumList.Items.Add(item0)

输出为http://i111.photobucket.com/albums/n122/mfacer/Screenshot2010-05-02at164815.png

但正如我所说,我希望专辑名称位于橙色框下....

任何想法? 感谢您的任何信息!

1 个答案:

答案 0 :(得分:7)

这是瓷砖视图的烘焙安排。如果您想要图像下方的标签,那么您来设置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