1、下载Nuget包,并安装
2、工具箱中拖入如下两个工具到界面
3、设置DockPanelSuite属性
4、更改子窗体代码,若Form2为子窗体,则将其继承Form改为DockContent
5、实现Form2每次显示在Form1内,可以更改DockState实现设置停靠位置
6、同Form2一样,再设置Form3
7、实现Form2和Form3显示在Form1的DockPanel中
8、保存配置文件到本栋,以及从本地加载界面配置,设置默认窗体布局
查看代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace 测试DockPanelSuite设计界面
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 form2;
Form3 form3;
private string m_DockPath = "";
private void 加载布局_Click(object sender, EventArgs e)
{
initFormsLayout();
}
private void 打开窗体_Click(object sender, EventArgs e)
{
if (form2 != null && !form2.IsDisposed) { } //已经打开
else
{
form2 = new Form2();
}
form2.Show(dockPanel1, DockState.DockLeft);
if (form3 != null && !form3.IsDisposed) { } //已经打开
else
{
form3 = new Form3();
}
form3.Show(dockPanel1, DockState.DockLeft);
}
private void 默认布局_Click(object sender, EventArgs e)
{
ShowDefaultLayout();
}
private void 保存布局_Click(object sender, EventArgs e)
{
save();
}
/// <summary>
/// 加载窗体布局
/// </summary>
void initFormsLayout()
{
form2 = new Form2();
form3 = new Form3();
dockPanel1.Visible = true;
m_DockPath = "DockPanel.config";
InitDockPanel(m_DockPath);
}
private IDockContent GetContentFromPersistString(string persistString)
{
if (persistString == typeof(Form2).ToString())
return form2;
if (persistString == typeof(Form3).ToString())
return form3;
return null;
}
public void InitDockPanel(string m_DockPath)
{
if (File.Exists(m_DockPath))
{
dockPanel1.LoadFromXml(m_DockPath, GetContentFromPersistString); //根据配置文件动态加载浮动窗体
}
}
/// <summary>
/// 默认布局
/// </summary>
public void ShowDefaultLayout()
{
if (form2 != null && !form2.IsDisposed)
{
form2.Show(dockPanel1, DockState.DockBottom);
}
else
{
form2 = new Form2();
form2.Show(dockPanel1, DockState.DockBottom);
}
if (form3 != null && !form3.IsDisposed)
{
form3.Show(dockPanel1, DockState.DockLeft);
}
else
{
// frmDisplay = new FrmDisplay(1);
form3 = new Form3();
form3.Show(dockPanel1, DockState.DockLeft);
}
}
/// <summary>
/// 保存当前布局
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void save()
{
dockPanel1.SaveAsXml(m_DockPath);
}
}
}