我正在尝试更改 ListView 中选择栏的默认(蓝色)颜色。
我拒绝使用 ObjectListView,因为我必须更改所有代码。
我搜索了这个主题并在这里找到了一些答案:
Change background selection color of ListView?
但这指向 ObjectListView。
当我以前使用 ListBox 时,这可以根据我的喜好设置选择栏颜色:
将 DrawMode 设置为OwnerDrawFixed属性下
将 DrawItem 设置为ListBox1_DrawItem事件下
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
//if the item state is selected them change the back color
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State ^ DrawItemState.Selected,
e.ForeColor,
Color.FromArgb(43, 144, 188));//Choose the color
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Draw the current item text
e.Graphics.DrawString(lb_result.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
但我现在正在使用 ListView。
我设置OwnerDraw为真
我将 DrawItem 设置为ListView1_DrawItem
...并使用上面的代码。
我希望它能按照说明向我显示不同的选择颜色,但我却遇到了一些错误:
我将如何为 ListView 正确使用此代码?
慕运维8079593
相关分类