How to change listview header's forecolor? C# windows form application

前端 未结 1 1771
粉色の甜心
粉色の甜心 2021-01-20 16:54

I know I can change the OwnerDraw property to true and then handle the DrawColumnHeader event but if I do it like this, I have to take

相关标签:
1条回答
  • 2021-01-20 17:03

    How about this:

    Create a new WinForm project, drag a ListView control onto the form, set OwnerDraw = true, View = Details in the Properties pane, then handle the DrawColumnHeader event.

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                this.SetLastColumnWidth();
    
                this.theListView.Layout += delegate
                {
                    this.SetLastColumnWidth();
                };
            }
    
            private void SetLastColumnWidth()
            {
                // Force the last ListView column width to occupy all the
                // available space.
                this.theListView.Columns[ this.theListView.Columns.Count - 1 ].Width = -2;
            }
    
            private void listView1_DrawColumnHeader( object sender, DrawListViewColumnHeaderEventArgs e )
            {
                // Fill header background with solid yello color.
                e.Graphics.FillRectangle( Brushes.Yellow, e.Bounds );
                // Let ListView draw everything else.
                e.DrawText();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题