在C#中更改组合框下拉列表边框的颜色

是否可以在c#中更改组合框下拉列表的边框颜色?

我想将白色边框更改为较深的阴影以匹配深色方案.我搜索了.net文档,找到了DropDownList.BorderStyle属性.但是,我不确定这是否行得通.我正在使用WinForms.

这是我用于组合框的类:

public class FlattenCombo : ComboBox
{
    private Brush BorderBrush = new SolidBrush(SystemColors.WindowFrame);
    private Brush ArrowBrush = new SolidBrush(SystemColors.ControlText);
    private Brush DropButtonBrush = new SolidBrush(SystemColors.Control);

    private Color _borderColor = Color.Black;
    private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
    private static int WM_PAINT = 0x000F; 

    private Color _ButtonColor = SystemColors.Control;

    public Color ButtonColor
    {
        get { return _ButtonColor; }
        set
        {
            _ButtonColor = value;
            DropButtonBrush = new SolidBrush(this.ButtonColor);
            this.Invalidate();
        }
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        switch (m.Msg)
        {
            case 0xf:
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Black);
                g.FillRectangle(BorderBrush, this.ClientRectangle);

                //Draw the background of the dropdown button
                Rectangle rect = new Rectangle(this.Width - 17, 0, 17, this.Height);
                g.FillRectangle(DropButtonBrush, rect);

                //Create the path for the arrow
                System.Drawing.Drawing2D.GraphicsPath pth = new System.Drawing.Drawing2D.GraphicsPath();
                PointF TopLeft = new PointF(this.Width - 13, (this.Height - 5) / 2);
                PointF TopRight = new PointF(this.Width - 6, (this.Height - 5) / 2);
                PointF Bottom = new PointF(this.Width - 9, (this.Height + 2) / 2);
                pth.AddLine(TopLeft, TopRight);
                pth.AddLine(TopRight, Bottom);

                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //Determine the arrow's color.
                if (this.DroppedDown)
                {
                    ArrowBrush = new SolidBrush(SystemColors.HighlightText);
                }
                else
                {
                    ArrowBrush = new SolidBrush(SystemColors.ControlText);
                }

                //Draw the arrow
                g.FillPath(ArrowBrush, pth);

                break;
            default:
                break;
        }
    }

    [Category("Appearance")]
    public Color BorderColor
    {
        get { return _borderColor; }
        set
        {
            _borderColor = value;
            Invalidate(); // causes control to be redrawn
        }
    }

    [Category("Appearance")]
    public ButtonBorderStyle BorderStyle
    {
        get { return _borderStyle; }
        set
        {
            _borderStyle = value;
            Invalidate();
        }
    }

    protected override void OnLostFocus(System.EventArgs e)
    {
        base.OnLostFocus(e);
        this.Invalidate();
    }

    protected override void OnGotFocus(System.EventArgs e)
    {
        base.OnGotFocus(e);
        this.Invalidate();
    }
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        this.Invalidate();
    }
}

解决方法:

我为此花了太长时间了.我从上一个问题中看到,您询问您是否已从以下代码中获取代码:http://www.codeproject.com/Articles/2433/Flatten-that-Combobox并设置了BackColor:

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    switch (m.Msg)
    {
        case 0xf:
            base.BackColor = Color.Black;

使用WndProc,这是我得到的最接近的东西,但没有为下拉列表的Popup / ItemSelection边框上色:

if (this.DroppedDown)
{
    ArrowBrush = new SolidBrush(SystemColors.HighlightText);

    Rectangle dropDownBounds = new Rectangle(0, 0, Width,Height + DropDownHeight );
    //ControlPaint.DrawBorder(g, dropDownBounds, _borderColor, _borderStyle);
    ControlPaint.DrawBorder(g, dropDownBounds, _borderColor,1, ButtonBorderStyle.Dotted ,Color.GreenYellow,1,ButtonBorderStyle.Solid ,Color.Gold,1,ButtonBorderStyle.Dashed,Color.HotPink,1,ButtonBorderStyle.Solid);

}

事实证明,执行此操作所需的类FlatComboBoxAdapter是私有的,建议使用WPF:
ComboBox DropDown-Area Border Color

更多信息:Combobox borderstyle-但是即使有LarsTech和Hans的建议(使用非客户喷漆信息),它仍然不起作用,并且闪烁得可怕.

除了WPF以外的其他建议,请重写Combobox .Net Framework代码:http://www.dotnetframework.org/default.aspx/FX-1434/FX-1434/1@0/untmp/whidbey/REDBITS/ndp/fx/src/WinForms/Managed/System/WinForms/ComboBox@cs/2/ComboBox@cs

上一篇:JavaFX ComboBox图像


下一篇:C#-使用ItemsSource将预定义项目添加到ComboBox