Winforms中的自定义ListView?

Joa*_*nge 6 .net c# listview gdi+ winforms

是否可以在列表视图上绘制一些字符串?

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

列表是否可以像其他winforms一样自定义,比如Button控件?

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

Gra*_*ian 10

您不能只是覆盖该OnPaint()方法.该方法在ListView中不执行任何操作.同样,OwnerDrawn允许您自定义绘制每个单元格,但不允许您作为整体绘制控件.

使用ObjectListView(围绕.NET WinForms ListView的开源包装器)并使用其Overlay功能.这让你毫不费力地做这样的事情:

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;
Run Code Online (Sandbox Code Playgroud)


And*_*ena 6

 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));
        }

    }
Run Code Online (Sandbox Code Playgroud)

  • 这不适用于ListView。它只是使ListView不绘制任何内容。 (2认同)