在开发Winform程序的时候,经常会用到等待窗口(如网络通讯、数据库连接等需要一定时间来执行的操作),这样可以给用户提供更好的体验。
等待窗口的主要功能是一边执行需要等待的操作,一边显示一个等待界面。当执行完毕时等待界面消失。用户可以提前取消操作,还可以设置操作的最大等待时间,若超过指定时间仍没完成操作可结束当前操作。等待窗口的操作处理内容可用λ表达式,在后面的应用实例中可看到使用方法。
实现界面如下图:
等待界面主要包含的部分:
- 等待图片;
- 等待消息文字("正在处理数据,请稍后..."):可自定义;
- 计时器:可设置不显示;
- 取消返回按钮:可设置不显示;
- 另外等待窗口显示和关闭的时候都有渐变的一个简单特效,等待窗口的颜色是在一定范围内随即的。
等待窗口实现代码:
001 |
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.ComponentModel; |
004 |
using System.Data; |
005 |
using System.Drawing; |
006 |
using System.Linq; |
007 |
using System.Text; |
008 |
using System.Windows.Forms; |
009 |
|
010 |
namespace WinForm_Test |
011 |
{ |
012 |
public partial class frmWaitingBox : Form |
013 |
{ |
014 |
#region Properties |
015 |
private int _MaxWaitTime; |
016 |
private int _WaitTime; |
017 |
private bool _CancelEnable; |
018 |
private IAsyncResult _AsyncResult; |
019 |
private EventHandler<EventArgs> _Method; |
020 |
private bool _IsShown = true ; |
021 |
private readonly int _EffectCount = 10; |
022 |
private readonly int _EffectTime = 500; |
023 |
/// <summary> |
024 |
/// 控制界面显示的特性 |
025 |
/// </summary> |
026 |
private Timer _Timer; |
027 |
public string Message { get ; private set ; } |
028 |
public int TimeSpan { get ; set ; } |
029 |
public bool FormEffectEnable { get ; set ; } |
030 |
#endregion |
031 |
|
032 |
#region frmWaitingBox |
033 |
public frmWaitingBox(EventHandler<EventArgs> method, int maxWaitTime, string waitMessage, bool cancelEnable, bool timerVisable) |
034 |
{ |
035 |
maxWaitTime *= 1000; |
036 |
Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable); |
037 |
} |
038 |
public frmWaitingBox(EventHandler<EventArgs> method) |
039 |
{ |
040 |
int maxWaitTime=60*1000; |
041 |
string waitMessage = "正在处理数据,请稍后..." ; |
042 |
bool cancelEnable= true ; |
043 |
bool timerVisable= true ; |
044 |
Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable); |
045 |
} |
046 |
public frmWaitingBox(EventHandler<EventArgs> method, string waitMessage) |
047 |
{ |
048 |
int maxWaitTime = 60 * 1000; |
049 |
bool cancelEnable = true ; |
050 |
bool timerVisable = true ; |
051 |
Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable); |
052 |
} |
053 |
public frmWaitingBox(EventHandler<EventArgs> method, bool cancelEnable, bool timerVisable) |
054 |
{ |
055 |
int maxWaitTime = 60*1000; |
056 |
string waitMessage = "正在处理数据,请稍后..." ; |
057 |
Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable); |
058 |
} |
059 |
#endregion |
060 |
|
061 |
#region Initialize |
062 |
private void Initialize(EventHandler<EventArgs> method, int maxWaitTime, string waitMessage, bool cancelEnable, bool timerVisable) |
063 |
{ |
064 |
InitializeComponent(); |
065 |
//initialize form |
066 |
this .FormBorderStyle = FormBorderStyle.None; |
067 |
this .StartPosition = FormStartPosition.CenterParent; |
068 |
this .ShowInTaskbar = false ; |
069 |
Color[] c = GetRandColor(); |
070 |
this .panel1.BackColor = c[0]; |
071 |
this .BackColor = c[1]; |
072 |
this .labMessage.Text = waitMessage; |
073 |
_Timer = new Timer(); |
074 |
_Timer.Interval = _EffectTime/_EffectCount; |
075 |
_Timer.Tick += _Timer_Tick; |
076 |
this .Opacity = 0; |
077 |
FormEffectEnable = true ; |
078 |
//para |
079 |
TimeSpan = 500; |
080 |
Message = string .Empty; |
081 |
_CancelEnable = cancelEnable; |
082 |
_MaxWaitTime = maxWaitTime; |
083 |
_WaitTime = 0; |
084 |
_Method = method; |
085 |
this .pictureBoxCancel.Visible = _CancelEnable; |
086 |
this .labTimer.Visible = timerVisable; |
087 |
this .timer1.Interval = TimeSpan; |
088 |
this .timer1.Start(); |
089 |
} |
090 |
#endregion |
091 |
|
092 |
#region Color |
093 |
private Color[] GetRandColor() |
094 |
{ |
095 |
int rMax = 248; |
096 |
int rMin = 204; |
097 |
int gMax = 250; |
098 |
int gMin = 215; |
099 |
int bMax = 250; |
100 |
int bMin = 240; |
101 |
Random r = new Random(DateTime.Now.Millisecond); |
102 |
int r1 = r.Next(rMin, rMax); |
103 |
int r2 = r1 + 5; |
104 |
int g1 = r.Next(gMin, gMax); |
105 |
int g2 = g1 + 5; |
106 |
int b1 = r.Next(bMin, bMax); |
107 |
int b2 = b1 + 5; |
108 |
Color c1 = Color.FromArgb(r1, g1, b1); |
109 |
Color c2 = Color.FromArgb(r2, g2, b2); |
110 |
Color[] c = { c1, c2 }; |
111 |
return c; |
112 |
} |
113 |
#endregion |
114 |
|
115 |
#region Events |
116 |
private void btnCancel_Click( object sender, EventArgs e) |
117 |
{ |
118 |
this .Message = "您结束了当前操作!" ; |
119 |
this .Close(); |
120 |
} |
121 |
|
122 |
private void timer1_Tick( object sender, EventArgs e) |
123 |
{ |
124 |
_WaitTime += TimeSpan; |
125 |
this .labTimer.Text = string .Format( "{0}秒" , _WaitTime / 1000); |
126 |
if (! this ._AsyncResult.IsCompleted) |
127 |
{ |
128 |
if (_WaitTime > _MaxWaitTime) |
129 |
{ |
130 |
Message = string .Format( "处理数据超时{0}秒,结束当前操作!" , _MaxWaitTime / 1000); |
131 |
this .Close(); |
132 |
} |
133 |
} |
134 |
else |
135 |
{ |
136 |
this .Message = string .Empty; |
137 |
this .Close(); |
138 |
} |
139 |
|
140 |
} |
141 |
|
142 |
private void frmWaitingBox_Shown( object sender, EventArgs e) |
143 |
{ |
144 |
_AsyncResult = _Method.BeginInvoke( null , null , null , null ); |
145 |
//Effect |
146 |
if (FormEffectEnable) |
147 |
{ |
148 |
_Timer.Start(); |
149 |
} |
150 |
else |
151 |
this .Opacity = 1; |
152 |
} |
153 |
private void frmWaitingBox_FormClosing( object sender, FormClosingEventArgs e) |
154 |
{ |
155 |
if (FormEffectEnable) |
156 |
{ |
157 |
if ( this .Opacity>=1) |
158 |
e.Cancel = true ; |
159 |
_Timer.Start(); |
160 |
} |
161 |
} |
162 |
private void _Timer_Tick( object sender, EventArgs e) |
163 |
{ |
164 |
if (_IsShown) |
165 |
{ |
166 |
if ( this .Opacity >= 1) |
167 |
{ |
168 |
_Timer.Stop(); |
169 |
_IsShown = false ; |
170 |
} |
171 |
this .Opacity += 1.00 / _EffectCount; |
172 |
} |
173 |
else |
174 |
{ |
175 |
if ( this .Opacity <= 0) |
176 |
{ |
177 |
_Timer.Stop(); |
178 |
_IsShown = true ; |
179 |
this .Close(); |
180 |
} |
181 |
this .Opacity -= 1.00 / _EffectCount; |
182 |
} |
183 |
} |
184 |
#endregion |
185 |
|
186 |
|
187 |
} |
188 |
} |
应用实例代码:
01 |
string res; |
02 |
DataTable dt= null ; |
03 |
frmWaitingBox f = new frmWaitingBox((obj,args)=> |
04 |
{ |
05 |
Thread.Sleep(5000); |
06 |
string sql = "SELECT * FROM [test].[dbo].[studentInfo]" ; |
07 |
SQLHelper sqlHelper= new SQLHelper(); |
08 |
dt = sqlHelper.ExecuteSqlStr(sql, out res); |
09 |
}); |
10 |
f.ShowDialog( this ); |
11 |
12 |
dataGridView1.DataSource = dt; |
源码下载:下载地址——(https://files.cnblogs.com/anding/WinForm_Test1.rar)