如何更改 ListView 的默认选择颜色?

我正在尝试更改 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

...并使用上面的代码。


我希望它能按照说明向我显示不同的选择颜色,但我却遇到了一些错误:

http://img.mukewang.com/637c8cef00019a0d15330229.jpg

我将如何为 ListView 正确使用此代码?



慕田峪7331174
浏览 228回答 1
1回答

慕运维8079593

所有者绘制 ListView 控件比 ListBox 控件更复杂:需要处理更多细节。这是一个考虑ListView 的四个视图设置的示例:View.Details、View.List和。View.TileView.SmallIcon此处仅绘制文本(这View.LargeIcon就是不包括在内的原因),以将代码包含在合适的范围内。此处是绘制链接到 ListView 的 ImageList 中包含的位图的示例。设置 ListView:启用您的 ListViewOwnerDraw模式,然后订阅它的DrawItem、DrawSubItem和DrawColumnHeader事件,如示例代码所示(强制,如果您希望 ListView 显示任何内容)。使用默认渲染(设置e.DrawDefault = true)绘制标题。常用操作说明:使用TextRenderer.DrawText绘制 Item Text :这是 ListView 用于绘制其项目的原始方法。它允许完全匹配默认呈现,因此我们不会注意到文本的一些未对齐。该DrawItem事件用于在所有模式下绘制自定义背景,并将在除View.DetailsView之外的所有模式下绘制项目的文本,事件开始发挥作用:如果事件执行相同的任务,我们将绘制第一个项目的文本两次.DrawSubItemsDrawItem当设置为或时,不会调用该DrawSubItems事件。ViewTileList此处提供的代码的详细信息:辅助方法GetTextAlignment负责设置项目的对齐方式,因为每个列都可以具有特定的文本对齐方式。字段Color listViewSelectionColor用于设置/更改所选项目的颜色。要修改选择颜色,请将此字段设置为任何值,Invalidate()然后 ListView: 将立即应用新颜色。结果样本:https://i.stack.imgur.com/NTpWn.gif bool lvEditMode = false;Color listViewSelectionColor = Color.Orange;protected void listView1_DrawItem(object sender, DrawListViewItemEventArgs e){&nbsp; &nbsp; var lView = sender as ListView;&nbsp; &nbsp; if (lvEditMode || lView.View == View.Details) return;&nbsp; &nbsp; TextFormatFlags flags = GetTextAlignment(lView, 0);&nbsp; &nbsp; Color itemColor = e.Item.ForeColor;&nbsp; &nbsp; if (e.Item.Selected) {&nbsp; &nbsp; &nbsp; &nbsp; using (var bkBrush = new SolidBrush(listViewSelectionColor)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Graphics.FillRectangle(bkBrush, e.Bounds);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; itemColor = e.Item.BackColor;&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; e.DrawBackground();&nbsp; &nbsp; }&nbsp; &nbsp; TextRenderer.DrawText(e.Graphics, e.Item.Text, e.Item.Font, e.Bounds, itemColor, flags);&nbsp; &nbsp; if (lView.View == View.Tile && e.Item.SubItems.Count > 1) {&nbsp; &nbsp; &nbsp; &nbsp; var subItem = e.Item.SubItems[1];&nbsp; &nbsp; &nbsp; &nbsp; flags = GetTextAlignment(lView, 1);&nbsp; &nbsp; &nbsp; &nbsp; TextRenderer.DrawText(e.Graphics, subItem.Text, subItem.Font, e.Bounds, SystemColors.GrayText, flags);&nbsp; &nbsp; }}private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e){&nbsp; &nbsp; var lView = sender as ListView;&nbsp; &nbsp; TextFormatFlags flags = GetTextAlignment(lView, e.ColumnIndex);&nbsp; &nbsp; Color itemColor = e.Item.ForeColor;&nbsp; &nbsp; if (e.Item.Selected && !lvEditMode) {&nbsp; &nbsp; &nbsp; &nbsp; if (e.ColumnIndex == 0 || lView.FullRowSelect) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var bkgrBrush = new SolidBrush(listViewSelectionColor)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Graphics.FillRectangle(bkgrBrush, e.Bounds);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemColor = e.Item.BackColor;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; e.DrawBackground();&nbsp; &nbsp; }&nbsp; &nbsp; TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, itemColor, flags);}protected void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)&nbsp; &nbsp; => e.DrawDefault = true;private TextFormatFlags GetTextAlignment(ListView lstView, int colIndex){&nbsp; &nbsp; TextFormatFlags flags = (lstView.View == View.Tile)&nbsp; &nbsp; &nbsp; &nbsp; ? (colIndex == 0) ? TextFormatFlags.Default : TextFormatFlags.Bottom&nbsp; &nbsp; &nbsp; &nbsp; : TextFormatFlags.VerticalCenter;&nbsp; &nbsp; if (lstView.View == View.Details) flags |= TextFormatFlags.LeftAndRightPadding;&nbsp; &nbsp; if (lstView.Columns[colIndex].TextAlign != HorizontalAlignment.Left) {&nbsp; &nbsp; &nbsp; &nbsp; flags |= (TextFormatFlags)((int)lstView.Columns[colIndex].TextAlign ^ 3);&nbsp; &nbsp; }&nbsp; &nbsp; return flags;}private void listView1_BeforeLabelEdit(object sender, LabelEditEventArgs e) => lvEditMode = true;private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e) => lvEditMode = false;&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP