吉吉于

free

WeifenLuo.WinFormsUI.Docking.dll之保存界面布局(C#)

之前用WeifenLuo.WinFormsUI.Docking.dll布局学生管理系统,由于当时时间紧,没涉及到布局保存,今儿把代码贴出来,供日后引用。

引用之后会在程序目录自动生成DockPanel.config文件,可通过修改此文件改变布局。

下载

布局:

[][1]

代码:

01 /*
02 * 由SharpDevelop创建。
03 * 用户: Flowerowl
04 * 日期: 2012/2/5
05 * 时间: 14:28
06 *
07 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
08 */
09 using System;
10 using System.Collections.Generic;
11 using System.Drawing;
12 using System.Windows.Forms;
13 using WeifenLuo.WinFormsUI.Docking;
14 using System.IO;
15 namespace weifenluo_config
16 {
17     /// </span>
18     /// Description of MainForm.
19     /// </summary>
20     public partial class MainForm:WeifenLuo.WinFormsUI.Docking.DockContent
21     {
22         public MainForm()
23         {
24
25             InitializeComponent();
26
27         }
28           private bool z_SaveLayout= true;
29           private DeserializeDockContent   z_deserializeDockContent;
30           Form1 z=new Form1();
31           Form2 x=new Form2();
32           Form3 y=new Form3();
33         void MainFormLoad(object sender, EventArgs e)
34         {
35
36
37             z_deserializeDockContent=new DeserializeDockContent(GetContentFormPersistString);
38             string configFile=Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),“DockPanel.config”);
39             if(File.Exists(configFile))
40             {
41                 dockPanel1.LoadFromXml(configFile,z_deserializeDockContent);
42             }
43         }
44
45         void MainFormFormClosing(object sender, FormClosingEventArgs e)
46         {
47             string configFile=Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),“DockPanel.config”);
48             if(z_SaveLayout)
49                 dockPanel1.SaveAsXml(configFile);
50             else if(File.Exists(configFile))
51                 File.Delete(configFile);
52         }
53         private IDockContent GetContentFormPersistString(string persistString)
54         {
55             if(persistString ==typeof(Form1).ToString())
56                 return z;
57             else if(persistString==typeof(Form2).ToString())
58                 return x;
59             else if(persistString==typeof(Form3).ToString())
60                 return y;
61             else
62                 return null;
63         }
64     }
65 } </div> config文件:
01
02 <!–DockPanel configuration file. Author: Weifen Luo, all rights reserved.–>
03 <!–!!! AUTOMATICALLY GENERATED FILE. DO NOT MODIFY !!!–>
04 <DockPanel FormatVersion=“1.0″ DockLeftPortion=“0.25″ DockRightPortion=“0.25″ DockTopPortion=“0.25″ DockBottomPortion=“0.25″ ActiveDocumentPane=“0″ ActivePane=“0″>
05   <Contents Count=“3″>
06     <Content ID=“0″ PersistString=“weifenluo_config.Form1″ AutoHidePortion=“0.25″ IsHidden=“False” IsFloat=“False” />
07     <Content ID=“1″ PersistString=“weifenluo_config.Form2″ AutoHidePortion=“0.25″ IsHidden=“False” IsFloat=“False” />
08     <Content ID=“2″ PersistString=“weifenluo_config.Form3″ AutoHidePortion=“0.25″ IsHidden=“False” IsFloat=“False” />
09   </Contents>
10   <Panes Count=“1″>
11     <Pane ID=“0″ DockState=“Document” ActiveContent=“2″>
12       <Contents Count=“3″>
13         <Content ID=“0″ RefID=“0″ />
14         <Content ID=“1″ RefID=“1″ />
15         <Content ID=“2″ RefID=“2″ />
16       </Contents>
17     </Pane>
18   </Panes>
19   </span>
20     <DockWindow ID=“0″ DockState=“Document” ZOrderIndex=“0″>
21       <NestedPanes Count=“1″>
22         <Pane ID=“0″ RefID=“0″ PrevPane=“-1″ Alignment=“Right” Proportion=“0.5″ />
23       </NestedPanes>
24     </DockWindow>
25     <DockWindow ID=“1″ DockState=“DockLeft” ZOrderIndex=“2″>
26       <NestedPanes Count=“0″ />
27     </DockWindow>
28     <DockWindow ID=“2″ DockState=“DockRight” ZOrderIndex=“3″>
29       <NestedPanes Count=“0″ />
30     </DockWindow>
31     <DockWindow ID=“3″ DockState=“DockTop” ZOrderIndex=“4″>
32       <NestedPanes Count=“0″ />
33     </DockWindow>
34     <DockWindow ID=“4″ DockState=“DockBottom” ZOrderIndex=“5″>
35       <NestedPanes Count=“0″ />
36     </DockWindow>
37   </DockWindows>
38   <FloatWindows Count=“0″ />
39 </DockPanel> </div> 转载请注明:[于哲的博客][2] » [WeifenLuo.WinFormsUI.Docking.dll之保存界面布局(C#)][3] [1]: http://lazynight.me/wp-content/uploads/2012/02/123123123.jpg [2]: http://lazynight.me [3]: http://lazynight.me/1193.html