本文介绍了在 Windows 窗体中更改 ComboBox 边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已将组合框属性设置为

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

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

我在下面的链接中进行了验证

使用系统;使用 System.Drawing;使用 System.Windows.Forms;公共类 FlatCombo : ComboBox{私有常量 int WM_PAINT = 0xF;private int buttonWidth = SystemInformation.Horizo​​ntalScrollBarArrowWidth;颜色 borderColor = Color.Blue;公共颜色边框颜色{得到 { 返回边框颜色;}设置 { 边框颜色 = 值;无效();}}protected override void WndProc(ref Message m){base.WndProc(ref m);if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple){使用 (var g = Graphics.FromHwnd(Handle)){使用 (var p = new Pen(BorderColor)){g.DrawRectangle(p, 0, 0, 宽度 - 1, 高度 - 1);var d = FlatStyle == FlatStyle.Popup ?1:0;g.DrawLine(p, 宽度 - 按钮宽度 - d,0, 宽度 - 按钮宽度 - d, 高度);}}}}}

注意:

  • 在上面的示例中,我使用前景色作为边框,您可以添加 BorderColor 属性或使用其他颜色.
  • 如果您不喜欢下拉按钮的左边框,您可以注释该 DrawLine 方法.
  • 当控件为RightToLeft时需要从(0, buttonWidth)(Height, buttonWidth)
  • 画线
  • 要了解有关如何呈现平面组合框的更多信息,您可以查看内部

    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

    How to override UserControl class to draw a custom border?

    解决方案

    You can inherit from ComboBox and override WndProc and handle WM_PAINT message and draw border for your combo box:

    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.
    • If you don't like the left border of dropdown button, you can comment that DrawLine method.
    • You need to draw line when the control is RightToLeft from (0, buttonWidth) to (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.

    Flat ComboBox

    You may also like Flat ComboBox:

    这篇关于在 Windows 窗体中更改 ComboBox 边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:53