winform ListView control to draw item and subitems color

Set the color of the ListView control

Are summarized as follows:

The listview of OwnerDraw  property is set to true and the  View  property is set to  View. Details  , it will trigger

ListViewDrawColumnHeader events (including event ListView DrawItem, ListView DrawSubItem event) Videos own background color, the following code can be separately three column headers (the ColumnHeader) redraw, Item and SubItem not redrawn.

  1.  
    private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
  2.  
    {
  3.  
    if (e.ColumnIndex == 0)
  4.  
    {
  5.  
    e.Graphics.FillRectangle (Brushes.DarkGray, e.Bounds); // using a specific color to draw the title bar, where I used gray
  6.  
    e.DrawText (); // default way to draw the title text
  7.  
    }
  8.  
     
  9.  
    else if (e.ColumnIndex == 1)
  10.  
    {
  11.  
    e.Graphics.FillRectangle (Brushes.DarkGray, e.Bounds); // using a specific color to draw the title bar, where I used gray
  12.  
    e.DrawText (); // default way to draw the title text
  13.  
    }
  14.  
     
  15.  
    else if (e.ColumnIndex == 2)
  16.  
    {
  17.  
    e.Graphics.FillRectangle (Brushes.DarkGray, e.Bounds); // using a specific color to draw the title bar, where I used gray
  18.  
    e.DrawText (); // default way to draw the title text
  19.  
    }
  20.  
    }
  21.  
     
  22.  
    private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
  23.  
    {
  24.  
    = e.DrawDefault to true; // draw the items using the system default
  25.  
    }
  26.  
     
  27.  
    private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
  28.  
    {
  29.  
    = e.DrawDefault to true; // draw the items using the system default
  30.  
    }

For SubItem of Item and repaint, see the code below

  1.  
    private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
  2.  
    {
  3.  
    e.Graphics.FillRectangle (Brushes.Red, e.Bounds); // using a specific color to draw the title bar, where I used red
  4.  
    e.DrawText (); // default way to draw the title text
  5.  
    }
  6.  
     
  7.  
    private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
  8.  
    {
  9.  
    e.Graphics.FillRectangle (Brushes.Red, e.Bounds); // using a specific color to draw the title bar, where I used red
  10.  
    e.DrawText (); // default way to draw the title text
  11.  
    }

Header row centered in the listview OwnerDraw  the property set to true, the alignment of the title bar of the code would take effect;

If the listview OwnerDraw  property is set to false, the alignment of the title bar code will not change (always aligned to the left).

With particular reference to the following code:

  1.  
    = this.listView1.OwnerDraw to true; // allow self-drawn.
  2.  
                
  3.  
    ColumnHeader ch = new ColumnHeader();
  4.  
    = ch.Text "1 column heading";   // Set the column headings
  5.  
    = ch.Width 120;     // set the column width
  6.  
    = HorizontalAlignment.Center ch.TextAlign; // set the alignment of columns, this.listView1.OwnerDraw = true effective.
  7.  
    this.listView1.Columns.Add (CH);     // column header to the ListView control.

 

Guess you like

Origin www.cnblogs.com/baylor2019/p/11910187.html