界面布局控件WeifenLuo.WinFormsUI.Docking_保存窗体布局

界面布局控件-WeifenLuo.WinFormsUI.Docking_保存窗体布局

//201117

我们在使用WeifenLuo.WinFormsUI.Docking控件控制窗体布局后,可以将当前的控件布局格保存下来,当系统再次重新启动的时候,就显示原来已经保存的页面布局格式。

实现思路:

1) 系统退出前,将当前的页面布局保存下来,以XML文件存放

2) 系统加载后,读取该XML的配置信息,然后根据读取的配置信息,显示页面格局。

实现效果如下所示:

保存前的布局样式,也是系统第一运行后的现实效果

调整后的系统布局

实现步骤:

1) 系统退出前,保存当前的布局,代码如下所示:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"DockPanel.config");

//configFile 配置文件保存的路径

            dockPanel1.SaveAsXml(configFile);

        }

2)窗体加载前的代码

     child_1 b = new child_1();//第一个子窗体

        child_2 c = new child_2();//第二个子窗体

        private DeserializeDockContent ddc;//全局变量

        //辅助函数,这里需要注意的是,有几个字控件,就需要有几个if()语句来判断

        private IDockContent GetContentFromPersistString(string persistString)

        {

            //persistString=命名空间+窗体类的命名,比如:WindowsFormsApplication1+child_1

            if (persistString == typeof(child_1).ToString())

            {

                return b;

            }

            if (persistString == typeof(child_2).ToString())

            {

 

                return c;

            }

            return null;

 

        }

//窗体加载调用的函数

  private void Form1_Load(object sender, EventArgs e)

        {

            ddc = new DeserializeDockContent(GetContentFromPersistString); // 放在后面的话会出错。

            //返回配置文件的路径

            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");

            if (File.Exists(configFile))

            {

//如果配置文件存在,就调用该函数,读取配置文件信息

                dockPanel1.LoadFromXml(configFile, ddc);

            }

            else

            {

//如果第一次运行系统,或者不存在配置文件,运行下面的代码,显示最原始状态的控件显示布局

                b.Show(this.dockPanel1);

                b.DockTo(this.dockPanel1, DockStyle.Left);

                c.Show(this.dockPanel1);

                c.DockTo(this.dockPanel1, DockStyle.Right);

            }

        }

原文地址:https://www.cnblogs.com/xingchen/p/1930276.html