c# - 将图像绘制到 ListView 中的子项

标签 c# winforms listview

我的 ListView 在详细 View 中设置,具有以下列标题:

图片名称||图片位置 ||图片大小 ||图片预览

我想知道是否有办法在第 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);

问题是我必须自己处理所有 ListView 绘图... 我想知道是否有更好的方法将图像绘制到子项中,或者是否有一种方法只处理 DrawSubItem 事件?

最佳答案

我刚遇到这个。

@zidane's answer几乎是正确的。我想发布实际需要做的事情,以便稍后阅读的人不必自己弄清楚。


仅使用 e.DrawDefault = true; 和子项绘图处理 DrawColumnHeader。事实上,如果您在 DrawItem 事件中设置 e.DrawDefault = true;DrawSubItem 事件永远不会触发,大概是假设您想绘制整行而不关心子项。

唯一真正的代码在 DrawSubItem 中,使用这个基本结构:

if (/* condition to determine if you want to draw this subitem */)
{
    // Draw it
}
else
    e.DrawDefault = true;

关于c# - 将图像绘制到 ListView 中的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1050185/

相关文章:

java - Chris Banes实现ActionBar-PulltoRefresh,库错误等

c# - 在c# 3.0 中,是否可以将隐式运算符添加到字符串类中?

c# - 将 XML 数据类型与 LINQ to SQL 结合使用

c# - 在 getAsync 中为客户端添加 header (在 using block 中)

winforms - 在 PowerShell 中过滤 TreeView 节点

C# anchor 属性似乎不起作用

c# - 为新的自定义控件扩展什么?

android - 在 ListView 中看不到 TextView

c# - 内存泄漏和局部变量

c++ - 设置另一个程序的 ListView 选择项