2009-10-22 90 views
5

是否可以将一些字符串绘制到列表视图上?在Winforms中自定义ListView?

我重写了OnPaint事件,但我没有看到任何更改。我检查了自定义列表视图上的一些代码,但似乎人们正在使用p/invoke等。为什么?

是不是像其他winforms一样定制,就像按钮控件一样?

我不会疯狂定制,只需在完成标准绘画后再绘制一些。

回答

7
class MyCustomlistView : ListView 
    { 
     public MyCustomlistView() 
      : base() 
     { 
      SetStyle(ControlStyles.UserPaint, true); 
     } 
     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 
      e.Graphics.DrawString("This is a custom string", new Font(FontFamily.GenericSerif, 10, FontStyle.Bold), Brushes.Black, new PointF(0, 50)); 
     } 

    } 
+0

谢谢,这并获得成功。 – 2009-10-22 09:37:29

+0

这不适用于ListView。它只是简单地使ListView没有画任何东西。 – Grammarian 2009-10-26 17:30:55

8

您不能仅覆盖OnPaint()方法。该方法在ListView中不执行任何操作。同样,OwnerDrawn可让您自定义绘制每个单元格,但不会让您在整个控件上绘画。

使用ObjectListView(.NET WinForms ListView的开源包装器)并使用其Overlay feature。这可以让你毫不费力地做这样的事情:

text over a ListView http://i37.tinypic.com/29zwu1d.jpg

这是由这个代码生成:

this.olv1.OverlayText.Alignment = ContentAlignment.BottomRight; 
this.olv1.OverlayText.Text = "Trial version"; 
this.olv1.OverlayText.BackColor = Color.White; 
this.olv1.OverlayText.BorderWidth = 2.0f; 
this.olv1.OverlayText.BorderColor = Color.RoyalBlue; 
this.olv1.OverlayText.TextColor = Color.DarkBlue;