自动调整在面板上呈现的多个表单的大小 [英] Auto-resize multiple Forms rendered on Panel

查看:21
本文介绍了自动调整在面板上呈现的多个表单的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有面板的表单.此表单具有三个面板.
一个面板是可折叠的并充当侧边栏,另一个位于顶部并用于显示标题,最后一个是通过单击面板一中提供的项目之一打开的表单的占位符.

现在我想要做的是根据面板一的状态调整(扩大和缩小)占位符面板的大小(仅宽度)和面板上打开的表单,可以展开或折叠.扩展坞不工作.

解决方案

经过一些澄清后,所描述表单的所需布局和行为似乎与此示例配置类似:

一个 WinForms Form 嵌入在另一个 Form 中,并放置在一个 Panel.
此访客 Form 去除了它的 TopLevel 徽章并父级到中央面板,如下图所示:


如何停靠这些 Panel 以获得此布局:

绿色面板位于表单顶部.
深灰色面板位于表单的左侧.
灰色面板占用剩余空间.

  • 在表单容器上插入三个面板.
  • 绿色面板需要保持其位置,永远不会改变:
    • 右键单击 → SendToBack (!important :).
    • 停靠→顶部.
  • 深灰色面板位于绿色面板下方,表单左侧.它需要在需要时调整自己的大小,但永远不会覆盖绿色面板:
    • 停靠→左
  • 灰色面板需要占用剩余空间.它需要在需要时调整自己的大小,但它永远不会覆盖绿色面板或深灰色面板:
    • 右键单击 →BringToFront (!important)
    • 停靠→中心

对接时的最高优先级分配给堆栈中具有最低 z 顺序的元素:绿色面板,此处.最低优先级分配给具有最高 z 顺序的元素:灰色面板,然后它会收缩和拉伸在所有其他具有更高优先级的元素中(遵循 z 顺序).

如何嵌入表单:

容易的部分.它是我们项目中的一个表单,无需执行任何魔法来使其在重新父化时保持活力:
(这仅适用于 1 个表单.对于更多表单,您将需要类似 List:

//这里定义要嵌入的Form[你的表单类] EmbeddedForm;private void button1_Click(object sender, EventArgs e){EmbeddedForm = new [您的表单类]() {顶层 = 假,父 = panContainer,位置 = 新点 (4, 4),启用 = 真};EmbeddedForm.Show();}private void buttonShrink_Click(object sender, EventArgs e){//也许可以插入一个经典的虚线迷你按钮在需要时重新充气侧边栏panelSideBar.Width = 6;}私有无效面板Container_Resize(对象发送者,EventArgs e){矩形矩形 = panelContainer.ClientRectangle;rect.Inflate(-3, -3);EmbeddedForm.Size = rect.Size;}

如果您允许容器面板 AutoScroll 其内容,则不需要 Resize 事件.


示例图形中 Form 的完整源代码的 PasteBin:嵌入式表单

I have this 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 title, the last one is the placeholder for the Forms being opened by clicking on one of the items being catered in Panel one.

Now what I want to do is resize (grow and shrink) the size (width only) of the placeholder Panel and the Form that is opened on the Panel according to the state of Panel one, which could either be expanded or collapsed. The dock is not working.

解决方案

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

这篇关于自动调整在面板上呈现的多个表单的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆