C#更新usercontrol库将删除内部内容

C#更新usercontrol库将删除内部内容,c#,user-controls,panel,rebuild,C#,User Controls,Panel,Rebuild,我有一个非常讨厌的问题 在我的用户控件库中,我有一个控件,里面有一个面板,可以容纳内容。 如果我构建这个dll并将它引用到一个项目中,我通常可以使用这个控件并向它添加内容。 现在的问题是:每当我构建此控件库的新版本并更新/覆盖我使用此dll的项目中的dll时,将重新生成包含内容的控件,而现在内容将被删除。 如何解决这个问题 致意 斯文·科尼格 编辑1 注意:我指的面板是一个用户控件,它有一个Windows.Forms.panel来存储内容。 此用户控件的设计器是“ParentControlDe

我有一个非常讨厌的问题

在我的用户控件库中,我有一个控件,里面有一个面板,可以容纳内容。 如果我构建这个dll并将它引用到一个项目中,我通常可以使用这个控件并向它添加内容。 现在的问题是:每当我构建此控件库的新版本并更新/覆盖我使用此dll的项目中的dll时,将重新生成包含内容的控件,而现在内容将被删除。 如何解决这个问题


致意

斯文·科尼格

编辑1
注意:我指的面板是一个用户控件,它有一个Windows.Forms.panel来存储内容。 此用户控件的设计器是“ParentControlDesigner”

编辑2
抱歉,要保存到内容的控件直接保存到通过ParentControlDesigner设计的用户控件。 我之所以这样做是因为如果我使用Windows.Forms.Panel,我在设计时有调整大小的选项。这是我不想要的

编辑3
下面是GroupBox的代码,其中的内容控件位于

[Designer(typeof(BorderedGroupBoxDesigner))]
public partial class BorderedGroupBox : UserControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    internal Panel GroupingPanel { get; }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    internal readonly Label TitelLabel;

    private VisualStyleRenderer _renderer;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        TitelLabel = new Label
        {
            Location = new Point(1, 1),
            AutoSize = false,
            TextAlign = ContentAlignment.MiddleLeft
        };

        //Create GroupingPanel
        GroupingPanel = new Panel
        {
            Location = new Point(1, TitleHeight - 1)
        };

        //Create Container and add Panel
        Control container = new ContainerControl
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(-1),
            Controls = {TitelLabel, GroupingPanel}
        };

        //Add container and it's content to this control
        Controls.Add(container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        GroupingPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        GroupingPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
    }

    //Make default prope rty "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public sealed override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return GroupingPanel.BackColor; } set { GroupingPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            GroupingPanel.Location = new Point(GroupingPanel.Location.X, value + 2);
            GroupingPanel.Size = new Size(GroupingPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <returns>Sets visibility of the design grid to easily align controls on grid.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets visibility of the design grid to easily align controls on grid.")]
    [Category("Design")]
    [DesignOnly(true)]
    [DefaultValue(typeof(bool), "false")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public bool ShowDesignGrid
    {
        get { return GroupingPanel.Designer.DrawGridState; }
        set
        {
            if (value)
                GroupingPanel.Designer.EnableDrawGrid();
            else
                GroupingPanel.Designer.DisableDrawGrid();

            Refresh();
        }
    }

    /// <returns>Sets visibility of the design grid to easily align controls on grid.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets size of the design grid.")]
    [Category("Design")]
    [DesignOnly(true)]
    [DefaultValue(typeof(Size), "8; 8")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string DesignGridSize
    {
        get { return $"{GroupingPanel.Designer.GridSize.Width}; {GroupingPanel.Designer.GridSize.Height}"; }
        set
        {
            var values = value.Split(';');

            GroupingPanel.Designer.GridSize = new Size(int.Parse(values[0]), int.Parse(values[1]));
        }
    }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public new Padding Padding
    {
        get { return GroupingPanel.Padding; }
        set { GroupingPanel.Padding = value; }
    }

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

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }
}

internal class BorderedGroupBoxDesigner : ControlDesigner
{
    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);

        var uc = component as BorderedGroupBox;
        if (uc != null)
            EnableDesignMode(uc.GroupingPanel, "Panel");
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}

这是我的代码更新。

更改:
-现在使用标准Windows.Forms.Panel作为控制容器
-为了避免调整大小或移动,我为事件“Resize”和“LocationChanged”编写了代码


下面是结果的图片:



下面是当前代码:

[Designer(typeof(BorderedGroupBoxDesigner))]
public sealed partial class BorderedGroupBox : UserControl
{
    /// <summary>The Panel which stores the content controls.</summary>
    [Category("Behavior")]
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public readonly Panel ContentPanel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Label TitelLabel;

    private VisualStyleRenderer _renderer;

    private int _contentPanelTop => TitleHeight - 1;

    private int _contentPanelLeft => 1;

    private int _contentPanelWidth => Size.Width - 2;

    private int _contentPanelHeight => Size.Height - TitleHeight - 3;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        TitelLabel = new Label
        {
            Location = new Point(1, 1),
            AutoSize = false,
            TextAlign = ContentAlignment.MiddleLeft
        };

        //Create GroupingPanel
        ContentPanel = new Panel
        {
            Location = new Point(1, TitleHeight - 1)
        };

        //Create Container and add Panel
        Control container = new ContainerControl
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(-1),
            Controls = {TitelLabel, ContentPanel}
        };

        //Add container and it's content to this control
        Controls.Add(container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        ContentPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        ContentPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
        AllowDrop = true;

        //Set event handler
        ContentPanel.Resize += ContentPanelOnResize;
        ContentPanel.LocationChanged += ContentPanelOnLocationChanged;
    }

    private void ContentPanelOnLocationChanged(object sender, EventArgs eventArgs)
    {
        if (ContentPanel.Left != _contentPanelLeft | ContentPanel.Top != _contentPanelTop)
            ContentPanel.Location = new Point(_contentPanelLeft, _contentPanelTop);
    }

    private void ContentPanelOnResize(object sender, EventArgs eventArgs)
    {
        if (ContentPanel.Size.Width != _contentPanelWidth | ContentPanel.Size.Height != Size.Height - TitleHeight - 3)
            ContentPanel.Size = new Size(_contentPanelWidth, Size.Height - TitleHeight - 3);
    }

    //Make default property "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return ContentPanel.BackColor; } set { ContentPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            ContentPanel.Location = new Point(ContentPanel.Location.X, value + 2);
            ContentPanel.Size = new Size(ContentPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <returns>Sets the interior spacing in the control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the interior spacing in the control.")]
    [Category("Layout")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public new Padding Padding
    {
        get { return ContentPanel.Padding; }
        set { ContentPanel.Padding = value; }
    }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.Paint" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data. </param>
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }
}

internal class BorderedGroupBoxDesigner : ControlDesigner
{
    private BorderedGroupBox borderedGroupBox;

    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);

        borderedGroupBox = component as BorderedGroupBox;

        if (borderedGroupBox != null)
            EnableDesignMode(borderedGroupBox.ContentPanel, "Panel");
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}
[Designer(typeof(BorderedGroupBoxDesigner))]
公共密封的分部类BordedGroupBox:UserControl
{
///存储内容控件的面板。
[类别(“行为”)]
[可浏览(true)、可编辑可浏览(editorbrowsebleState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共只读面板ContentPanel;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
内部只读标签滴度标签;
私有VisualStyleRenderer\u渲染器;
private int_contentPanelTop=>titlelight-1;
私有int_contentPanelLeft=>1;
私有int\u contentPanelWidth=>Size.Width-2;
private int_contentPanelHeight=>Size.Height-titlelight-3;
/// 
///初始化控件。
/// 
公共边界分组框()
{
初始化组件();
//创建标题标签
TitelLabel=新标签
{
位置=新点(1,1),
AutoSize=false,
TextAlign=ContentAlignment.middleft
};
//创建分组面板
ContentPanel=新面板
{
位置=新点(1,标题灯-1)
};
//创建容器并添加面板
控制容器=新容器控制
{
Dock=DockStyle.Fill,
填充=新填充(-1),
控件={TitelLabel,ContentPanel}
};
//将容器及其内容添加到此控件
控件。添加(容器);
//设置内部控件的大小
TitelLabel.Size=新尺寸(Size.Width-2,20);
ContentPanel.Size=新尺寸(Size.Width-2,Size.Height-titlelight-3);
//设置内部控件的锚点
TitelLabel.Anchor=AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
ContentPanel.Anchor=AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
//默认值
BackgroundColor=SystemColor.Window;
BorderColor=SystemColor.GradientActiveCeption;
TitleBackColor=Color.fromnowncolor(KnownColor.DodgerBlue);
TitleFont=新字体(“Calibri”,titleLight-9,FontStyle.Bold);
TitleFontColor=SystemColor.Window;
AllowDrop=true;
//设置事件处理程序
ContentPanel.Resize+=ContentPanelOnResize;
ContentPanel.LocationChanged+=ContentPanelOnLocationChanged;
}
私有void ContentPanelOnLocationChanged(对象发送方,EventArgs EventArgs)
{
if(ContentPanel.Left!=\u contentPanelLeft | ContentPanel.Top!=\u contentPanelTop)
ContentPanel.Location=新点(\u contentPanelLeft,\u contentPanelTop);
}
私有void ContentPanelOnResize(对象发送方,EventArgs EventArgs)
{
if(ContentPanel.Size.Width!=_contentPanelWidth | ContentPanel.Size.Height!=Size.Height-titlelight-3)
ContentPanel.Size=新尺寸(_contentPanelWidth,Size.Height-titlelight-3);
}
//使默认属性“BackColor”不可见
[可浏览(false)、可编辑可浏览(editorbrowseblestate.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
公共覆盖颜色背景{get;set;}
//使用“BackgroundColor”而不是默认的“BackColor”
///与此控件关联的背景色。
[可浏览(true)、可编辑可浏览(editorbrowsebleState.Always)]
[说明(“组件的背景色”)]
[类别(“外观”)]
[默认值(类型(颜色),“窗口”)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
公共颜色背景颜色{get{return ContentPanel.BackColor;}set{ContentPanel.BackColor=value;}}
///与此控件关联的边框颜色。
[可浏览(true)、可编辑可浏览(editorbrowsebleState.Always)]
[说明(“设置边框颜色”)]
[类别(“外观”)]
[默认值(类型(颜色),“渐变激活选项”)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
公共颜色边框颜色{get{return BackColor;}set{BackColor=value;}}
///与此控件关联的标题的边框颜色。
[可浏览(true)、可编辑可浏览(editorbrowsebleState.Always)]
[说明(“设置标题颜色”)]
[类别(“外观”)]
[默认值(颜色类型),“道奇蓝”)]
[DesignerSerializationVisibility(DesignerSeriali
[Designer(typeof(BorderedGroupBoxDesigner))]
public sealed partial class BorderedGroupBox : UserControl
{
    /// <summary>The Panel which stores the content controls.</summary>
    [Category("Behavior")]
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public readonly Panel ContentPanel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Label TitelLabel;

    private VisualStyleRenderer _renderer;

    private int _contentPanelTop => TitleHeight - 1;

    private int _contentPanelLeft => 1;

    private int _contentPanelWidth => Size.Width - 2;

    private int _contentPanelHeight => Size.Height - TitleHeight - 3;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        TitelLabel = new Label
        {
            Location = new Point(1, 1),
            AutoSize = false,
            TextAlign = ContentAlignment.MiddleLeft
        };

        //Create GroupingPanel
        ContentPanel = new Panel
        {
            Location = new Point(1, TitleHeight - 1)
        };

        //Create Container and add Panel
        Control container = new ContainerControl
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(-1),
            Controls = {TitelLabel, ContentPanel}
        };

        //Add container and it's content to this control
        Controls.Add(container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        ContentPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        ContentPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
        AllowDrop = true;

        //Set event handler
        ContentPanel.Resize += ContentPanelOnResize;
        ContentPanel.LocationChanged += ContentPanelOnLocationChanged;
    }

    private void ContentPanelOnLocationChanged(object sender, EventArgs eventArgs)
    {
        if (ContentPanel.Left != _contentPanelLeft | ContentPanel.Top != _contentPanelTop)
            ContentPanel.Location = new Point(_contentPanelLeft, _contentPanelTop);
    }

    private void ContentPanelOnResize(object sender, EventArgs eventArgs)
    {
        if (ContentPanel.Size.Width != _contentPanelWidth | ContentPanel.Size.Height != Size.Height - TitleHeight - 3)
            ContentPanel.Size = new Size(_contentPanelWidth, Size.Height - TitleHeight - 3);
    }

    //Make default property "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return ContentPanel.BackColor; } set { ContentPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            ContentPanel.Location = new Point(ContentPanel.Location.X, value + 2);
            ContentPanel.Size = new Size(ContentPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <returns>Sets the interior spacing in the control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the interior spacing in the control.")]
    [Category("Layout")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public new Padding Padding
    {
        get { return ContentPanel.Padding; }
        set { ContentPanel.Padding = value; }
    }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.Paint" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data. </param>
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }
}

internal class BorderedGroupBoxDesigner : ControlDesigner
{
    private BorderedGroupBox borderedGroupBox;

    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);

        borderedGroupBox = component as BorderedGroupBox;

        if (borderedGroupBox != null)
            EnableDesignMode(borderedGroupBox.ContentPanel, "Panel");
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}
    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        e.Control.BringToFront();
    }
[Designer(typeof(BorderedGroupBoxDesigner))]
[DefaultEvent("Load")]
public sealed partial class BorderedGroupBox : UserControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Panel BackgroundPanel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Label TitelLabel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal new readonly ContainerControl Container;

    private VisualStyleRenderer _renderer;

    private int _contentPanelTop => TitleHeight + 2;

    private int _contentPanelLeft => 1;

    private int _contentPanelWidth => Size.Width - 2;

    private int _contentPanelHeight => Size.Height - TitleHeight;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        if (TitelLabel == null)
            TitelLabel = new Label
            {
                Location = new Point(1, 1),
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleLeft
            };

        //Create BackgroundPanel
        if (BackgroundPanel == null)
            BackgroundPanel = new Panel
            {
                Location = new Point(1, TitleHeight - 1)
            };

        //Create Container and add Panel
        if (Container == null)
            Container = new ContainerControl
            {
                Dock = DockStyle.Fill,
                Padding = new Padding(-1),
                Controls = { TitelLabel, BackgroundPanel }
            };

        //Add container and it's content to this control
        Controls.Add(Container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        BackgroundPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        BackgroundPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
        AllowDrop = true;
    }

    //Make default property "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return BackgroundPanel.BackColor; } set { BackgroundPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            BackgroundPanel.Location = new Point(BackgroundPanel.Location.X, value + 2);
            BackgroundPanel.Size = new Size(BackgroundPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.Paint" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data. </param>
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.ControlAdded" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.ControlEventArgs" /> that contains the event data. </param>
    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        e.Control.BringToFront();
    }
}

internal class BorderedGroupBoxDesigner : ParentControlDesigner
{
    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}