[转载]Winform等待窗口的实现(附源代码)
在开发Winform程序的时候,经常会用到等待窗口(如网络通讯、数据库连接等需要一定时间来执行的操作),这样可以给用户提供更好的体验。
等待窗口的主要功能是一边执行需要等待的操作,一边显示一个等待界面。当执行完毕时等待界面消失。用户可以提前取消操作,还可以设置操作的最大等待时间,若超过指定时间仍没完成操作可结束当前操作。等待窗口的操作处理内容可用λ表达式,在后面的应用实例中可看到使用方法。
实现界面如下图:

等待界面主要包含的部分:
- 等待图片;
- 等待消息文字("正在处理数据,请稍后..."):可自定义;
- 计时器:可设置不显示;
- 取消返回按钮:可设置不显示;
- 另外等待窗口显示和关闭的时候都有渐变的一个简单特效,等待窗口的颜色是在一定范围内随即的。
等待窗口实现代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
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;namespace WinForm_Test{ public partial class frmWaitingBox : Form { #region Properties private int _MaxWaitTime; private int _WaitTime; private bool _CancelEnable; private IAsyncResult _AsyncResult; private EventHandler<EventArgs> _Method; private bool _IsShown = true; private readonly int _EffectCount = 10; private readonly int _EffectTime = 500; /// <summary> /// 控制界面显示的特性 /// </summary> private Timer _Timer; public string Message { get; private set; } public int TimeSpan { get; set; } public bool FormEffectEnable { get; set; } #endregion #region frmWaitingBox public frmWaitingBox(EventHandler<EventArgs> method,int maxWaitTime,string waitMessage,bool cancelEnable,bool timerVisable) { maxWaitTime *= 1000; Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable); } public frmWaitingBox(EventHandler<EventArgs> method) { int maxWaitTime=60*1000; string waitMessage = "正在处理数据,请稍后..."; bool cancelEnable=true; bool timerVisable=true; Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable); } public frmWaitingBox(EventHandler<EventArgs> method, string waitMessage) { int maxWaitTime = 60 * 1000; bool cancelEnable = true; bool timerVisable = true; Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable); } public frmWaitingBox(EventHandler<EventArgs> method, bool cancelEnable, bool timerVisable) { int maxWaitTime = 60*1000; string waitMessage = "正在处理数据,请稍后..."; Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable); } #endregion #region Initialize private void Initialize(EventHandler<EventArgs> method, int maxWaitTime,string waitMessage,bool cancelEnable, bool timerVisable) { InitializeComponent(); //initialize form this.FormBorderStyle = FormBorderStyle.None; this.StartPosition = FormStartPosition.CenterParent; this.ShowInTaskbar = false; Color[] c = GetRandColor(); this.panel1.BackColor = c[0]; this.BackColor = c[1]; this.labMessage.Text = waitMessage; _Timer = new Timer(); _Timer.Interval = _EffectTime/_EffectCount; _Timer.Tick += _Timer_Tick; this.Opacity = 0; FormEffectEnable = true; //para TimeSpan = 500; Message = string.Empty; _CancelEnable = cancelEnable; _MaxWaitTime = maxWaitTime; _WaitTime = 0; _Method = method; this.pictureBoxCancel.Visible = _CancelEnable; this.labTimer.Visible = timerVisable; this.timer1.Interval = TimeSpan; this.timer1.Start(); } #endregion #region Color private Color[] GetRandColor() { int rMax = 248; int rMin = 204; int gMax = 250; int gMin = 215; int bMax = 250; int bMin = 240; Random r = new Random(DateTime.Now.Millisecond); int r1 = r.Next(rMin, rMax); int r2 = r1 + 5; int g1 = r.Next(gMin, gMax); int g2 = g1 + 5; int b1 = r.Next(bMin, bMax); int b2 = b1 + 5; Color c1 = Color.FromArgb(r1, g1, b1); Color c2 = Color.FromArgb(r2, g2, b2); Color[] c = { c1, c2 }; return c; } #endregion #region Events private void btnCancel_Click(object sender, EventArgs e) { this.Message = "您结束了当前操作!"; this.Close(); } private void timer1_Tick(object sender, EventArgs e) { _WaitTime += TimeSpan; this.labTimer.Text = string.Format("{0}秒", _WaitTime / 1000); if (!this._AsyncResult.IsCompleted) { if (_WaitTime > _MaxWaitTime) { Message = string.Format("处理数据超时{0}秒,结束当前操作!", _MaxWaitTime / 1000); this.Close(); } } else { this.Message = string.Empty; this.Close(); } } private void frmWaitingBox_Shown(object sender, EventArgs e) { _AsyncResult = _Method.BeginInvoke(null, null, null, null); //Effect if (FormEffectEnable) { _Timer.Start(); } else this.Opacity = 1; } private void frmWaitingBox_FormClosing(object sender, FormClosingEventArgs e) { if (FormEffectEnable) { if(this.Opacity>=1) e.Cancel = true; _Timer.Start(); } } private void _Timer_Tick(object sender, EventArgs e) { if (_IsShown) { if (this.Opacity >= 1) { _Timer.Stop(); _IsShown = false; } this.Opacity += 1.00 / _EffectCount; } else { if (this.Opacity <= 0) { _Timer.Stop(); _IsShown = true; this.Close(); } this.Opacity -= 1.00 / _EffectCount; } } #endregion }} |
应用实例代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
string res; DataTable dt=null; frmWaitingBox f = new frmWaitingBox((obj,args)=> { Thread.Sleep(5000); string sql = "SELECT * FROM [test].[dbo].[studentInfo]"; SQLHelper sqlHelper=new SQLHelper(); dt = sqlHelper.ExecuteSqlStr(sql,out res); }); f.ShowDialog(this); dataGridView1.DataSource = dt; |
源码下载:下载地址——(http://files.cnblogs.com/anding/WinForm_Test1.rar)
原文地址:http://www.cnblogs.com/anding/archive/2010/10/07/1845251.html
[转载]Winform等待窗口的实现(附源代码)的更多相关文章
- [C#] (原创)进度等待窗口(附:自定义控件的使用)
一.前言 技术没有先进与落后,只有合适与不合适. 在程序当中,经常有耗时较长的操作,为了给用户更好的体验,就需要给用户一个及时的反馈,这种时候就需要用到进度等待窗口. 实现进度等待窗口的技术有很多,比 ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- ASP.NET制作一个简单的等待窗口
前一阵做一个项目,在处理报表的时候时间偏长,客户提出要做出一个等待窗口提示用户等待(页面太久没反映,用户还以为死了呢).在分析这一需求之后,觉得如果要实现像winform应用中的processbar太 ...
- 【转】P2P之UDP穿透NAT的原理与实现(附源代码)
作者:shootingstars (有容乃大,无欲则刚) 日期:2004-5-25 出处:P2P中国(PPcn.net) P2P 之 UDP穿透NAT的原理与实现(附源代码)原创:shootings ...
- WCF技术剖析之二十八:自己动手获取元数据[附源代码下载]
原文:WCF技术剖析之二十八:自己动手获取元数据[附源代码下载] 元数据的发布方式决定了元数据的获取行为,WCF服务元数据架构体系通过ServiceMetadataBehavior实现了基于WS-ME ...
- Java设计模式-代理模式之动态代理(附源代码分析)
Java设计模式-代理模式之动态代理(附源代码分析) 动态代理概念及类图 上一篇中介绍了静态代理,动态代理跟静态代理一个最大的差别就是:动态代理是在执行时刻动态的创建出代理类及其对象. 上篇中的静态代 ...
- c# winform进入窗口后在文本框里的默认焦点
c# winform 设置winform进入窗口后在文本框里的默认焦点 进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这 ...
- c# winform 设置winform进入窗口后在文本框里的默认焦点
c# winform 设置winform进入窗口后在文本框里的默认焦点 进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这个文 ...
- 仿36氪(iOS版附源代码)
前言: 这是我2016年3月开始写的,利用课余时间全心投入的项目,本以为是凭着轻松愉悦的方式来学习的,中途遇到bug解决bug的时候,每天晚上几乎都是写到寝室关灯,还有一次使用Github不当写了五天 ...
随机推荐
- 我终于理解了LISP『代码即数据|数据即代码』的含义
以前我一直不能理解LISP里引用的作用,感觉引用和字符串没什么区别.比如:> (define (func) 'ok) > (func) 'ok 这里把引用ok当做了函数func的返 ...
- uiautomator做自动化的过程
UIautiomator官网地址:http://android.toolib.net/sdk/index.html 1.环境搭建 使用uiautomator需要导入jar包,uiautomator.j ...
- scala学习笔记:高阶函数
scala> def power(y:Double)=(x:Double)=>Math.pow(x,y) warning: there were 1 deprecation warning ...
- ASP生成静态文件编码为UTF-8格式的HTML文件
一般在ASP环境下,运行动生静操作时都用到的是FSO,FSO是专门对文件进行操作的一个组件,FSO的编码属性只有三种,系统默认,Unicode,ASCII,并没有utf-8,所以一般中文系统上使用FS ...
- SQL Server 负载均衡集群方案之Moebius
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...
- SQL Server调优系列进阶篇 - 如何索引调优
前言 上一篇我们分析了数据库中的统计信息的作用,我们已经了解了数据库如何通过统计信息来掌控数据库中各个表的内容分布.不清楚的童鞋可以点击参考. 作为调优系列的文章,数据库的索引肯定是不能少的了,所以本 ...
- ASP.NET Web API 使用记录
WebAPI采用REST架构,用的是无状态的HTTP协议.Web Service则是SOAP协议,比较重量级. 推荐阅读:Difference between WCF and Web API and ...
- 调起MT096的配置过程
FTP::cips\/var/cics_regions/RGCIPS/database/PD/PD.RGCIPS|PD.auto 更加新的PD号(其中的路径指向新的程序ibmp),并修改FTP::ci ...
- CICS定时
F+14个0-----给CICS传参数FM CICSServer svr; svr.GetRetCommData(&pMsgStr, &chMsgType); CICSS ...
- sgu 105 Div 3
一个数能整除3当且仅当各位数之和能整除3. 有了这个规律就好办了, 但是呢,仔细一看, n太大了, 都到 2^31 了.所以简单的模拟肯定不行. 这种貌似像数论的题,一时找不到好办法,就打表! 打表出 ...