Dockpanel 实现浮动和多标签窗体解决方案(转)
首先,下载Dockpanel suite,在sf上能下到最新的2.3.1版本的dll,和demo.
http://sourceforge.net/projects/dockpanelsuite/
下面介绍一下dockpanel suite的使用过程:
1,在主窗体中添加dockpanel控件,并把设置主窗体 IsMdiContainer=true。不然在向主窗体中添加标签窗体时,会报异常:Invalid parent form. When using DockingMdi or SystemMdi document style, the DockPanel control must be the child control of the main MDI container form.
并把设置dockpanel 的documentstyle :dockPanel.DocumentStyle = DocumentStyle.DockingMdi.(注:documentstyle 有四种,要实现浮动和多标签效果可以使用:DockingMdi 或DockingSdi,在下面的demo中可以测试,没有看出二者的区别,还望高手指教)
2,加载Dockpanel 的配置文件。Dockpanel suite 是基于配置文件的,其配置文件Dockpanel.config可以放置到指定的位置。在主窗体的load 事件中要做加载配置文件的工作。需要执行配置文件的路径,并使用dockpanel 的LoadFromXml方法加载需要显示的浮动窗体,在initDockpanel 中自定义装载的哪些浮动窗体。
| 1 | private void frmMain_Load(object sender, EventArgs e) |
| 2 | { |
| 3 | this.dockPanel.DocumentStyle = DocumentStyle.DockingMdi; |
| 4 | this.m_DockPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config"); |
| 5 | this.InitDockPanel(); |
| 6 | } |
| 7 | |
| 8 | /// <summary> |
| 9 | /// 按照配置文件初始化Dockpanel |
| 10 | /// </summary> |
| 11 | private void InitDockPanel() |
| 12 | { |
| 13 | try |
| 14 | { |
| 15 | //根据配置文件动态加载浮动窗体 |
| 16 | this.dockPanel.LoadFromXml(this.m_DockPath, delegate(string persistString) |
| 17 | { |
| 18 | if (persistString == typeof(FrmSolution).ToString()) |
| 19 | { |
| 20 | return FrmSolution.GetInstance(); |
| 21 | } |
| 22 | if (persistString == typeof(frmProperty).ToString()) |
| 23 | { |
| 24 | return frmProperty.GetInstance(); |
| 25 | } |
| 26 | if (persistString == typeof(FrmToolBox).ToString()) |
| 27 | { |
| 28 | return FrmToolBox.GetInstance(); |
| 29 | } |
| 30 | if (persistString == typeof(FrmOutput).ToString()) |
| 31 | { |
| 32 | return FrmOutput.GetInstance(); |
| 33 | } |
| 34 | return null; |
| 35 | }); |
| 36 | } |
| 37 | catch (Exception) |
| 38 | { |
| 39 | //配置文件不存在或配置文件有问题时 按系统默认规则加载子窗体 |
| 40 | FrmSolution.GetInstance().Show(this.dockPanel, AppConfig.ms_FrmSolutionState); |
| 41 | frmProperty.GetInstance().Show(this.dockPanel, AppConfig.ms_FrmProState); |
| 42 | FrmToolBox.GetInstance().Show(this.dockPanel, AppConfig.ms_FrmToolBoxState); |
| 43 | FrmOutput.GetInstance().Show(this.dockPanel, AppConfig.ms_FrmOutPutState); |
| 44 | } |
| 45 | } |
注:在此加入的特殊处理,来改进Dockpanel 的加载,如果dockpanel.config不存在(不小心删除了),可以按照Appconfig中预定义的dockstate显示。
3,浮动窗体和多标签窗体
浮动窗体和标签窗体需要继承自DockContent,为了保证在关闭某一浮动窗体之后,再打开时能够在原位置显示,要对浮动窗体处理,处理窗体的DockstateChanged事件。代码如下
| 1 | /// <summary> |
| 2 | /// 标签窗体dock位置改变,记录到公共类 |
| 3 | /// </summary> |
| 4 | /// <param name="sender"></param> |
| 5 | /// <param name="e"></param> |
| 6 | private void frmServer_DockStateChanged(object sender, EventArgs e) |
| 7 | { |
| 8 | //关闭时(dockstate为unknown) 不把dockstate保存 |
| 9 | if (Instance != null) |
| 10 | { |
| 11 | if (this.DockState == DockState.Unknown || this.DockState == DockState.Hidden) |
| 12 | { |
| 13 | return; |
| 14 | } |
| 15 | AppConfig.ms_FrmSolutionState = this.DockState; |
| 16 | } |
| 17 | } |
注意:两种dockstate:unknown ,hidden 不能记录,这两种状态加载时会出现异常。
浮动窗体使用:frmServer.Show(this.dockPanel, dockstate);
标签窗体使用:dummyDoc.Show(dockPanel);
4,保存浮动窗体和标签窗体的状态
如果下次打开程序时,浮动窗体的显示位置和关闭时一致。可以在主窗体的frmMain_FormClosing事件中调用:
dockPanel.SaveAsXml(this.m_DockPath)
以上4点是搭建基于dockpanel 浮动和多标签窗体系统 的主要步骤。

浙公网安备 33010602011771号