开发者

How to override UserControl class to draw a custom border?

I would like to override System.Windows.Forms.UserControl to draw a custom border (e.g. using custom color). It's not possiblу to do usign built-in classes, because the only method/property you can affect the border behavior is BorderStyle.

Overriding OnPaint the following way (code below) is not a good solution, because it's basically drawing another border on top of original one.

    pro开发者_如何学Ctected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (this.BorderStyle == BorderStyle.FixedSingle)
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.LightGray, ButtonBorderStyle.Solid);
    }

Does anyone know how to override border drawing in custom control?

Putting this user control into a panel is not an option in my case for certain reasons.


Set base.BorderStyle to None to the default border isn't drawn. You'll need to override the BorderStyle property to make this work.

    public UserControl1() {
        InitializeComponent();
        base.BorderStyle = BorderStyle.None;
        this.BorderStyle = BorderStyle.FixedSingle;
    }

    private BorderStyle border;

    public new BorderStyle BorderStyle {
        get { return border; }
        set {
            border = value;
            Invalidate();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜