如何更改ListView的默认选择颜色? [英] How to change default selection color of a ListView?

查看:67
本文介绍了如何更改ListView的默认选择颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改ListView中选择栏的默认(蓝色)颜色.
我拒绝使用ObjectListView,因为我必须更改所有代码.

I'm trying to change the default (blue) color of selection bar in ListView.
I'm refusing to use ObjectListView since I'll have to change all the code.

我已经搜索了这个主题,并在这里找到了一些答案:
更改ListView的背景选择颜色?
但这指向了ObjectListView.

I've searched on this subject and found some answers here:
Change background selection color of ListView?
but that points to ObjectListView.

以前我使用ListBox时,这可以根据自己的喜好设置选择栏的颜色:

When I was using ListBox before, this worked to set the selection bar color to my likings:

  1. 在属性"下将DrawMode设置为OwnerDrawFixed
  2. 在事件"下将DrawItem设置为ListBox1_DrawItem
  1. Set DrawMode to OwnerDrawFixed under Properties
  2. Set DrawItem to ListBox1_DrawItem under Events


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.

But I'm now using ListView.

  1. 我将OwnerDraw设置为True
  2. 我将DrawItem设置为ListView1_DrawItem
  1. I set OwnerDraw to True
  2. I set DrawItem to ListView1_DrawItem

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

...and use the code from above.

我希望它能为我显示不同的选择颜色,但是却出现了一些错误:

I expected it to show me a different selection color as stated, but instead I get a few errors:

我如何在ListView中正确使用此代码?

How would I use this code correctly for a ListView?

推荐答案

所有者绘制ListView控件要比ListBox控件复杂:要处理的细节很多. 此示例考虑了四个查看 ListView的设置:
View.DetailsView.ListView.TileView.SmallIcon.

Owner-drawing a ListView control is more complicated than a ListBox control: many more details to take care of. This is an example that considers four View settings of a ListView:
View.Details, View.List, View.Tile and View.SmallIcon.

仅在此处绘制文本(这就是为什么不包括View.LargeIcon),以将代码包含在适当范围内的原因. 此处是绘制与链接到ListView的ImageList中包含的位图的示例.

Only the Text is drawn here (that's why View.LargeIcon is not included), to contain the code to a decent limit.
An example of drawing the Bitmaps included in a ImageList linked to the ListView is here.

设置ListView :
启用ListView OwnerDraw 模式,然后订阅其 DrawSubItem DrawColumnHeader 事件,如示例代码所示(强制,如果希望ListView显示任何内容).

Set up the ListView:
Enable your ListView OwnerDraw mode, then subscribe to its DrawItem, DrawSubItem and DrawColumnHeader events as shown in the sample code (mandatory, if you want the ListView to show anything).

标题是使用默认渲染(设置 e.DrawDefault = true )绘制的.

The Headers are painted using the default rendering (setting e.DrawDefault = true).

常用操作说明:
使用 TextRenderer.DrawText :这是ListView用来绘制其项目的原始方法.它允许精确匹配默认渲染,因此我们不会注意到文本的某些未对齐情况.

Description of common operations:
The Item Text is drawn using TextRenderer.DrawText: this is the original method used by the ListView to draw its items. It allows to match exactly the default rendering, so we won't notice some mis-alignment of the text.

DrawItem事件用于在所有 View 模式下绘制自定义背景,并将在除 View.Details (视图.详细信息)之外的所有模式下绘制项目的文本, DrawSubItems事件起作用的地方:如果DrawItem事件执行相同的任务,我们将绘制第一个项目的文本两次.

The DrawItem event is used to draw a custom background in all View modes and will draw the Items' text in all mode except View.Details, where the DrawSubItems event comes into play: we would draw the first Item's text twice, should the DrawItem event perform the same task.

View设置为TileList 时,不会调用DrawSubItems事件.

The DrawSubItems event is not called when the View is set to Tile or List.

此处显示的代码的详细信息:
辅助方法 GetTextAlignment 负责设置项目的对齐方式,因为每个列可以具有特定的文本对齐方式.

Details on the code presented here:
A helper method, GetTextAlignment, takes care of setting the Items' alignment, since each Column can have a specific text alignment.

Color listViewSelectionColor 字段用于设置/更改所选项目的颜色.要修改选择颜色,请将此字段设置为任何值,并 Invalidate() ListView:新的Color将立即应用.

A field, Color listViewSelectionColor, is used to set/change the Color of the select Items. To modify the selection color, set this filed to any value and Invalidate() the ListView: the new Color will be applied immediately.

结果样本:

Color listViewSelectionColor = Color.Orange;

protected void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    ListView lView = sender as ListView;
    if (lView.View == View.Details) return;
    TextFormatFlags flags = GetTextAlignment(lView, 0);
    Color itemColor = e.Item.ForeColor;
    if (e.Item.Selected) {
        using (SolidBrush bkgrBrush = new SolidBrush(listViewSelectionColor)) {
            e.Graphics.FillRectangle(bkgrBrush, e.Bounds);
        }
        itemColor = e.Item.BackColor;
    }
    else {
        e.DrawBackground();
    }
    TextRenderer.DrawText(e.Graphics, e.Item.Text, e.Item.Font, e.Bounds, itemColor, flags);

    if (lView.View == View.Tile && e.Item.SubItems.Count > 1)
    {
        var subItem = e.Item.SubItems[1];
        flags = GetTextAlignment(lView, 1);
        TextRenderer.DrawText(e.Graphics, subItem.Text, subItem.Font, e.Bounds, SystemColors.GrayText, flags);
    }
}

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    ListView lView = sender as ListView;
    TextFormatFlags flags = GetTextAlignment(lView, e.ColumnIndex);
    Color itemColor = e.Item.ForeColor;
    if (e.Item.Selected) {
        if (e.ColumnIndex == 0 || lView.FullRowSelect) {
            using (SolidBrush bkgrBrush = new SolidBrush(listViewSelectionColor)) {
                e.Graphics.FillRectangle(bkgrBrush, e.Bounds);
            }
            itemColor = e.Item.BackColor;
        }
    }
    else  {
        e.DrawBackground();
    }
    TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, itemColor, flags);
}

protected void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

private TextFormatFlags GetTextAlignment(ListView lstView, int colIndex)
{
    TextFormatFlags flags = (lstView.View == View.Tile)
        ? (colIndex == 0) ? TextFormatFlags.Default : TextFormatFlags.Bottom
        : TextFormatFlags.VerticalCenter;

    flags |= TextFormatFlags.LeftAndRightPadding | TextFormatFlags.NoPrefix;
    switch (lstView.Columns[colIndex].TextAlign)
    {
        case HorizontalAlignment.Left:
            flags |= TextFormatFlags.Left;
            break;
        case HorizontalAlignment.Right:
            flags |= TextFormatFlags.Right;
            break;
        case HorizontalAlignment.Center:
            flags |= TextFormatFlags.HorizontalCenter;
            break;
    }
    return flags;
}

这篇关于如何更改ListView的默认选择颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆