我知道我可以将OwnerDraw
属性更改为true
,然后处理DrawColumnHeader
事件,但是如果我这样做,我必须注意绘制标题时的所有内容。
有没有办法只更改前景色,其他所有内容都使用默认值进行绘制?
我知道我可以将OwnerDraw
属性更改为true
,然后处理DrawColumnHeader
事件,但是如果我这样做,我必须注意绘制标题时的所有内容。
有没有办法只更改前景色,其他所有内容都使用默认值进行绘制?
这样怎么样:
创建一个新的WinForm项目,在表单上拖放一个ListView控件,设置属性窗格中的OwnerDraw = true和View = 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();
}
}
}