ListView OwnerDraw的默认实现

Jus*_*tin 9 c# listview virtualmode winforms

我有一个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();
}
Run Code Online (Sandbox Code Playgroud)

Jus*_*tin 12

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

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

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

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

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

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

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

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


Lar*_*ech 5

我不知道这是否会完全帮助您,但我会添加一些注释:

要记住的一件事是,它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();
}
Run Code Online (Sandbox Code Playgroud)

对于您的 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

一般来说,绘制 ListView 控件的所有者在伤害世界中是受欢迎的。您不再使用 Visual Styles 绘图,您也必须自己这样做。啊。