简体   繁体   English

在 Windows 窗体中更改 ComboBox 边框颜色

[英]Change ComboBox Border Color in Windows Forms

In My Application i have added Combobox as shown in below picture在我的应用程序中,我添加了组合框,如下图所示

在此处输入图像描述

i have set the combobox property as我已将组合框属性设置为

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

And now my question is how to set border style to combobox so that it will look nice.现在我的问题是如何将边框样式设置为组合框,以便它看起来不错。

I verified in below link我在下面的链接中验证

Flat style Combo box 平面样式组合框

My question is different from below link's.我的问题与以下链接不同。

Generic ComboBox in Windows Forms Application Windows 窗体应用程序中的通用组合框

How to override UserControl class to draw a custom border? 如何覆盖 UserControl 类来绘制自定义边框?

You can inherit from ComboBox and override WndProc and handle WM_PAINT message and draw border for your combo box:您可以从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);
                }
            }
        }
    }
}

Note:笔记:

  • In the above example I used fore color for border, you can add a BorderColor property or use another color.在上面的示例中,我使用前景色作为边框,您可以添加BorderColor属性或使用其他颜色。
  • If you don't like the left border of dropdown button, you can comment that DrawLine method.如果你不喜欢下拉按钮的左边框,你可以评论那个DrawLine方法。
  • You need to draw line when the control is RightToLeft from (0, buttonWidth) to (Height, buttonWidth)当控件为RightToLeft(0, buttonWidth)(Height, buttonWidth)时需要画线
  • To learn more about how to render a flat combo box, you can take a look at source code of internal ComboBox.FlatComboAdapter class of.Net Framework.要了解有关如何呈现平面组合框的更多信息,您可以查看 .Net Framework 的内部ComboBox.FlatComboAdapter类的源代码。

Flat ComboBox平面组合框

You may also like Flat ComboBox :您可能也喜欢平面组合框

在此处输入图像描述 在此处输入图像描述

CodingGorilla has the right answer, derive your own control from ComboBox and then paint the border yourself. CodingGorilla 的答案是正确的,从 ComboBox 派生你自己的控件,然后自己绘制边框。

Here's a working example that paints a 1 pixel wide dark gray border:这是一个绘制 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);
        }
    }
}

自定义组合框边框示例
Normal on the left, my example on the right.左边是正常的,右边是我的例子。

Another option is to draw the border yourself in the Parent control's Paint Event:另一种选择是在 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- -OO-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM