記得我之前發表過一篇文章《Winform應用程式實現通用遮罩層》,是實現了透明遮罩的消息視窗,功能側重點在動圖顯示+消息提醒,效果看上去比較的炫,而本篇我又來重新設計通用消息視窗,功能重點在於消息提醒、進度報告,當然如果大家時間,可以將兩種相結合,那樣就會更完美了,我這裡仍是以實現功能為主,由於代碼
記得我之前發表過一篇文章《Winform應用程式實現通用遮罩層》,是實現了透明遮罩的消息視窗,功能側重點在動圖顯示+消息提醒,效果看上去比較的炫,而本篇我又來重新設計通用消息視窗,功能重點在於消息提醒、進度報告,當然如果大家時間,可以將兩種相結合,那樣就會更完美了,我這裡仍是以實現功能為主,由於代碼相對簡單,我就直接貼上所有代碼,大家可以直接複製到本地測試,若發現問題可自行改正或反饋給我,我來完善,謝謝!
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication1 { /// <summary> /// 等待視窗:用於處理耗時工作時,友好顯示消息視窗 /// 作者:Zuowenjun /// 日期:2016-1-29 /// 網址:http://www.zuowenjun.cn /// </summary> public partial class FRM_Waitting : Form { private SynchronizationContext formContext; public string Message { get { return labMessage.Text; } set { labMessage.Text = value; } } public Action<WaittingForWorkObject> WorkAction { get; set; } public object WorkActionParam { get; set; } public Exception WorkException { get; private set; } public class WaittingForWorkObject { private SendOrPostCallback UpdateMessageAction = null; public SynchronizationContext Context { get; private set; } public object UserData { get; private set; } public void UpdateMessage(string msg) { this.Context.Post(UpdateMessageAction, msg); } public WaittingForWorkObject(FRM_Waitting parentForm) { this.Context = parentForm.formContext; this.UserData = parentForm.WorkActionParam; this.UpdateMessageAction = delegate(object o) { parentForm.Message = o.ToString(); }; } } public static void WaittingForWork(Action<WaittingForWorkObject> workAction, object workParam = null, string text = "請稍候", string message = "系統處理中,請稍候...") { var waittingForm = new FRM_Waitting(text, message, workAction, workParam); waittingForm.ShowDialog(); if (waittingForm.WorkException != null) { throw waittingForm.WorkException; } } public FRM_Waitting() { InitializeComponent(); } public FRM_Waitting(string text, string message, Action<WaittingForWorkObject> workAction, object workParam = null) : this() { this.Text = text; this.Message = message; this.WorkAction = workAction; this.WorkActionParam = workParam; } private void FRM_Waitting_Load(object sender, EventArgs e) { } private void FRM_Waitting_Shown(object sender, EventArgs e) { formContext = SynchronizationContext.Current; if (WorkAction != null) { Thread workThread = new Thread(DoWork); workThread.IsBackground = true; workThread.Start(); } } private void DoWork() { try { var wfObject = new WaittingForWorkObject(this); WorkAction(wfObject); } catch (Exception ex) { WorkException = ex; } formContext.Send(delegate(object o) { this.Close(); }, null); } } }
以下是系統自動生成的代碼:
namespace WindowsFormsApplication1 { partial class FRM_Waitting { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.labMessage = new System.Windows.Forms.Label(); this.SuspendLayout(); // // labMessage // this.labMessage.Dock = System.Windows.Forms.DockStyle.Fill; this.labMessage.Font = new System.Drawing.Font("宋體", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.labMessage.Location = new System.Drawing.Point(0, 0); this.labMessage.Name = "labMessage"; this.labMessage.Padding = new System.Windows.Forms.Padding(5); this.labMessage.Size = new System.Drawing.Size(453, 125); this.labMessage.TabIndex = 0; this.labMessage.Text = "Message"; this.labMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.labMessage.UseWaitCursor = true; // // FRM_Waitting // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(453, 125); this.ControlBox = false; this.Controls.Add(this.labMessage); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "FRM_Waitting"; this.ShowIcon = false; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "FRM_Waitting"; this.UseWaitCursor = true; this.Load += new System.EventHandler(this.FRM_Waitting_Load); this.Shown += new System.EventHandler(this.FRM_Waitting_Shown); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Label labMessage; } }
上述代碼比較簡單,我這裡對消息視窗的實現原理作一個簡要的說明:
1.將耗時處理邏輯代碼封裝到一個委托中(Aciton<FRM_Waitting.WaittingForWorkObject>);
2.獲取當前同步上下文並保存,以便可以跨線程操作UI;
3.創建並運行一個後臺線程,同時將該線程指定到DoWork(工作方法);
4.在DoWork方法中實例化WaittingForWorkObject對象,並傳給1中委托,然後執行委托,這樣耗時的操作都在後臺線程中處理了;
5.在DoWork方法使用try catch捕獲可能存在的異常,若發生異常則保存到WorkException屬性中;
6.執行完成後(無論是否報錯),通過同上下文發送關閉消息視窗指令,使消息視窗關閉;
7.在靜態方法WaittingForWork中判斷WorkException屬性是否不為空,若不為空則重新拋出錯誤,這樣主線程就知道發生了什麼異常;
說明:為了能夠相容.NET 2.0及以上版本,代碼中採用了匿名方法,而非Lambada表達式,實際使用時則可以任意選擇,下麵的測試示例中均提供了新舊兩種代碼寫法,以供大家比較。
以下是各種測試示例:
/// <summary> /// 測試:普通顯示一個消息視窗 /// </summary> private void Test1() { //舊方式(相容.NET2.0及以上) FRM_Waitting.WaittingForWork(delegate(FRM_Waitting.WaittingForWorkObject o) { //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 Thread.Sleep(10 * 1000); }); //新方式(.NET4.0及以上) //FRM_Waitting.WaittingForWork((o) => //{ //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 // Thread.Sleep(10 * 1000); //}); }
效果如下:
/// <summary> /// 測試:普通顯示一個消息視窗,並自定義提示消息並視窗標題 /// </summary> private void Test1_1() { //舊方式(相容.NET2.0及以上) FRM_Waitting.WaittingForWork(delegate(FRM_Waitting.WaittingForWorkObject o) { //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 Thread.Sleep(10 * 1000); },null,"客官請稍候","客官,店小二正在為您拼命處理中,請稍等片刻..."); //新方式(.NET4.0及以上) //FRM_Waitting.WaittingForWork((o) => //{ //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 // Thread.Sleep(10 * 1000); //},null,"客官請稍候","客官,店小二正在為您拼命處理中,請稍等片刻..."); }
效果如下:
/// <summary> /// 測試:普通顯示一個消息視窗,併在後臺線程中拋出錯誤,前臺顯示錯誤信息 /// </summary> private void Test1_2() { try { //舊方式(相容.NET2.0及以上) //FRM_Waitting.WaittingForWork(delegate(FRM_Waitting.WaittingForWorkObject o) //{ // //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 // Thread.Sleep(10 * 1000); // throw new Exception("這裡後臺線程里拋出的錯誤!"); //}); //新方式(.NET4.0及以上) FRM_Waitting.WaittingForWork((o) => { //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 Thread.Sleep(10 * 1000); throw new Exception("這裡後臺線程里拋出的錯誤!"); }); } catch(Exception ex) { MessageBox.Show("發生異常:" + ex.Message); } }
效果如下:
/// <summary> /// 測試:在消息視窗上顯示載入進度 /// </summary> private void Test2() { //舊方式(相容.NET2.0及以上) //FRM_Waitting.WaittingForWork(delegate(FRM_Waitting.WaittingForWorkObject o) //{ //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 // for (int i = 1; i <= 10; i++) // { // Thread.Sleep(1000); // o.UpdateMessage(string.Format("共{0}項,當前已載入{1}項", 10, i)); // } //}); //新方式(.NET4.0及以上) FRM_Waitting.WaittingForWork((o) => { //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 for (int i = 1; i <= 10; i++) { Thread.Sleep(1000); o.UpdateMessage(string.Format("共{0}項,當前已載入{1}項", 10, i)); } }); }
效果如下:
/// <summary> /// 測試:在消息視窗上顯示載入進度,並同時在主視窗(非消息視窗都可以)上更新控制項內容 /// </summary> private void Test3() { //舊方式(相容.NET2.0及以上) FRM_Waitting.WaittingForWork(delegate(FRM_Waitting.WaittingForWorkObject o) { //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 for (int i = 1; i <= 10; i++) { Thread.Sleep(1000); o.UpdateMessage(string.Format("共{0}項,當前已載入{1}項", 10, i)); o.Context.Send(delegate(object d) { this.listBox1.Items.Add(d); }, string.Format("共{0}項,當前已載入{1}項", 10, i)); } }); //新方式(.NET4.0及以上) //FRM_Waitting.WaittingForWork((o) => //{ //在這裡面寫耗時處理邏輯代碼,以下是模擬耗時 // for (int i = 1; i <= 10; i++) // { // Thread.Sleep(1000); // o.UpdateMessage(string.Format("共{0}項,當前已載入{1}項", 10, i)); // o.Context.Send(d => this.listBox1.Items.Add(d), string.Format("共{0}項,當前已載入{1}項", 10, i)); // } //}); }
效果如下:
看完上面的測試效果,大家覺得如何,能否滿足你的日常要求呢,我認為基本都可以滿足的,當然如果發現更多的情況,歡迎在下方評論留言。