WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用
(一)
編寫人:CC阿爸
2015-1-28
在伍華聰的博客中,看到布局控件"WeifenLuo.WinFormsUI.Docking",發現的確是一個非常棒的開源控件,用過的人都深有體會,該控件之強大、美觀、不亞於商業控件。而且控件使用也是比較簡單的今天在這里,我想與大家一起分這一偉大的控件。有興趣的同學,可以一同探討與學習一下,否則就略過吧。
一、引用方法:
1.建立一個WinForm工程,默認生成了一個WinForm窗體。
2.引用—>添加引用—>瀏覽—>weiFenLuo.winFormsUI.Docking.dll。
3.窗體屬性IsMdiContainer:True。
4.工具箱—>右鍵—>選擇項—>.net組件—>瀏覽—>weiFenLuo.winFormsUI.Docking.dll—>在工具箱出現dockPanel。
5.將dockPanel拖到窗體上,設置Dock屬性,我設置的是:Fill。在這里要注意,在先增加菜單工具條,后增加dockpanel否則,會出現布局介面顯示不全的問題。
以下為具體的設計介面:
左側加入一窗體,並設計成outlookbar的樣式。它其實也是在一個停靠的窗體中的,繼承自WeifenLuo.WinFormsUI.Docking.DockContent
二、加入其它兩個控件配合介面的設計: (UtilityLibrary+ IrisSkin2)
UtilityLibrary.dll為可以產生outlookbar這樣的效果
IrisSkin2.dll為引入皮膚控件
三.設計完成后的介面如下:
四.其它部分就是代碼部分:
private string m_strConfigFile;
private DeserializeDockContent m_deserializeDockContent;
public frmMain()
{
InitializeComponent();
InitializeLeftBar();
}
private void InitializeLeftBar()
{
m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
frmTemp = this;
m_strConfigFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), " DockPanel.config ");
m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
}
private IDockContent GetContentFromPersistString( string persistString)
{
if (persistString == typeof(frmLeftBar).ToString())
{
return frmLeftBar;
}
else
{
// 可以加的其它子窗體
return null;
}
}
2.加載主窗口
private void frmMain_Load( object sender, EventArgs e)
{
globalcolor = System.Drawing.Color.FromArgb(Convert.ToInt32(W1.LoadXmlFileValue( " config.xml ", " Color ", " UserColor ")));
globalcolor2 = System.Drawing.Color.FromArgb(Convert.ToInt32(W1.LoadXmlFileValue( " config.xml ", " Color ", " IMColor ")));
CreditControl = Convert.ToBoolean(C_BaseInfo.GetsysConf().Tables[ 0].Rows[ 0][ " CreditControl "]);
// 設置時間和日期
tssl1.Text = " 今天日期: " + DateTime.Now.ToString( " yyyy-MM-dd ");
tssl2.Text = " 登錄時間: " + System.DateTime.Now.ToLongTimeString();
tsslLoginUser.Text = " 當前用戶: " + " " + frmLogin.C_UserInfo.SysUser;
MenuStrip ms = (MenuStrip) this.Controls[ " menuStrip1 "];
ArrayList arr = new ArrayList();
dsright = C_BaseInfo.UserRight(frmLogin.C_UserInfo);
GetMenuAllName(arr, null, 0, ms); // 調用遞歸函數
if (File.Exists(m_strConfigFile))
{
dockPanel1.LoadFromXml(m_strConfigFile, m_deserializeDockContent);
}
frmLeftBar.Show( this.dockPanel1, DockState.DockLeft);
this.dockPanel1.BackgroundImage = global::ECM.Properties.Resources.cable_16_92;
}
3.顯示子窗口
private ECM.Purchase.frmPO frmpo = null;
private void mnuPO_Click( object sender, EventArgs e)
{
if (FindFormName( " frmPO ") == null)
{
frmpo = new ECM.Purchase.frmPO( this);
frmpo.MdiParent = this;
frmpo.Show(frmMain.frmTemp.dockPanel1);
frmpo.Focus();
}
else
{
Form f = FindFormName( " frmPO ") as Form;
f.Focus();
}
}
以下為其它同學編寫有關該控件的技術文檔,供大家參考
http://www.cnblogs.com/wuhuacong/archive/2009/07/09/1520082.html
http://www.cnblogs.com/luomingui/archive/2013/09/19/3329763.html
歡迎加入技術分享群!!!!