C# 将图像绘制到 ListView 中的子项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1050185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing an Image to a subItem in the ListView
提问by
My Listview is setup in the details view with the following column headers:
我的 Listview 是在详细信息视图中设置的,带有以下列标题:
Image Name || Image Location || Image Size || Image Preview
图片名称 || 图片位置 || 图片尺寸 || 图像预览
I would like to know if there is a way to draw an image in the 4th column there. The only way I know, is to set
我想知道是否有办法在第 4 列中绘制图像。我知道的唯一方法是设置
this.listview1.OwnerDraw = true
this.listView1.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
this.listView1.DrawItem += new System.Windows.Forms.DrawListViewItemEventHandler(listView1_DrawItem);
this.listView1.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(listView1_DrawSubItem);
The problem with this is I have to handle ALL the listview drawing myself... I was wondering if there is a better way to draw in image to a subItem, or if there is a way to only handle the DrawSubItem event?
问题是我必须自己处理所有的列表视图绘图......我想知道是否有更好的方法将图像绘制到子项,或者是否有办法只处理 DrawSubItem 事件?
采纳答案by zidane
In listView1_DrawColumnHeader
and listView1_DrawItem
event handlers you should put this
在listView1_DrawColumnHeader
和listView1_DrawItem
事件处理程序中,你应该把这个
e.DrawDefault = true;
It will use default drawing implementation for columns and items, all you have to do is write your own implementation only for subitems.
它将为列和项目使用默认绘图实现,您所要做的就是仅为子项目编写自己的实现。
回答by Grammarian
ObjectListView(an open source wrapper around a .NET WinForms ListView) trivially supports drawing images in columns without having to do all the owner drawing yourself. The Data tab of the demo shows animated GIFs, which is a bit OTT but it will work equally well with static images.
ObjectListView(一个围绕 .NET WinForms ListView 的开源包装器)简单地支持在列中绘制图像,而不必自己绘制所有所有者。演示的“数据”选项卡显示动画 GIF,这有点 OTT,但它对静态图像同样有效。
With a few lines of code, this is what your ListView can look like:
(source: sourceforge.net)
用几行代码,这就是你的 ListView 的样子:(
来源:sourceforge.net)
回答by Jon Seigel
I just ran into this.
我刚遇到这个。
@zidane's answeris nearly correct. I want to post what actually needs to be done so people reading back later don't have to figure this out themselves.
@zidane 的回答几乎是正确的。我想发布实际需要做的事情,以便以后阅读的人不必自己弄清楚。
Only handle DrawColumnHeader
using e.DrawDefault = true;
and the subitem drawing. In fact, if you set e.DrawDefault = true;
in the DrawItem
event, the DrawSubItem
event never fires, presumably on the assumption that you want to draw the whole row and don't care about the subitems.
只处理DrawColumnHeader
使用e.DrawDefault = true;
和子项绘图。事实上,如果您e.DrawDefault = true;
在DrawItem
事件中设置,则DrawSubItem
事件永远不会触发,大概是基于您想要绘制整行而不关心子项的假设。
The only real code is in DrawSubItem
, using this basic construction:
唯一真正的代码在 中DrawSubItem
,使用以下基本结构:
if (/* condition to determine if you want to draw this subitem */)
{
// Draw it
}
else
e.DrawDefault = true;
回答by CrazyTim
Building upon previous answers, here's a full VB.NET example:
基于以前的答案,这是一个完整的 VB.NET 示例:
Public Class MyListView : Inherits System.Windows.Forms.ListView
Public Sub New()
MyBase.New()
MyBase.OwnerDraw = True
End Sub
Protected Overrides Sub OnDrawSubItem(ByVal e As DrawListViewSubItemEventArgs)
If x Then ' condition to determine if you want to draw this subitem
' draw code goes here
Else
e.DrawDefault = True
End If
MyBase.OnDrawSubItem(e)
End Sub
Protected Overrides Sub OnDrawColumnHeader(ByVal e As DrawListViewColumnHeaderEventArgs)
e.DrawDefault = True
MyBase.OnDrawColumnHeader(e)
End Sub
Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawListViewItemEventArgs)
e.DrawDefault = True
MyBase.OnDrawItem(e)
End Sub
End Class