利用DockPanel与C#制作窗体浮动和停靠(vs2010)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
namespace FloatingForm
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private string m_DockPath = string.Empty;
private void Form1_Load(object sender, EventArgs e)
{
this.DockPanel1.DocumentStyle = DocumentStyle.DockingMdi;
this.m_DockPath=Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),"DockPanel.config");
this.InitDockPanel();
this.StatusBar.Items.Add("就绪"); //这是状态栏的,可要可不要,不影响!
}
#region 按照配置文件初始化Dockpanel
private void InitDockPanel()
{
try
{
//根据配置文件动态加载浮动窗体
this.DockPanel1.LoadFromXml(this.m_DockPath, delegate(stringpersistString)
{
//功能窗体
if (persistString ==typeof(frmFunction).ToString())
{
returnfrmFunction.GetInstance();
}
//主框架之外的窗体不显示
return null;
});
}
catch (Exception)
{
// 配置文件不存在或配置文件有问题时按系统默认规则加载子窗体
frmFunction.GetInstance().Show(this.DockPanel1);
}
}
#endregion
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
//为了下次打开程序时,浮动窗体的显示位置和关闭时一致,
DockPanel1.SaveAsXml(this.m_DockPath);
}
catch (Exception ex)
{
MessageBox.Show("保存Dockpanel配置文件失败," + ex.Message);
return;
}
}
private void dockPanel1ToolStripMenuItem_Click(object sender, EventArgse)
{
frmFunction frmFun = frmFunction.GetInstance();
frmFun.Show(this.DockPanel1, AppConfig.ms_FrmFunction);
this.StatusBar.Items[0].Text = frmFun.Text;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
usingSystem.Windows.Forms;
usingWeifenLuo.WinFormsUI.Docking;
namespace FloatingForm
{
public partial class frmFunction : DockContent
{
private static frmFunctionInstance;
public frmFunction()
{
InitializeComponent();
}
public static frmFunction GetInstance()
{
if (Instance == null)
{
Instance = new frmFunction();
}
return Instance;
}
//为了保证在关闭某一浮动窗体之后,再打开时能够在原位置显示,要对浮动窗体处理,处理窗体的DockstateChanged事件,标签窗体dock位置改变,记录到公共类
private void FrmFunction_DockStateChanged(object sender, EventArgs e)
{
//关闭时(dockstate为unknown) 不把dockstate保存
if (Instance != null)
{
if (this.DockState ==DockState.Unknown || this.DockState == DockState.Hidden)
{
return;
}
AppConfig.ms_FrmFunction =this.DockState;
}
}
private void FrmFunction_FormClosing(object sender, FormClosingEventArgse)
{
Instance = null; // 否则下次打开时报错,提示“无法访问已释放对象”
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;
namespace FloatingForm
{
class AppConfig
{
public static DockState ms_FrmFunction =DockState.DockLeft; // 功能窗体,左端停靠
}
}
问题(1)描述:
vs2010添加WeifenLuo.WinFormsUI.Docking.DockPanel.dll文件后,从工具栏中添加DockPanel控件时报错,提示【类型 Universe 无法解析程序集: System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a。】
解决方法:
打开【项目】的FloatingForm属性,选择【应用程序】,,修改【目标Framework(所有配置)】选项,在下拉框选项中选择【.net Framework 4】即可。
以上我个人看别人博客之后,在自己动手操作后的个人总结,主要写了一些主要步骤和注意事项而已,如有纰漏,欢迎指教。谢谢!