2009-10-22 148 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;