2012-01-11 107 views
3

我知道我可以将OwnerDraw属性更改为true,然后处理DrawColumnHeader事件,但如果我这样做,我必须照顾绘制标题的所有内容。如何更改listview标头的前景色? C#窗体窗体应用程序

是否有无论如何,我只是改变前景色和其他一切都是用默认值绘制?

+5

不,这些标题是根据用户选择的视觉风格主题绘制的。在稍后的Windows版本上进行渐变填充时,它不仅仅是一种颜色。请问自己,是否重写用户的偏好*是你应该做的事情。是的,使用OwnerDraw强制您的偏好。 – 2012-01-11 13:13:21

回答

2

如何:

创建一个新的WinForm项目,将一个的ListView控件到窗体,设置的OwnerDraw =真查看=详情在属性面板中,然后办理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(); 
     } 
    } 
} 
相关问题