Custom ListView in Winforms?

折月煮酒 提交于 2019-12-28 16:05:31

问题


Is it possible to draw some strings onto a listview?

I overridden the OnPaint event but I don't see any change. I checked out some code on custom listview, but it seems like people are using p/invoke, etc. Why?

Isn't list as customizable as other winforms, like the Button control?

I am not gonna customize wildly, just paint some more after it's done the standard painting.


回答1:


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

    }



回答2:


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;



回答3:


Set the OwnerDraw property to true.

You can then handle the DrawItem, DrawSubItem, and DrawColumnHeader events to draw on specific elements of the ListView.



来源:https://stackoverflow.com/questions/1604704/custom-listview-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!