C# WinForm 控件美化之改变ListView Head 的背景色
方法1:(已测试)给ListView添加以下事件,改实例DataList为控件名称
private void DataList_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(Color.Black), e.Bounds);//设置背景颜色 e.Graphics.DrawString("最小值", this.DataList.Font, new SolidBrush(Color.White), 20, 0);//设置文本内容与颜色 e.Graphics.DrawString("最大值", this.DataList.Font, new SolidBrush(Color.White), 100, 0); e.Graphics.DrawString("平均值", this.DataList.Font, new SolidBrush(Color.White), 180, 0); e.DrawText();//进行描绘 } private void DataList_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawDefault = true; }
方法二:(未测试)与方法一相似,但添加事件不同
this.listView1.OwnerDraw = true; this.listView1.View = System.Windows.Forms.View.Details; this.listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(this.listView1_DrawColumnHeader); this.listView1.DrawSubItem += new DrawListViewSubItemEventHandler(this.listView1_DrawSubItem); private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = false; e.Graphics.FillRectangle(Brushes.GreenYellow, e.Bounds); e.DrawText(); } private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { if (e.ItemState == ListViewItemStates.Focused) { e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); e.Graphics.DrawString(e.Item.Text, this.listView1.Font, SystemBrushes.HighlightText, e.Bounds); } else { e.DrawBackground(); e.DrawText(); } }