Default implementation for ListView OwnerDraw

后端 未结 5 1107
萌比男神i
萌比男神i 2020-12-10 04:18

I have a ListView where I wish to tweak the drawing of items (for example highlighting certain strings in list view itmes), however I don\'t want to radically alter the way

5条回答
  •  攒了一身酷
    2020-12-10 05:07

    Item selected back color changed. In by default blue in windows. This code will help for u in any colors:

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawBackground(); 
             if (e.Item.Selected) 
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(250, 194, 87)), e.Bounds); 
            } 
            e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), new SolidBrush(Color.Black), e.Bounds); 
    
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            for (int ix = 0; ix < listView1.Items.Count; ++ix)
            {
                var item = listView1.Items[ix];
                item.BackColor = (ix % 2 == 0) ? Color.Gray : Color.LightGray;
    
            }
    
        }
    
        private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }
    
        }
    }
    

提交回复
热议问题