最近在做一个项目,需要用到等待窗体,在DevExpress下面有SplashScreen控件可以使用,同时也有ProgressIndicator控件能用,但是如果没有用Dev开发的话,我们就需要自定义一个等待窗体了。
首先,把截图放上来:
实现的功能比较简单,就是在程序处理 一些耗时比较多的代码时,将Loading窗体展示给用户,并在后台进行执行。
http://blog.csdn.net/ggz631047367/article/details/44646233
http://www.runoob.com/csharp/csharp-delegate.html
http://blog.csdn.net/sjj2011/article/details/7835200
http://blog.csdn.net/testcs_dn/article/details/37671513
LoadingControl.cs代码如下:
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 System.Threading;
namespace ControlToolsLibrary
{
public partial class LoadingControl: Form
{
public delegate void mydelegate();
public mydelegateeventMethod;
private static LoadingControlpLoading = new LoadingControl();
delegate void SetTextCallback(string title,string caption,string description);
delegate void CloseFormCallback();
public LoadingControl()
{
InitializeComponent();
initLoadintForm();
Threadt = new Thread(new ThreadStart(delegateEventMethod));
t.IsBackground= true;
t.Start();
}
private void LoadingControl_FormClosing(object sender,FormClosingEventArgs e)
{
if (!this.IsDisposed)
{
this.Dispose(true);
}
}
private void initLoadintForm(){
this.ControlBox= false; // 设置不出现关闭按钮
this.StartPosition= FormStartPosition.CenterParent;
}
private void delegateEventMethod()
{
eventMethod();
}
public static LoadingControlgetLoading()
{
if (pLoading.IsDisposed)
{
pLoading= new LoadingControl();
return pLoading;
}
else
{
return pLoading;
}
}
//这种方法演示如何在线程安全的模式下调用Windows窗体上的控件。
///<summary>
///设置Loading 窗体的 标题title,标签 caption 和描述 description
///</summary>
///<param name="title">窗口的标题[为空时,取默认值]</param>
///<param name="caption">标签(例如:please wait)[为空时,取默认值]</param>
///<param name="description">描述(例如:正在加载资源...)[为空时,取默认值]</param>
public void SetCaptionAndDescription(string title,string caption,string description)
{
if (this.InvokeRequired&& this.lbl_caption.InvokeRequired &&this.lbl_description.InvokeRequired)
{
SetTextCallbackd = new SetTextCallback(SetCaptionAndDescription);
this.Invoke(d,new object[]{ title,caption, description });
}
else
{
if (!title.Equals("")){
this.Text= title;
}
if (!caption.Equals(""))
{
this.lbl_caption.Text= caption;
}
if (!description.Equals("")){
this.lbl_description.Text= description;
}
}
}
public void CloseLoadingForm()
{
if (this.InvokeRequired)
{
CloseFormCallbackd = new CloseFormCallback(CloseLoadingForm);
this.Invoke(d,new object[]{ });
}
else
{
if (!this.IsDisposed)
{
this.Dispose(true);
}
}
}
public void SetExecuteMethod(mydelegatemethod)
{
this.eventMethod+= method;
}
}
}
下载链接:http://pan.baidu.com/s/1cl9saq