如何更改ListView头部的前景色?C# Windows窗体应用程序。

3

我知道我可以将OwnerDraw属性更改为true,然后处理DrawColumnHeader事件,但是如果我这样做,我必须注意绘制标题时的所有内容。

有没有办法只更改前景色,其他所有内容都使用默认值进行绘制?


5
不,这些标题根据用户选择的视觉样式主题绘制。较新的 Windows 版本使用渐变填充,而不仅仅是单一颜色。请问自己是否应该覆盖用户的偏好。如果是,请使用OwnerDraw来强制使用您的偏好。 - Hans Passant
1个回答

3

这样怎么样:

创建一个新的WinForm项目,在表单上拖放一个ListView控件,设置属性窗格中的OwnerDraw = trueView = Details,然后处理DrawColumnHeader事件。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接