ListView OwnerDraw的默认实现

我有一个ListView ,我希望调整项目的绘图(例如突出显示列表视图中的某些字符串),但我不想从根本上改变项目的显示方式。

我已将OwnerDraw设置为true,并且可以了解如何绘制我的突出显示效果,但是每当我尝试按照默认实现绘制列表视图项的其余部分时,事情就会出错并且我会留下一个整体加载图形问题,表明实际上我已经完全出错了。

有什么地方我可以看到DrawItemDrawSubItem事件的“默认”处理程序,以便我可以更好地理解并更容易调整我的代码吗?

这里有一个片段显示我目前正在做的事情:

 public MyListView() { this.OwnerDraw = true; this.DoubleBuffered = true; this.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(MyListView_DrawColumnHeader); this.DrawItem += new DrawListViewItemEventHandler(MyListView_DrawItem); this.DrawSubItem += new DrawListViewSubItemEventHandler(MyListView_DrawSubItem); } private void MyListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { // Not interested in changing the way columns are drawn - this works fine e.DrawDefault = true; } private void MyListView_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawBackground(); e.DrawFocusRectangle(); } private void MyListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { string searchTerm = "Term"; int index = e.SubItem.Text.IndexOf(searchTerm); if (index >= 0) { string sBefore = e.SubItem.Text.Substring(0, index); Size bounds = new Size(e.Bounds.Width, e.Bounds.Height); Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, this.Font, bounds); Size s2 = TextRenderer.MeasureText(e.Graphics, searchTerm, this.Font, bounds); Rectangle rect = new Rectangle(e.Bounds.X + s1.Width, e.Bounds.Y, s2.Width, e.Bounds.Height); e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), rect); } e.DrawText(); } 

我现在没有时间写一个完整的答案,所以我会放下一些快速的笔记,稍后再回过头来。

正如LarsTech所说,绘制ListView控件的所有者很痛苦 – .Net ListView类是底层Win32列表视图控件的包装器,并且NM_CUSTOMDRAW通知代码提供了“所有者绘制”的能力。 因此没有“默认的.Net实现” – 默认是使用底层的Win32控件。

为了让生活更加困难,还需要做出一些额外的考虑:

  • 正如LarsTech指出的那样,第一个子项实际上代表了父项本身,因此如果你在DrawItemDrawSubItem处理渲染,你可能会两次绘制第一个单元格的内容。
  • 底层列表视图控件中存在一个错误(在此页面上的注释中记录),这意味着DrawItem事件将在没有相应的DrawSubItem事件的情况下发生,这意味着如果您在DrawItem事件中绘制背景,然后在当鼠标hover时, DrawSubItem事件将使项目文本消失。
  • 默认情况下,某些渲染似乎也不是双缓冲的
  • 我还注意到ItemState属性并不总是正确的,例如在调整列大小之后。 因此,我发现最好不要依赖它。
  • 您还需要确保文本不会分割成多行,否则您将看到下面一行的前几个像素呈现在单元格的底部。
  • 在渲染第一个单元格时,还需要特别考虑,以考虑本机列表视图使用的额外填充。
  • 因为DrawItem事件首先发生,所以你在DrawItem处理程序中绘制的任何东西(例如选择效果)都可能被你在DrawSubItem处理程序中所做的事情所覆盖(例如,某些单元格具有不同的背景颜色)。

总而言之,处理所有者绘图是一个相当DrawSubItem事情 – 我发现最好处理DrawSubItem事件中的所有绘图,它也最好通过使用BufferedGraphics类来执行自己的双缓冲。

我还发现查看ObjectListView的源代码非常方便。

最后,所有这些只是为了处理列表视图的细节模式(我正在使用的唯一模式),如果你想让其他模式也工作,那么我相信还有额外的事情需要考虑。

当我有机会时,我会尝试发布我的工作示例代码。

我不知道这是否会对你有所帮助,但我会补充几点说明:

要记住的一件事是, DrawSubItem也将绘制第一个项目,这可能是你获得double-rendered外观的地方。

有些事要尝试(不考虑速度):

 private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawBackground(); if ((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected) { Rectangle r = new Rectangle(e.Bounds.Left + 4, e.Bounds.Top, TextRenderer.MeasureText(e.Item.Text, e.Item.Font).Width, e.Bounds.Height); e.Graphics.FillRectangle(SystemBrushes.Highlight, r); e.Item.ForeColor = SystemColors.HighlightText; } else { e.Item.ForeColor = SystemColors.WindowText; } e.DrawText(); e.DrawFocusRectangle(); } 

对于DrawSubItem例程,请确保您没有在第一列中绘图,并添加了DrawBackground()例程。 我在高亮矩形中添加了一些剪裁,因此它不会在列参数之外绘制。

 private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { if (e.ColumnIndex > 0) { e.DrawBackground(); string searchTerm = "Term"; int index = e.SubItem.Text.IndexOf(searchTerm); if (index >= 0) { string sBefore = e.SubItem.Text.Substring(0, index); Size bounds = new Size(e.Bounds.Width, e.Bounds.Height); Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, this.Font, bounds); Size s2 = TextRenderer.MeasureText(e.Graphics, searchTerm, this.Font, bounds); Rectangle rect = new Rectangle(e.Bounds.X + s1.Width, e.Bounds.Y, s2.Width, e.Bounds.Height); e.Graphics.SetClip(e.Bounds); e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), rect); e.Graphics.ResetClip(); } e.DrawText(); } } 

通常,绘制ListView控件的所有者欢迎受到伤害。 你不再使用视觉样式了,你也必须自己动手。 啊。

项目选择背面颜色已更改。 在Windows中默认为蓝色。 此代码将以任何颜色为您提供帮助:

  private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawBackground(); if (e.Item.Selected) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(250, 194, 87)), e.Bounds); } e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), new SolidBrush(Color.Black), e.Bounds); } private void Form1_Load(object sender, EventArgs e) { for (int ix = 0; ix < listView1.Items.Count; ++ix) { var item = listView1.Items[ix]; item.BackColor = (ix % 2 == 0) ? Color.Gray : Color.LightGray; } } private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = true; } } } 

ComponentOwl最近发布了名为Better ListView Express的免费软件组件 。

它的外观和行为与ListView完全相同,但具有更强大的所有者绘图function – 您可以精确地绘制所有元素,甚至可以关闭一些绘图(例如选择以使您开启)。

  private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawBackground(); if (e.Item.Selected) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(250, 194, 87)), e.Bounds); } e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), new SolidBrush(Color.Black), e.Bounds); }