auto-resize multiple windows forms being rendered on panel

后端 未结 1 1697
挽巷
挽巷 2020-11-28 17:02

I have this windows form with panels. This form has three panels. One panel is collapsible and acts as a sidebar, the other one sits at the top and is there for showing titl

相关标签:
1条回答
  • 2020-11-28 17:11

    After some clarifications, it appears that the desidered layout and behaviour of the described Form is similar to this sample disposition:

    A WinForms Form is embedded in another Form, and placed inside a Panel.
    This Guest Form is stripped of its TopLevel coat-of-arms and parented to central Panel, as shown in this graphic example:


    How do you dock these Panels to get this layout:

    The Green Panel stays on top of the Form.
    The DarkGray Panel lays on the left hand side of the Form.
    The Gray Panel occupies the remaining space.

    • Insert three Panels on a Form container.
    • The Green Panel needs to maintain its position, it will never change:
      • Right click → SendToBack (!important :).
      • Dock → Top.
    • The DarkGray Panel is positioned under the Green Panel, on the left side of the Form. It needs to resize itself when needed, but will never cover the Green Panel:
      • Dock → Left
    • The Gray Panel needs to occupy the remaining space. It needs to resize itself when needed, but it will never cover Green Panel or Dark Gray Panel:
      • Right click → BringToFront (!important)
      • Dock → Center

    The highest priority when docking, is assigned to the element which has the lowest z-order in the stack: the Green Panel, here. The lowest priority is assigned to element with the highest z-order: the Gray Panel, which will then shrink and stretch among all other elements with higher priority (following the z-order).

    How to embed the Form:

    The easy part. It's a Form in our Project, no need to perform any magic to keep it alive when re-parented:
    (This is just for 1 Form. With more Forms, you'll need something like a List<Control>:

    //Define here the Form which will be embedded
    [Your Form Class] EmbeddedForm;
    
    private void button1_Click(object sender, EventArgs e)
    {
        EmbeddedForm = new [Your Form Class]() {
            TopLevel = false,
            Parent = panContainer,
            Location = new Point(4, 4),
            Enabled = true
        };
        EmbeddedForm.Show();
    }
    
    private void buttonShrink_Click(object sender, EventArgs e)
    {
        //Maybe insert a classic dotted mini-button to re-inflate the sidebar when needed
        panelSideBar.Width = 6;
    }
    
    private void panelContainer_Resize(object sender, EventArgs e)
    {
        Rectangle rect = panelContainer.ClientRectangle;
        rect.Inflate(-3, -3);
        EmbeddedForm.Size = rect.Size;
    }
    

    If you allow your Container Panel to AutoScroll its content, the Resize event is not necessary.

    Edit:
    A PasteBin of the complete source code of the Form in sample graphics: Embedded Forms

    0 讨论(0)
提交回复
热议问题