DockPanelSuite的基础使用
DockPanel Suite是一个界面布局开源库,在WinForm中实现Visual Studio的布局效果
使用流程
安装主题:2015Theme
拖一个DockPanel到Form1,设置Dock属性为在父容器中停靠
工程里新加一个窗体Form2
将Form2继承类改为DockContent:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace WindowsFormsApp5
{
public partial class Form2 : DockContent
{
public Form2()
{
InitializeComponent();
}
}
}
在form1中进行调用布局
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
Form2 wid1 = new Form2();
Form2 wid2 = new Form2();
Form2 wid3 = new Form2();
Form2 wid4 = new Form2();
Form2 wid5 = new Form2();
Form2 wid6 = new Form2();
Form2 wid7 = new Form2();
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
//改变主题
VS2015LightTheme vs2015 = new VS2015LightTheme();
dockPanel1.Theme = vs2015;
//以各种方式停靠
wid1.Show(dockPanel1, DockState.DockLeft);
wid2.Show(dockPanel1, DockState.DockRight);
wid3.Show(dockPanel1, DockState.DockRight);
wid4.Show(dockPanel1, DockState.Document);
wid5.Show(dockPanel1, DockState.Document);
wid6.Show(dockPanel1, DockState.DockBottom);
wid7.Show(dockPanel1, DockState.Float);
}
}
}
Document布局,文档选项卡布局
默认布局
Form2 form2;
Form3 form3;
private void 默认布局_Click(object sender, EventArgs e)
{
ShowDefaultLayout();
}
/// <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);
}
}
保存当前布局
dockPanel1.SaveAsXml("DockPanel.config");
加载保存布局
FrmShowDisplay frmShowDisplay;
// 加载布局
frmShowDisplay = new FrmShowDisplay(); // 这个必须有
dockPanel1.Visible = true;
if (File.Exists(GlobalParams.dockPanelLayoutConfigPath))
{
dockPanel1.LoadFromXml(GlobalParams.dockPanelLayoutConfigPath, GetContentFromPersistString); //根据配置文件动态加载浮动窗体
}
private IDockContent GetContentFromPersistString(string persistString)
{
if (persistString == typeof(FrmShowDisplay).ToString())
return frmShowDisplay;
return null;
}
// 加载布局
initFormsLayout();
/// <summary>
/// 加载布局
/// </summary>
void initFormsLayout()
{
for (int i = 0; i < 20; i++) {
FrmShowDisplay frmShowDisplay = new FrmShowDisplay();
_dockFormList.Add(frmShowDisplay);
}
dockPanel1.Visible = true;
InitDockPanel();
}
int count_index = 0;
private IDockContent GetDeserializeDockContent(string persistString)
{
var persistList = persistString.Split(new char[] { '.' });//0-1命名空间;2窗体名;3窗体标题;4窗体Guid
var form = _dockFormList[count_index];
count_index++;
return form;
}
public void InitDockPanel()
{
if (File.Exists(GlobalParams.dockPanelLayoutConfigPath))
{
dockPanel1.LoadFromXml(GlobalParams.dockPanelLayoutConfigPath, GetDeserializeDockContent); //根据配置文件动态加载浮动窗体
}
}