Question

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 care of everything in drawing the header.

Is there anyway that I just change the foreground color and everything else is drawn with defaults?

Was it helpful?

Solution

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();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top