您的位置:首页 > 产品设计 > UI/UE

WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用

2012-11-27 18:19 627 查看


WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用



(2012-05-19 23:02:44)


转载▼


标签:


杂谈


本篇介绍Winform程序开发中的布局界面的设计,介绍如何在我的共享软件中使用布局控件"WeifenLuo.WinFormsUI.Docking"。


布局控件"WeifenLuo.WinFormsUI.Docking"是一个非常棒的开源控件,用过的人都深有体会,该控件之强大、美观、不亚于商业控件。而且控件使用也是比较简单的。先看看控件使用的程序界面展示效果。

配电网络可视化管理系统的界面截图:



深田之星送水管理系统网络版的界面截图:



我在几个共享软件都使用了该布局控件,我们先以“深田之星送水管理系统网络版”这款软件为例,介绍如何完成该界面的设计及显示的。

1、首先,我们添加一个主界面窗体,命名为MainForm,该窗体IsMdiContainer设置为True,也就是设置为多文档窗体格式。拖拉布局控件"WeifenLuo.WinFormsUI.Docking.DockPanel"到主窗体MainForm中,并设置下面几个属性:

Dock为Fill、DocumentStyle为DockingMdi、RightToLeftLayout为True。

这几个属性的意思应该不难,Dock就是 覆盖整个MDI窗体的区域,DocumentStyle为多文档类型、RightToLeftLayout是指新打开的窗口都停靠在右边区域。

我们看看设计界面视图如下所示。



2、主界面其实基本上就可以了,另外我们看到“送水管理系统网络版”的界面中有一个左边的工具栏,它其实也是在一个停靠的窗体中的,我们增加一个窗体用来承载相关的工具快捷键按钮展示。命名为MainToolWindow的窗体,继承自WeifenLuo.WinFormsUI.Docking.DockContent.



其中的“HideOnClose”属性很重要,该属性一般设置为True,就是指你关闭窗口时,窗体只是隐藏而不是真的关闭。

左边的窗口MainToolWindow实现停靠的代码是在MainForm的构造函数或者Load函数中加载即可。

mainToolWin.Show(this.dockPanel, DockState.DockLeft);

3、对于工具窗口我们已经完成了,但是主业务窗口还没有做,也就是下面的部分内容。



为了方便,我们定义一个基类窗体,命名为BaseForm,继承自DockContent,如下所示

public class BaseForm : DockContent

然后每个业务窗口继承BaseForm即可。

4、剩下的内容就是如何在主窗体MainForm中展示相关的业务窗口了,展示的代码如下所示
public partial class MainForm : Form

{

#region 属性字段

private MainToolWindow mainToolWin = new MainToolWindow();

private FrmProduct frmProduct = new FrmProduct();

private FrmCustomer frmCustomer = new FrmCustomer();

private FrmOrder frmOrder = new FrmOrder();

private FrmStock frmStock = new FrmStock();

private FrmComingCall frmComingCall = new FrmComingCall();

private FrmDeliving frmDeliving = new FrmDeliving();

private FrmTicketHistory frmHistory = new FrmTicketHistory();

#endregion

public MainForm()

{

InitializeComponent();

SplashScreen.Splasher.Status = "正在展示相关的内容

";

System.Threading.Thread.Sleep(100);

mainToolWin.Show(this.dockPanel, DockState.DockLeft);

frmComingCall.Show(this.dockPanel);

frmDeliving.Show(this.dockPanel);

frmHistory.Show(this.dockPanel);

frmStock.Show(this.dockPanel);

frmProduct.Show(this.dockPanel);

frmCustomer.Show(this.dockPanel);

frmOrder.Show(this.dockPanel);

SplashScreen.Splasher.Status = "初始化完毕

";

System.Threading.Thread.Sleep(50);

SplashScreen.Splasher.Close();

}

















5.下面贴出基本窗口的基本操作事件函数
private void menu_Window_CloseAll_Click(object sender, EventArgs e)

{

CloseAllDocuments();

}

private void menu_Window_CloseOther_Click(object sender, EventArgs e)

{

if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)

{

Form activeMdi = ActiveMdiChild;

foreach (Form form in MdiChildren)

{

if (form != activeMdi)

{

form.Close();

}

}

}

else

{

foreach (IDockContent document in dockPanel.DocumentsToArray())

{

if (!document.DockHandler.IsActivated)

{

document.DockHandler.Close();

}

}

}

}

private DockContent FindDocument(string text)

{

if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)

{

foreach (Form form in MdiChildren)

{

if (form.Text == text)

{

return form as DockContent;

}

}

return null;

}

else

{

foreach (DockContent content in dockPanel.Documents)

{

if (content.DockHandler.TabText == text)

{

return content;

}

}

return null;

}

}

public DockContent ShowContent(string caption, Type formType)

{

DockContent frm = FindDocument(caption);

if (frm == null)

{

frm = ChildWinManagement.LoadMdiForm(Portal.gc.MainDialog, formType) as DockContent;

}

frm.Show(this.dockPanel);

frm.BringToFront();

return frm;

}

public void CloseAllDocuments()

{

if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)

{

foreach (Form form in MdiChildren)

{

form.Close();

}

}

else

{

IDockContent[] documents = dockPanel.DocumentsToArray();

foreach (IDockContent content in documents)

{

content.DockHandler.Close();

}

}

}

最后呈上该控件文件,大家可以下来玩玩。

http://files.cnblogs.com/wuhuacong/WeifenLuo.WinFormsUI.Docking.rar


主要研究技术:代码生成工具、Visio二次开发、送水管理软件等共享软件开发

转载请注明出处:


撰写人:伍华聪 http://www.iqidi.com

在上篇文章《WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用》,我介绍了如何在程序界面中使用WeifenLuo.WinFormsUI.Docking这个优秀的布局控件,这款软件目前我还没有找出比他更好的免费控件了,不知WeifenLuo其人如何,不过东西确实不错,赞一个。

在使用这个控件的时候,估计大家都会碰到 这样一个问题,就是当窗口是自动隐藏的时候,好像出来的大小一般比实际的大,感觉不太美观,有没有什么方法可以控制它的呢,答案是当然有了,其实实现起来也很简单。

首先我们来看看其内部机制是如何实现的,因为该控件有一些属性,专门用来控制窗口的比例的。我们在该控件的源码上看到DocingPanel类中有这么一个属性,是用来控制自动隐藏窗口的缩放比例的。
[LocalizedCategory("Category_Docking")]

[LocalizedDescription("DockContent_AutoHidePortion_Description")]

[DefaultValue(0.25)]

public double AutoHidePortion

{

get { return DockHandler.AutoHidePortion; }

set { DockHandler.AutoHidePortion = value; }

}

默认的大小是0.25,这个参数是可以修改的。因为控件提供了一个保存布局状态的方法,它默认是没有保存的,如果需要记住调整的窗口大小(当然这是一种最好的方法),那么只需要加上几段代码即可。

首先我们看保存布局状态的代码。
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)

{

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

if (m_bSaveLayout)

dockPanel.SaveAsXml(configFile);

else if (File.Exists(configFile))

File.Delete(configFile);

}

这样的方法,因为是直接调用控件本身的保存,所以应该比较易懂。我们再看看程序启动的时候,加载还原原有布局信息的时候,是如何的。
private void MainForm_Load(object sender, EventArgs e)

{

//加载布局

m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);

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

if (File.Exists(configFile))

{

dockPanel.LoadFromXml(configFile, m_deserializeDockContent);

}

}

private IDockContent GetContentFromPersistString(string persistString)

{

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

return mainToolWin;

else if (persistString == typeof(FrmStatus).ToString())

return mainStatus;

else if (persistString == typeof(FrmRoomView).ToString())

return frmRoomView;

else

return null;

}

这样,我们就可以实现布局由用户调整,而不需要怕每次都有那么一点大,不雅观了。

我们看程序的根目录下面生成了一个文件,叫做DockPanel.config, 我们看看就知道里面的布局状态参数了,其中的AutoHidePortion,我们通过自动调整界面,它也就会跟着变化的了,如下面的那个AutoHidePortion="0.179554494828958" 就是我调整后的大小。
<Contents Count="7">

<Content ID="0" PersistString="DockSample.DummyToolbox" AutoHidePortion="0.25" IsHidden="True" IsFloat="True" />

<Content ID="1" PersistString="DockSample.DummySolutionExplorer" AutoHidePortion="0.25" IsHidden="False" IsFloat="False" />

<Content ID="2" PersistString="DockSample.DummyPropertyWindow" AutoHidePortion="0.25" IsHidden="False" IsFloat="False" />

<Content ID="3" PersistString="DockSample.DummyOutputWindow" AutoHidePortion="0.179554494828958" IsHidden="False" IsFloat="False" />

<Content ID="4" PersistString="DockSample.DummyTaskList" AutoHidePortion="0.25" IsHidden="True" IsFloat="True" />

<Content ID="5" PersistString="DockSample.DummyDoc,,Document1" AutoHidePortion="0.25" IsHidden="False" IsFloat="False" />

<Content ID="6" PersistString="DockSample.DummyDoc,,Document2" AutoHidePortion="0.25" IsHidden="False" IsFloat="False" />

</Contents>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐