加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在WIndows表单应用程序中将border应用于组合框?

发布时间:2020-12-15 08:12:27 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我添加了Combobox,如下图所示 我已将组合框属性设置为 cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 而现在我的问题是如何将边框样式设置为组合框以使其看起来不错. 我在下面的链接验证 Flat style Combo box 我的问题
在我的应用程序中,我添加了Combobox,如下图所示

我已将组合框属性设置为

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?

解决方法

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


public class FlatCombo: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))
            {
                using (var p = new Pen(this.ForeColor))
                {
                    g.DrawRectangle(p,Width - 1,Height - 1);
                    g.DrawLine(p,Width - buttonWidth,Height);
                }
            }
        }
    }
}

注意:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读