VisualStudio: how to add a dashed border to a UserControl during development?

I have a user control that descends from a UserControl.

When deleted on a form, the user control is invisible because it does not have any border to show itself.

The PictureBox and Panel controls draw a 1px dotted border at design time to make it visible.

What is the right way to do this? Is there an attribute that you can use to add VS?

+3
source share
2 answers

, . , OnPaint .

base.OnPaint(e), , .

 protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (this.DesignMode)
             ControlPaint.DrawBorder( e.Graphics,this.ClientRectangle,Color.Gray,ButtonBorderStyle.Dashed);   
    }

, if, DesignMode , IDE.

+3

, Panel ( , ), DesignerAttribute. Control -, (, Timer).

DesignerAttribute , IDesigner. Control, , ControlDesigner.

ControlDesigner OnPaintAdornment. - , , , , .

, Panel. , , , , Panel.

internal class PanelDesigner : ScrollableControlDesigner
{
    protected Pen BorderPen
    {
        get
        {
            Color color = ((double)this.Control.BackColor.GetBrightness() < 0.5) ? ControlPaint.Light(this.Control.BackColor) : ControlPaint.Dark(this.Control.BackColor);
            return new Pen(color)
            {
                DashStyle = DashStyle.Dash
            };
        }
    }
    public PanelDesigner()
    {
        base.AutoResizeHandles = true;
    }
    protected virtual void DrawBorder(Graphics graphics)
    {
        Panel panel = (Panel)base.Component;
        if (panel == null || !panel.Visible)
        {
            return;
        }
        Pen borderPen = this.BorderPen;
        Rectangle clientRectangle = this.Control.ClientRectangle;
        int num = clientRectangle.Width;
        clientRectangle.Width = num - 1;
        num = clientRectangle.Height;
        clientRectangle.Height = num - 1;
        graphics.DrawRectangle(borderPen, clientRectangle);
        borderPen.Dispose();
    }
    protected override void OnPaintAdornments(PaintEventArgs pe)
    {
        Panel panel = (Panel)base.Component;
        if (panel.BorderStyle == BorderStyle.None)
        {
            this.DrawBorder(pe.Graphics);
        }
        base.OnPaintAdornments(pe);
    }
}

ScrollableControlDesigner - , .

0

Source: https://habr.com/ru/post/1702841/


All Articles