2013-07-31 50 views
2

如何更改ListView標題顏色? 如果您知道如何操作,請給出一個代碼示例。更改列表視圖標題的顏色?

private System.Windows.Forms.ListView lvFiles; 
+1

這可能有所幫助:http://stackoverflow.com/questions/8818224/how-to-change-listview-headers-forecolor-c-sharp-windows-form-application –

回答

4

這不是一個很好的問題試圖要求我們這樣做,但這裏是一個代碼示例。

ListView lvFiles= new ListView(); 
public Form1() 
{ 
    InitializeComponent(); 

    lvFiles.View = View.Details; 
    lvFiles.OwnerDraw = true; 
    lvFiles.Size = new Size(200, 100); 

    lvFiles.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(lv_DrawColumn Header); 
    lvFiles.DrawSubItem += new DrawListViewSubItemEventHandler(lv_DrawSubItem); 

    lv.Columns.Add("Col1"); 
    lv.Columns.Add("Col1"); 
    lv.Columns.Add("Col1"); 

    this.Controls.Add(lv); 

    lv.Items.Add(new ListViewItem(new string[] { "One", "Two","Three" })); 
    lv.Items.Add(new ListViewItem(new string[] { "One", "Two","Three" })); 
    lv.Items.Add(new ListViewItem(new string[] { "One", "Two","Three" })); 
} 

void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgse) 
{ 
    if ((e.ItemState & ListViewItemStates.Focused) 0) 
    { 
     e.Graphics.FillRectangle(SystemBrushes.Highlight,e.Bounds); 
     e.Graphics.DrawString(e.Item.Text, lv.Font,SystemBrushes.HighlightText, e.Bounds); 
    } 
    else 
    { 
     e.DrawBackground(); 
     e.DrawText(); 
    } 
} 

void lv_DrawColumnHeader(object sender,DrawListViewColumnHeaderEventArgs e) 
{ 
    e.Graphics.FillRectangle(Brushes.GreenYellow, e.Bounds); 
    e.DrawText(); 
}