Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

c# - Combobox borderstyle

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

Is it possible to draw a border around this control?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
        }
    }     
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...