数据加载提示窗口
以上就是程序执行时,所呈现的效果,这是经过自己摸索出来的一种解决方案,在网上也搜索过,但是有不少人在问,却没有多少明确的答案。所以在这里我把自己成功的方案给大伙share一下,我想应该有达人们比我有更好的法子。但我这已经完全实现了想要的效果,所以就在此抛出来,以此达到抛砖引玉的效果,如有更好的方案,希望与我交流。
以下就贴上我的代码:功能是写在dll里的,所以比较通用!
下面是我的BaseFunc.dll里的类文件Func.cs的代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.ComponentModel; namespace BaseFunc { public class Func { private static BackgroundWorker m_oBackgroundWorker = null; private static FormLoadingData m_oFormLoadingData = null; public static void BeginMessage(string strMessageText) { if(m_oBackgroundWorker==null) m_oBackgroundWorker = new BackgroundWorker(); if (!m_oBackgroundWorker.IsBusy) { m_oBackgroundWorker.DoWork += new DoWorkEventHandler(m_oBackgroundWorker_DoWork); m_oBackgroundWorker.RunWorkerAsync(strMessageText); } else { if (m_oFormLoadingData != null) { m_oFormLoadingData.MessageText = strMessageText; } } } static void m_oBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { ShowLoadingForm(e.Argument.ToString()); } public static void EndMessage() { CloseLoadingForm(); } private static void ShowLoadingForm(string strMessageText) { m_oFormLoadingData = new FormLoadingData(); m_oFormLoadingData.MessageText = strMessageText; m_oFormLoadingData.TopMost = true; m_oFormLoadingData.ShowDialog(); } private static void CloseLoadingForm() { try { if (m_oFormLoadingData.InvokeRequired) { MethodInvoker callback = new MethodInvoker(CloseLoadingForm); m_oFormLoadingData.Invoke(callback); } else { m_oFormLoadingData.Close(); m_oFormLoadingData = null; } } catch { } m_oBackgroundWorker = null; } } }
接下来是我的BaseFunc.dll里的窗体文件(属性设置如下:)
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.ShowIcon = false;
this.ShowInTaskbar = false;
Form里主要代码如下:
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 BaseFunc { public partial class FormLoadingData : Form { private delegate void SetMessageTextCallback(string strMessageText); private Timer m_oSetMessageTimer = null; private string m_strMessageText = "数据加载中,请稍候..."; public FormLoadingData() { InitializeComponent(); try { m_oSetMessageTimer = new Timer(this.Container); m_oSetMessageTimer.Tick += new EventHandler(m_oSetMessageTimer_Tick); m_oSetMessageTimer.Interval = 500; m_oSetMessageTimer.Start(); } catch { } } void m_oSetMessageTimer_Tick(object sender, EventArgs e) { SetMessageText(m_strMessageText); this.lblMsgText.Refresh(); } public string MessageText { get { return this.lblMsgText.Text; } set { m_strMessageText = value; } } private void SetMessageText(string strMessageText) { try { if (this.lblMsgText.InvokeRequired) { SetMessageTextCallback callback = new SetMessageTextCallback(SetMessageText); this.lblMsgText.Invoke(callback, new object[] { strMessageText }); } else { this.lblMsgText.Text = strMessageText; } } catch { } } } }
以下是调用代码:
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.Runtime.InteropServices; using BaseFunc; namespace FormTest { public partial class Form1 : Form { [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern void Sleep(int dwMilliseconds); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Func.BeginMessage("正在加载数据,请稍候。。。"); //此处模拟耗时操作 Sleep(5*1000); Form2 frm = new Form2(); frm.Show(); Func.EndMessage(); } } }
最后附上工程文件排列图:
转载于:https://www.cnblogs.com/yulinlover/archive/2009/02/10/1911861.html