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();
}
}
}
WinForm界面开发之"SplashScreen控件"
我们在开发桌面应用程序的时候,由于程序启动比较慢,往往为了提高用户的体验,增加一个闪屏,也就是SplashScreen,好处有:1、让用户看到加载的过程,提高程序的交互响应;2.可以简短展示或者介绍程序的功能或者展示Logo,给客户较深的印象。
本人在开发的共享软件中,对于启动比较慢的程序,也倾向于引入这个控件来展示下,先看看软件启动的时候的效果
中间的那些文字“正在展示相关的内容”可以根据加载的进度显示不同的内容,当然最好简单扼要了,其他的内容你也可以视需要做相应变化,因为这个是一个Form,你想改变什么就改变什么的。
看看闪屏代码如何使用先,首先我们在入口的Main函数中开始,看看代码就知道
public class Portal
{
public static GlobalControl gc = new GlobalControl();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//登陆界面
Logon dlg = new Logon();
dlg.StartPosition = FormStartPosition.CenterScreen;
if (DialogResult.OK == dlg.ShowDialog())
{
if (dlg.bLogin)
{
SplashScreen.Splasher.Show(typeof(SplashScreen.frmSplash));
gc.MainDialog = new MainForm();
gc.MainDialog.StartPosition = FormStartPosition.CenterScreen;
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(gc.MainDialog);
}
}
dlg.Dispose();
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs ex)
{
LogHelper.Error(ex.Exception);
string message = string.Format("{0}\r\n操作发生错误,您需要退出系统么?", ex.Exception.Message);
if (DialogResult.Yes == MessageUtil.ShowYesNoAndError(message))
{
Application.Exit();
}
}
}
上面中最关键的代码是:SplashScreen.Splasher.Show(typeof(SplashScreen.frmSplash));
之所以贴出全部的代码,也是供大家参考如何启动登陆窗口并运行主窗体程序的,上面的GlobalControl类是一个公共类,用来放置一些全局变量或者通用操作的函数。Application_ThreadException是用来在程序运行出错的时候,友好提示一下用户,是否退出,否则有一些莫名其妙的错误,程序没有提示就马上退出的问题,扯远了,言归正传,还是说说如何使用闪屏的功能吧。
上面开启了闪屏的功能后,那么我们可能就要在程序中,根据不同的加载进度显示不同的内容了,看看是如何做到的
记得在MainForm窗体的构造函数中添加相应的闪屏操作代码,如下所示。
public MainForm()
{
InitializeComponent();
SplashScreen.Splasher.Status = "正在展示相关的内容";
System.Threading.Thread.Sleep(100);
..//此处省略部分加载耗时的代码
SplashScreen.Splasher.Status = "初始化完毕";
System.Threading.Thread.Sleep(50);
SplashScreen.Splasher.Close();
}
控件的使用代码就这么多了,其他的就是封装好的控件部分内容了,下面打包放上来,给大家参考使用,源码级的哦,下载了记得顶一下。
提示:控件的背景图片可能不能正常显示,您自己弄一个图片附上去就可以了。
原文地址:http://blog.sina.com.cn/s/blog_5fbf4ff50100lcbn.html

浙公网安备 33010602011771号