13

我有一个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();
}
4

5 回答 5

16

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

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

为了使生活更加困难,需要考虑许多额外的因素:

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

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

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

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

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

于 2012-02-01T13:41:00.893 回答
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();
}

对于您的 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 控件的所有者在一个受伤害的世界中是受欢迎的。您不再使用视觉样式绘图,您也必须自己做。啊。

于 2012-01-30T17:14:20.087 回答
2

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

    }
}
于 2013-11-05T09:49:16.447 回答
0

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

它的外观和行为与 ListView 完全相同,但拥有更强大的所有者绘图功能 - 您可以准确地在所有元素上绘制,甚至可以关闭某些绘图(例如,让您打开的选择)。

于 2012-02-01T14:52:54.307 回答
0
  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); 

  }
于 2013-11-05T09:52:36.213 回答