微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在我的应用程序中,我添加了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的源代码.

原文地址:https://www.jb51.cc/csharp/238941.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐