【问题标题】:How can I get the height of a ListView item?如何获取 ListView 项的高度?
【发布时间】:2011-10-06 01:39:31
【问题描述】:

我有 ListView,我需要确定项目高度。

【问题讨论】:

    标签: c# .net winforms listview


    【解决方案1】:

    您可以使用GetItemRect() 方法:

    int itemHeight = yourListView.GetItemRect(itemIndex).Height;
    

    【讨论】:

      【解决方案2】:

      我不是 100% 确定,但这可能会有所帮助:

      int itemHeight = listView.Items[itemIndex].GetBounds(ItemBoundsPortion.Entire).Height;
      

      【讨论】:

        【解决方案3】:

        我有同样的问题,但是有一个问题 - 在绘制列表视图之前,未设置值。而且您可能希望能够在添加任何项目之前设置大小(例如,如果我想干燥一个可以显示 5 个条目但开始为空的列表视图)。

        因此,我的解决方法是在应用程序的初始化部分运行以下代码,强制呈现控件但不显示它,并将值保存为全局变量以供以后使用。而且,要绕过具有不同字体大小的列表视图,只存储高度和字体高度之间的差异:

        Dim lvwTemp As New ListView
        lvwTemp.View = View.Details
        lvwTemp.Columns.Add("test")
        lvwTemp.Items.Add("test")
        Dim zTempBitmap As New Bitmap(100, 100)
        lvwTemp.DrawToBitmap(zTempBitmap, New Rectangle(0, 0, 100, 100))
        zTempBitmap.Dispose()
        gintListviewHeaderHeightMinusFontSize = lvwTemp.Items(0).GetBounds(ItemBoundsPortion.Entire).Top - lvwTemp.Font.Height
        gintListviewItemHeightMinusFontSize = lvwTemp.Items(0).GetBounds(ItemBoundsPortion.Entire).Height - lvwTemp.Font.Height
        

        【讨论】:

        • 这让你很容易得到列标题高度和项目高度——很好!
        猜你喜欢
        • 2011-03-22
        • 2010-10-07
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        相关资源
        最近更新 更多