组合框边框 [英] Combobox borderstyle

查看:123
本文介绍了组合框边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi我已经将组合框控件的平面设置为平面。

Hi I have set the combobox control's flatstyle to flat.

可以在此控件周围绘制边框吗?

Is it possible to draw a border around this control?

没有borderstyle属性。任何建议将不胜感激。

The control does not have a borderstyle property. Any suggestions would be appreciated. Side note: I wish to keep the flatstyle flat if at all possible.

推荐答案

创建自定义ComboBox控件,并覆盖它 WndProc 方法。您可以使用 ControlPaint.DrawBorder 方法轻松绘制边框:

Create custom ComboBox control, and override it's WndProc method. You can easily draw a border with ControlPaint.DrawBorder method:

public class ComboBoxWithBorder : ComboBox
{
    private Color _borderColor = Color.Black;
    private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
    private static int WM_PAINT = 0x000F; 

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (m.Msg == WM_PAINT)
        {
            Graphics g = Graphics.FromHwnd(Handle);
            Rectangle bounds = new Rectangle(0, 0, Width, Height);
            ControlPaint.DrawBorder(g, bounds, _borderColor, _borderStyle);
        }
    }

    [Category("Appearance")]
    public Color BorderColor
    {
        get { return _borderColor; }
        set 
        { 
            _borderColor = value;
            Invalidate(); // causes control to be redrawn
        }
    }

    [Category("Appearance")]
    public ButtonBorderStyle BorderStyle
    {
        get { return _borderStyle; }
        set 
        { 
            _borderStyle = value;
            Invalidate();
        }
    }     
}

em>还有重载的DrawBorder方法,它允许设置边框的宽度。如果需要,可以使用它。

BTW There is also overloaded DrawBorder method, which allows to set width of border. Use it if you need.

这篇关于组合框边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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