更改组合框突出显示的颜色 [英] Changing the Color of ComboBox highlighting

查看:142
本文介绍了更改组合框突出显示的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来解决不断变化的在C#中组合框下拉突出的颜色 Windows窗体应用程序。
我已经寻找答案了整个网络,我发现最好的选择,到目前为止是提请当正在绘制所选项目的所需颜色的矩形。



<预类=郎-CS prettyprint-覆盖> 类搜索
{
公共搜索()
{
}

私人无效addFilter()方法
{
组合框字段=新的ComboBox();

field.Items.AddRange(新的String [] {项目1,ITEM2});
field.Text =项目1;
field.DropDownStyle = ComboBoxStyle.DropDownList;
field.FlatStyle = FlatStyle.Flat的;
field.BackColor = Color.FromArgb(235,235,235);
field.DrawMode = DrawMode.OwnerDrawFixed;
field.DrawItem + = field_DrawItem;
}

私人无效field_DrawItem(对象发件人,DrawItemEventArgs E)
{
如果(e.Index> = 0)
{
组合框组合=发件人作为组合框;

如果(e.Index == combo.SelectedIndex)
e.Graphics.FillRectangle(新SolidBrush(Color.Gray),
e.Bounds
);
,否则
e.Graphics.FillRectangle(新SolidBrush(combo.BackColor),
e.Bounds
);

e.Graphics.DrawString(combo.Items [e.Index]的ToString(),e.​​Font,
新SolidBrush(combo.ForeColor),
新点( e.Bounds.X,e.Bounds.Y)
);
}
}
}



这段代码的问题,是,一旦在下拉列表中的另一项选择,其他项目我绘制一个矩形仍然是我想强调的颜色。
然后我试图挽救最后绘制的项目,并重绘:



<预类=郎-CS prettyprint-覆盖> 类搜索
{
私人DrawItemEventArgs lastDrawn;

公共搜索()
{
lastDrawn = NULL;
}

私人无效addFilter()方法
{
组合框字段=新的ComboBox();

field.Items.AddRange(新的String [] {项目1,ITEM2});
field.Text =项目1;
field.DropDownStyle = ComboBoxStyle.DropDownList;
field.FlatStyle = FlatStyle.Flat的;
field.BackColor = Color.FromArgb(235,235,235);
field.DrawMode = DrawMode.OwnerDrawFixed;
field.DrawItem + = field_DrawItem;
}

私人无效field_DrawItem(对象发件人,DrawItemEventArgs E)
{
如果(e.Index> = 0)
{
组合框组合=发件人作为组合框;
如果(e.Index == combo.SelectedIndex)
{
e.Graphics.FillRectangle(新SolidBrush(Color.Gray),e.Bounds);
如果(lastDrawn!= NULL)
lastDrawn.Graphics.FillRectangle(新SolidBrush(combo.BackColor),
lastDrawn.Bounds
);
lastDrawn = E;
}
,否则
e.Graphics.FillRectangle(新SolidBrush(combo.BackColor),
e.Bounds
);

e.Graphics.DrawString(combo.Items [e.Index]的ToString(),e.​​Font,
新SolidBrush(combo.ForeColor),
新点( e.Bounds.X,e.Bounds.Y)
);
}
}
}

这行返回一个错误,因为lastDrawn.Bounds的(不兼容的类型)



<预类=郎-CS prettyprint-覆盖> lastDrawn.Graphics.FillRectangle(新SolidBrush(组合。背景色),
lastDrawn.Bounds
);



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


解决方案

如果你使用的是组合框在项目中的多个地方,就没有什么意义了一遍又一遍重复 DRAWITEM 的事件。就在这个类添加到您的项目,你将有一个具有 HightlightColor 属性,该属性将使得它更容易使用控制遍布项目中的新ComboBox控件:

 类AdvancedComboBox:组合框
{
新的公共System.Windows.Forms.DrawMode DrawMode {搞定;组; }
众彩HighlightColor {搞定;组; }

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

无效AdvancedComboBox_DrawItem(对象发件人,DrawItemEventArgs E)
{
如果(e.Index℃下)
的回报;

组合框组合=发件人作为组合框;
如果((e.State&安培; DrawItemState.Selected)== DrawItemState.Selected)
e.Graphics.FillRectangle(新SolidBrush(HighlightColor),
e.Bounds);
,否则
e.Graphics.FillRectangle(新SolidBrush(combo.BackColor),
e.Bounds);

e.Graphics.DrawString(combo.Items [e.Index]的ToString(),e.​​Font,
新SolidBrush(combo.ForeColor),
新点( e.Bounds.X,e.Bounds.Y));

e.DrawFocusRectangle();
}
}


I am trying to work around changing the color of highlighting in a ComboBox dropdown on a C# Windows Forms application. 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.

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:

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.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                                 lastDrawn.Bounds
                                                );

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

解决方案

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

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();
    }
}

这篇关于更改组合框突出显示的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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