【问题标题】:Default implementation for ListView OwnerDrawListView OwnerDraw 的默认实现
【发布时间】:2012-01-30 15:36:24
【问题描述】:

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

我已将 OwnerDraw 设置为 true,并且可以了解如何绘制突出显示效果,但是每当我尝试遵循默认实现来绘制列表视图项的其余部分时,都会出错,我我留下一大堆图形问题,表明实际上我完全出错了。

我是否可以在某个地方看到 DrawItemDrawSubItem 事件的“默认”处理程序的作用,以便我更好地理解并更轻松地调整我的代码?

作为参考,这里有一个显示我当前正在做的事情的 sn-p:

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 中的所有列的文本中加粗搜索词。
  • @Terry 抱歉,这是很久以前的事了,现在我不记得了

标签: c# winforms listview virtualmode


【解决方案1】:

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

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

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

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

总而言之,处理所有者绘图是一件相当复杂的事情 - 我发现最好在 DrawSubItem 事件中处理 all 绘图,最好使用BufferedGraphics类。

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

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

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

【讨论】:

    【解决方案2】:

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

    要记住的一点是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 控件的所有者很受欢迎。您不再使用视觉样式绘图,您也必须自己做。呃。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        【解决方案5】:
          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); 
        
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-10
          • 2011-04-29
          • 2012-05-16
          • 2014-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-18
          相关资源
          最近更新 更多