c# - 更改 ComboBox 边框轮廓颜色

标签 c# winforms combobox

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

由于箭头的缘故,在深色主题中,仅绘制一个正方形作为边框是行不通的。这让我得出结论,这个边框可能是一个实际的图像文件。

这个可以换吗?

enter image description here

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

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

我还交换了“BorderColor”以匹配我的 UI 的其余部分:

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

这是目前的结果: enter image description here enter image description here

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

更新: 我已经能够使用以下代码将图片框添加到自定义控件中:

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

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

比较之前 - 之后: enter image description here enter image description here

最佳答案

this answer的帮助下, 我能够想出以下内容:

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

protected override CreateParams CreateParams
{
    get
    {
        CreateParams handleParam = base.CreateParams;
        handleParam.ExStyle |= 0x02000000;      // WS_EX_COMPOSITED
        return handleParam;
    }
}

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

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

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

结果:

ComboBox_BorderColor

更新:

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

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

关于c# - 更改 ComboBox 边框轮廓颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52541869/

相关文章:

c# - 从字体样式中减去标志(切换字体样式)[C#]

c# - C#中关于ThreadPool的困惑

c# - 如何将文本转换为日期?

c# - 无法在表单中找到 {Microsoft.VisualBasic.PowerPacks.RectangleShape} 控件

c# - C# 中垂直(仅)可调整大小的窗口窗体

c# - 在 Windows 窗体中创建带圆角的自定义 ComboBox

c# - 无限while循环问题

c# - 在代码中复制 SQL Server 数据库

.net - 组合框在显示列表项时,如何将鼠标事件拦截到窗体上的任意位置以隐藏列表?

c# - 显示组合框(项目)的值