Winforms中的自定义ListView?
是否可以在列表视图上绘制一些字符串?
我重写了OnPaint事件,但我没有看到任何变化。 我在自定义列表视图上检查了一些代码,但似乎人们正在使用p / invoke等等。为什么?
列表是否可以像其他winforms一样自定义,比如Button控件?
我不会疯狂地定制,只需在完成标准绘画后再画一些。
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)); } }
您不能只重写OnPaint()
方法。 该方法在ListView中不执行任何操作。 同样, OwnerDrawn
允许您自定义绘制每个单元格,但不允许您作为整体绘制控件。
使用ObjectListView (围绕.NET WinForms ListView的开源包装器)并使用其Overlayfunction 。 这让你毫不费力地做这样的事情:
ListView上的文本http://sofzh.miximages.com/c%23/29zwu1d.png
这是由以下代码生成的:
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;
将OwnerDraw属性设置为true。
然后,您可以处理DrawItem , DrawSubItem和DrawColumnHeader事件以在ListView的特定元素上绘制。