ListView的默认实现OwnerDraw

问题描述:

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

我已经将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(); 
} 
+0

我想你需要添加关于什么错误的详细信息。我测试了你的代码,它是......好的。它用黄色框突出了“Term”一词。也许显示绘图结果的图像。 – LarsTech 2012-01-30 16:01:17

+0

@LarsTech我遇到了很多问题,从选择效果不再可见(可以在上面的示例中看到)到项目文本被“双重渲染”,并且当我将鼠标悬停在项目上时也消失。我不希望有人能够神奇地修复所有这些问题(这些问题也会过于具体以至于不能帮助未来的用户),相反我希望看到一个能够尽可能接近地呈现所有内容的示例实现。标准实施 – Justin 2012-01-30 16:22:08

我现在还没有时间写出完整的答案,所以我会写下一些快速记录,稍后再回来。

正如LarsTech说,使用绘制ListView控制是一种痛苦 - 。净ListView类是底层Win32 List View Control周围的包装和“所有者绘制”的能力是由NM_CUSTOMDRAW notification code提供。因此没有“默认的.Net实现” - 默认是使用底层的Win32控件。

为了让生活更加困难也有一些额外的因素使:

  • 作为LarsTech指出,其实第一个子项表示父项目本身,所以如果你在两个把手渲染DrawItemDrawSubItem你可能会画第一个单元格的内容两次。如果在DrawItem事件中绘制背景,然后绘制文本在DrawSubItem事件中,当您将鼠标悬停时,项目文本将消失。
  • 某些渲染似乎并不是默认的双缓冲
  • 我还注意到ItemState属性并不总是正确的,例如在调整列大小之后。因此,我发现最好不要依赖它。
  • 您还需要确保您的文本不会分割成多行,否则您会在单元格底部看到较低行的前几个像素。
  • 在渲染第一个单元格时需要特别考虑本地列表视图使用的额外填充。
  • 因为DrawItem事件首先发生,所以您在DrawItem处理程序中绘制的任何内容(例如选择效果)都可能被您在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控件在受到伤害的世界中是欢迎的。你不再使用视觉样式,你也必须自己去做。啊。

ComponentOwl最近发布了freeware component called Better ListView Express

它的外观和行为与ListView完全相同,但具有much more powerful owner drawing的功能 - 您可以准确地绘制所有元素,甚至关闭某些绘图(例如选择以使您开启)。

选择的项目背面颜色已更改。在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; 
    } 

    } 
} 

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

    }