C# 更改 ComboBox 突出显示的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13212179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:47:03  来源:igfitidea点击:

Changing the Color of ComboBox highlighting

c#winformscomboboxhighlighting

提问by Lino Oliveira

I am trying to work around changing the color of highlighting in a ComboBoxdropdown on a C# Windows Formsapplication. I have searched the whole web for an answer, and the best option i found so far was to draw a rectangle of the desired color when the item that is selected is being drawn.

我正在尝试ComboBox在 C#Windows Forms应用程序的下拉列表中更改突出显示的颜色。我在整个网络上搜索了答案,到目前为止我发现的最佳选择是在绘制所选项目时绘制所需颜色的矩形。

Class Search
{
    Public Search()
    {
    }

    private void addFilter()
    {
        ComboBox field = new ComboBox();

        field.Items.AddRange(new string[] { "Item1", "item2" });
        field.Text = "Item1";
        field.DropDownStyle = ComboBoxStyle.DropDownList;
        field.FlatStyle = FlatStyle.Flat;
        field.BackColor = Color.FromArgb(235, 235, 235);
        field.DrawMode = DrawMode.OwnerDrawFixed;
        field.DrawItem += field_DrawItem;
    }

    private void field_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index >= 0)
        {
            ComboBox combo = sender as ComboBox;

            if (e.Index == combo.SelectedIndex)
                e.Graphics.FillRectangle(new SolidBrush(Color.Gray),
                                         e.Bounds
                                        );
            else
                e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                         e.Bounds
                                        );

            e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                                  new SolidBrush(combo.ForeColor),
                                  new Point(e.Bounds.X, e.Bounds.Y)
                                 );
        }
    }
}

The problem with this code, is that once another Item in the dropdown is selected, the other Item I draw a rectangle is still with the color i want to highlight. Then i tried to save the last Item drawn and redraw it:

这段代码的问题是,一旦选择了下拉列表中的另一个项目,我绘制矩形的另一个项目仍然是我想要突出显示的颜色。然后我尝试保存绘制的最后一个 Item 并重新绘制它:

Class Search
{
    private DrawItemEventArgs lastDrawn;

    Public Search()
    {
        lastDrawn = null;
    }

    private void addFilter()
    {
        ComboBox field = new ComboBox();

        field.Items.AddRange(new string[] { "Item1", "item2" });
        field.Text = "Item1";
        field.DropDownStyle = ComboBoxStyle.DropDownList;
        field.FlatStyle = FlatStyle.Flat;
        field.BackColor = Color.FromArgb(235, 235, 235);
        field.DrawMode = DrawMode.OwnerDrawFixed;
        field.DrawItem += field_DrawItem;
    }

    private void field_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index >= 0)
        {
            ComboBox combo = sender as ComboBox;
            if (e.Index == combo.SelectedIndex)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Gray), e.Bounds);
                if(lastDrawn != null)
                    lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                                 lastDrawn.Bounds
                                                );
                lastDrawn = e;
            }
            else
                e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                         e.Bounds
                                        );

            e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                                  new SolidBrush(combo.ForeColor),
                                  new Point(e.Bounds.X, e.Bounds.Y)
                                 );
        }
    }
}

This line returns an error because of lastDrawn.Bounds (incompatible type)

由于 lastDrawn.Bounds(类型不兼容),此行返回错误

lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                                 lastDrawn.Bounds
                                                );

I am feeling that changing the highlight color of the dropdown is impossible. Thanks in advance!

我觉得改变下拉菜单的高亮颜色是不可能的。提前致谢!

采纳答案by Lino Oliveira

In case you are using the ComboBoxin more than one place in your project, it will not make sense to repeat the same code for DrawItemevent over and over again. Just add this class to your project and you will have a new ComboBox control that has the HightlightColorproperty which will makes it easier to use the control all over the project:

如果您ComboBox在项目中的多个地方使用,那么一遍DrawItem又一遍地为事件重复相同的代码是没有意义的。只需将此类添加到您的项目中,您将拥有一个新的 ComboBox 控件,该控件具有以下HightlightColor属性,可以更轻松地在整个项目中使用该控件:

class AdvancedComboBox : ComboBox
{
    new public System.Windows.Forms.DrawMode DrawMode { get; set; }
    public Color HighlightColor { get; set; }

    public AdvancedComboBox()
    {
        base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        this.HighlightColor = Color.Gray;
        this.DrawItem += new DrawItemEventHandler(AdvancedComboBox_DrawItem);
    }

    void AdvancedComboBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index < 0)
            return;

        ComboBox combo = sender as ComboBox;
        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            e.Graphics.FillRectangle(new SolidBrush(HighlightColor),
                                     e.Bounds);
        else
            e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                     e.Bounds);

        e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                              new SolidBrush(combo.ForeColor),
                              new Point(e.Bounds.X, e.Bounds.Y));

        e.DrawFocusRectangle();
    }
}