Winforms中的自定义ListView?
题
是否可以在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的开源包装器)并使用其叠加功能。这让你毫不费力地做这样的事情:
这是由以下代码生成的:
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 财产到真。
然后,您可以处理 DrawItem , DrawSubItem ,并绘制 DrawColumnHeader 事件ListView的特定元素。
不隶属于 StackOverflow