【问题标题】:Images in a ListView with text below themListView 中的图像及其下方的文本
【发布时间】:2014-11-22 13:07:00
【问题描述】:

我正在 C# 中创建一个 2D 地图编辑器,并且我一直在尝试在 ListView 中显示对象,所以它看起来像这个示例(我不确定示例中的控件是否为 ListView . 可能是别的东西):

但我似乎无法到达那里!我正在使用带有 imageList 的 ListViewlistView.ViewTile,我正在设置 ListViewItem 的文本。

看起来就是这样(我添加了一项)

我找不到将项目文本放在图像下方而不是旁边的方法,就像在示例中一样。

有什么想法可以做到这一点吗?

【问题讨论】:

标签: c# listview imagelist


【解决方案1】:

TaW 的回答当然是正确的,但是 View=LargeIcon 的问题是项目之间的间距太大,我一开始想使用 View=Tile,因为项目之间没有间距。所以,在我对此进行了一些研究之后,我想出了一个间距解决方案,我对它进行了一些修改。

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    private static extern Int32 SendMessage(IntPtr hwnd, Int32 wMsg, Int32 wParam, Int32 lParam);

    const int LVM_FIRST = 0x1000;
    const int LVM_SETICONSPACING = LVM_FIRST + 53;

    public void SetControlSpacing(Control control, Int16 x, Int16 y)
    {
        SendMessage(control.Handle, LVM_SETICONSPACING, 0, x * 65536 + y);
        control.Refresh();
    }

并且由于列表控件的默认间距似乎是 100 (x)、100 (y),将每个间距更改为 75 即可解决问题,

listView.View = View.largeIcon;
SetControlSpacing(listView, 75, 75);

现在看起来像这样:

感谢您的帮助。

【讨论】:

    【解决方案2】:

    最简单的解决方案是选择View=LargeIcon。它确实确实将文本的位置更改为低于 LargeImage。

    View=Tiles 的解决方案是所有者绘制 ListView:

    private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
    {
        e.DrawBackground();
    
        ImageList iList = listView1.LargeImageList;
        Size iSize = iList.ImageSize;
        int fSize2 = 7;
    
        Rectangle R0 = new Rectangle(Point.Empty, iSize);
        Rectangle R1 = new Rectangle(new Point(e.Bounds.X , e.Bounds.Y ),
                        new Size(iSize.Width - fSize2, iSize.Height - fSize2) );
    
        e.Graphics.DrawImage(iList.Images[e.Item.ImageIndex], R1, R0, GraphicsUnit.Pixel);
    
        e.Graphics.DrawString(e.Item.Text, Font, Brushes.Black, 
                              2f, e.Bounds.Y + iSize.Height - fSize2);
    
    }
    

    请注意,ItemHeight 始终受到所选ImageListImage.Height 的限制,因此我将图像缩小一点,以便为下面的文本腾出空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2018-02-22
      • 2017-02-22
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多