在Windows窗体中更改ComboBox边框颜色 [英] Change ComboBox Border Color in Windows Forms

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

问题描述

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





我已将combobox属性设置为

  cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle。平面; 

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

我在下面的链接中验证



 使用系统; 
使用System.Drawing;
使用System.Windows.Forms;

公共类FlatCombo:ComboBox
{
private const int WM_PAINT = 0xF;
private int buttonWidth = SystemInformation.Horizo​​ntalScrollBarArrowWidth;
Color borderColor = Color.Blue;
public Color BorderColor
{
get {return borderColor; }
set {borderColor = value; Invalidate(); }
}
受保护的覆盖无效WndProc(ref Message m)
{
base.WndProc(ref m);
if(m.Msg == WM_PAINT&& DropDownStyle!= ComboBoxStyle.Simple)
{
使用(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 时,您需要绘制线code>(0,buttonWidth)到(Height,buttonWidth)

  • 了解更多有关如何呈现平面组合框的信息,您可以查看内部 ComboBox.FlatComboAdapter 类。


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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆