How to override UserControl class to draw a custom border?

て烟熏妆下的殇ゞ 提交于 2019-12-12 13:33:46

问题


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.

    protected 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.


回答1:


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


来源:https://stackoverflow.com/questions/3908384/how-to-override-usercontrol-class-to-draw-a-custom-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!