需求:
1.四个窗体 MainToolWindow(左边工具栏),MainForom(主窗体),form1(常规窗体)
form2(测试窗体)
2. 需要两个文件
UtilityLibrary.dll
WeifenLuo.WinFormsUI.Docking.dll
具体步骤如下:
1.设置MainForm的ISMdiContainer为True;
在MainForm中引用WeifenLuo.WinFormsUI.Docking.dll,然后在工具栏点选项卡,
添加控件dockPanel1拖至窗体,
设置DOCK为Fill
RightToLeftLayOut为True,
2.在MianToolWindow引用UtilityLibrary.dll,然后在工具栏点选项卡,
添加控件outlookBar1至窗体,再拖一个ImageList1,在Images里加入图片
具体代码如下:
MainForm.cs
public partial class MainForm : Form
{
#region 属性字段
private MainToolWindow mainToolWin = new MainToolWindow();
#endregion
public MainForm()
{
InitializeComponent();
mainToolWin.Show(this.dockPanel1, DockState.DockLeft);
}
private DockContent FindDocument(string text)
{
if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
{
foreach (Form form in MdiChildren)
{
if (form.Text == text)
{
return form as DockContent;
}
}
return null;
}
else
{
foreach (DockContent content in dockPanel1.Documents)
{
if (content.DockHandler.TabText == text)
{
return content;
}
}
return null;
}
}
public DockContent ShowContent(string caption, Type formType)
{
DockContent frm = FindDocument(caption);
if (frm == null)
{
frm = Activator.CreateInstance(formType) as DockContent;
frm.DockHandler.TabText = caption;
frm.Show(dockPanel1);
}
frm.Show(dockPanel1, DockState.Document);
frm.BringToFront();
return frm;
}
}
MainToolWindow.cs
public partial class MainToolWindow : DockContent
{
public MainToolWindow()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
setBase.Items.Add(new OutlookBarItem("常规",0));
setBase.Items.Add(new OutlookBarItem("测试",0));
outlookBar1.ItemClicked += new OutlookBarItemClickedHandler(OnOutlookBarItemClicked);
this.panel1.Controls.AddRange(new Control[] { outlookBar1 });
}
private void OnOutlookBarItemClicked(OutlookBarBand band, OutlookBarItem item)
{
switch (item.Text)
{
case "常规":
{
Form1.getinstance(DockPanel);
break;
}
case "测试":
{
Form2.getinstance(DockPanel);
break;
}
default:
break;
}
}
}
Fom1.cs的代码:
public partial class Form1 : DockContent
{
public Form1()
{
InitializeComponent();
}
private static Form1 _initialize = null;
public static Form1 getinstance(DockPanel p)
{
if (_initialize == null || _initialize.IsDisposed)
{
_initialize = new Form1();
}
_initialize.Show(p);
_initialize.Activate();
return _initialize;
}
}
form2同Form1,
效果图: