Custom ListView in Winforms?
You can't just override the OnPaint()
method. That method doesn't do anything in a ListView. Similarly, OwnerDrawn
lets you custom draw each cell, but doesn't let you paint over the control as a whole.
Use an ObjectListView (an open source wrapper around .NET WinForms ListView) and use its Overlay feature. That lets you effortlessly do something like this:
This was produced by this code:
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;
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));
}
}