Csharp/C#教程:ListView OwnerDraw的默认实现分享


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控件。

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

总而言之,处理所有者绘图是一个相当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 – 您可以精确地绘制所有元素,甚至可以关闭一些绘图(例如选择以使您开启)。

上述就是C#学习教程:ListView OwnerDraw的默认实现分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

  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); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1007116.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐