【问题标题】:Change ComboBox Border Color in Windows Forms在 Windows 窗体中更改组合框边框颜色
【发布时间】:2016-04-24 22:55:36
【问题描述】:

在我的应用程序中,我添加了组合框,如下图所示

我已将组合框属性设置为

cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

现在我的问题是如何将边框样式设置为组合框,使其看起来不错。

我在下面的链接中验证了

Flat style Combo box

我的问题与下面的链接不同。

Generic ComboBox in Windows Forms Application

How to override UserControl class to draw a custom border?

【问题讨论】:

  • 仔细阅读问题。
  • Windows 窗体控件没有边框样式,所以如果您想要一个同时保持扁平样式,则必须自己绘制。
  • 首先检查你把它作为重复的问题。那个没有正确答案。 @CodingGorilla
  • 我不明白。你设置了扁平化风格,你不喜欢它,所以你想改变它?太好了,将其设置回系统样式,使其看起来像所有其他标准组合框。扁平的风格首先是令人厌恶的。

标签: c# .net winforms combobox


【解决方案1】:

您可以从ComboBox 继承并覆盖WndProc 并处理WM_PAINT 消息并为您的组合框绘制边框:

using System;
using System.Drawing;
using System.Windows.Forms;

public class FlatCombo : ComboBox
{
    private const int WM_PAINT = 0xF;
    private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
    Color borderColor = Color.Blue;
    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; Invalidate(); }
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple)
        {
            using (var g = Graphics.FromHwnd(Handle))
            {
                using (var p = new Pen(BorderColor))
                {
                    g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);

                    var d = FlatStyle == FlatStyle.Popup ? 1 : 0;
                    g.DrawLine(p, Width - buttonWidth - d,
                        0, Width - buttonWidth - d, Height);
                }
            }
        }
    }
}

注意:

  • 在上面的示例中,我使用前景色作为边框,您可以添加BorderColor 属性或使用其他颜色。
  • 如果你不喜欢下拉按钮的左边框,可以评论DrawLine方法。
  • 控件为RightToLeft时需要从(0, buttonWidth)到(Height, buttonWidth)画线
  • 要了解更多关于如何渲染平面组合框的信息,您可以查看.Net Framework 内部ComboBox.FlatComboAdapter 类的源代码。

平面组合框

你可能也喜欢Flat ComboBox:

【讨论】:

    【解决方案2】:

    CodingGorilla 有正确答案,从 ComboBox 派生出您自己的控件,然后自己绘制边框。

    这是一个绘制 1 像素宽的深灰色边框的工作示例:

    class ColoredCombo : ComboBox
    {
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            using (var brush = new SolidBrush(BackColor))
            {
                e.Graphics.FillRectangle(brush, ClientRectangle);
                e.Graphics.DrawRectangle(Pens.DarkGray, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
            }
        }
    }
    


    左边正常,右边我的例子。

    【讨论】:

      【解决方案3】:

      另一种选择是在Parent控件的Paint Event中自己绘制边框:

          Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
              Panel1.CreateGraphics.DrawRectangle(Pens.Black, ComboBox1.Left - 1, ComboBox1.Top - 1, ComboBox1.Width + 1, ComboBox1.Height + 1)
          End Sub
      

      -OO-

      【讨论】:

      • 在我的例子中,父容器就是表单本身。 C# 等效代码:this.CreateGraphics().DrawRectangle(Pens.Gray, combo1EClientVersion.Left - 1, combo1EClientVersion.Top - 1, combo1EClientVersion.Width + 1, combo1EClientVersion.Height + 1);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      相关资源
      最近更新 更多