更改组合框边框轮廓颜色

jul*_*old 5 c# combobox winforms

我正在尝试管理ComboBox的颜色。虽然可以更改背景颜色,但找不到边框轮廓的属性。

由于箭头的缘故,在黑暗的主题中仅画一个正方形作为边框是行不通的。这使我得出以下结论:该边框可能是实际的图像文件。

是否可以替换呢?

在此处输入图片说明

更新: 我已经实现了@AhmedAbdelhameed的解决方案-现在看起来好多了。但是对于平面样式,我必须像下面那样调整矩形:

using (var p = new Pen(this.BorderColor, 1))
{
    g.DrawRectangle(p, 0, 0, Width - buttonWidth - 1, Height - 1);
}
Run Code Online (Sandbox Code Playgroud)

我还交换了“ BorderColor”以匹配我的其他UI:

public CustomComboBox()
{
    BorderColor = Color.Gray;
} 
Run Code Online (Sandbox Code Playgroud)

到目前为止的结果是: 在此处输入图片说明 在此处输入图片说明

我现在想做的是仅在深色主题中更改实际的下拉按钮(也许带有叠加png)

更新: 我已经能够使用以下代码向自定义控件添加pricturebox:

using (var g = Graphics.FromHwnd(Handle))
{
    using (var p = new Pen(this.BorderColor, 1))
    {
        g.DrawRectangle(p, 0, 0, Width - buttonWidth - 1, Height - 1);
    }
    if (Properties.Settings.Default.Theme == "Dark")
    {
        g.DrawImageUnscaled(Properties.Resources.dropdown, new Point(Width - buttonWidth - 1));
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来很棒!当我在主题组合框中更改主题时,或多或少由于我不了解的巧合,深色的下拉按钮甚至消失了。

之前-比较之后: 在此处输入图片说明 在此处输入图片说明

Ahm*_*eed 5

这个答案的帮助下,我提出了以下建议:

首先,将以下内容添加到表单中以避免闪烁:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams handleParam = base.CreateParams;
        handleParam.ExStyle |= 0x02000000;      // WS_EX_COMPOSITED
        return handleParam;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,将以下类添加到您的项目中:

public class CustomComboBox : ComboBox
{
    private const int WM_PAINT = 0xF;
    private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT)
        {
            using (var g = Graphics.FromHwnd(Handle))
            {
                // Uncomment this if you don't want the "highlight border".
                /*
                using (var p = new Pen(this.BorderColor, 1))
                {
                    g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
                }*/
                using (var p = new Pen(this.BorderColor, 2))
                {
                    g.DrawRectangle(p, 2, 2, Width - buttonWidth - 4, Height - 4);
                }
            }
        }
    }

    public CustomComboBox()
    {
        BorderColor = Color.DimGray;
    }

    [Browsable(true)]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DimGray")]
    public Color BorderColor { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

重建项目,用new替换ComboBox控件CustomComboBox,将BorderColor属性设置为您选择的颜色,就可以了。

结果:

ComboBox_BorderColor

更新:

使用以下值似乎可以得到更好的结果(特别是在单击下拉按钮时),但是您可能仍需要绘制第一个矩形(上面注释的那个),以避免仅在按钮周围显示“突出显示边框”:

using (var p = new Pen(this.BorderColor, 3))
{
    g.DrawRectangle(p, 1, 1, Width - buttonWidth - 3, Height - 3);
}
Run Code Online (Sandbox Code Playgroud)