classShaderBox { privateForm _shader; privateForm _parent; privateForm _child; publicShaderBox(Form parent, Form child) { _parent =parent; _child =child; _shader = newForm() { Owner =_parent, Location =_parent.Location, Size =_parent.Size, BackColor =System.Drawing.Color.DimGray, //DoubleBuffered = true, FormBorderStyle =FormBorderStyle.None, Opacity = 0.8D, ShowInTaskbar = false, StartPosition =FormStartPosition.CenterParent }; _child.Owner =_shader; _child.MaximumSize = new System.Drawing.Size(_shader.Width * 80 / 100, _shader.Height * 80 / 100); _child.Font =_parent.Font; _shader.Shown +=Shader_Shown; } public voidShow() { _shader.Show(); _shader.Location =_parent.Location; _shader.BringToFront(); } public voidHide() { _shader.Hide(); } public voidClose() { _shader.Close(); } private void Shader_Shown(objectsender, EventArgs e) { _child.Show(); int x = _shader.Left + (_shader.Width - _child.Width) / 2; int y = _shader.Top + (_shader.Height - _child.Height) / 2; _child.Location = newSystem.Drawing.Point(x, y); _child.BringToFront(); } } classWaitBox : Form { private Label _lblMsg = null; private Timer _timer = null; private Label _lblElapsed = null; private long _elapsed = 0; /// <summary> ///消息 /// </summary> public stringMsg { get =>_lblMsg.Text; set => _lblMsg.Text =value; } publicWaitBox() { Init(); FontChanged +=WaitBox_FontChanged; } private void WaitBox_FontChanged(objectsender, EventArgs e) { _lblMsg.Font =Font; } private voidInit() { Width = 400; Height = 70; FormBorderStyle =FormBorderStyle.None; AutoScaleMode =AutoScaleMode.None; ShowInTaskbar = false; Shown +=WaitBox_Shown; _lblElapsed = newLabel { Dock =DockStyle.Top, TextAlign =ContentAlignment.MiddleRight, Text = "", ForeColor =Color.Blue, Font = newFont(SystemFonts.DefaultFont, FontStyle.Regular), Parent = this}; Controls.Add(_lblElapsed); _lblMsg = newLabel { Dock =DockStyle.Fill, TextAlign =ContentAlignment.MiddleCenter, Text = "请等待...", Parent = this}; Controls.Add(_lblMsg); _timer = newTimer(); _timer.Interval = 1000; _timer.Tick +=_timer_Tick; } private void WaitBox_Shown(objectsender, EventArgs e) { _timer.Start(); } private void _timer_Tick(objectsender, EventArgs e) { _elapsed++; _lblElapsed.Text = string.Format("{0}s", _elapsed); } } classWaitHelper { private static ShaderBox _shaderBox = null; private static WaitBox _childBox = null; /// <summary> ///等待开始 /// </summary> /// <param name="parent">父窗体</param> /// <param name="msg">提示消息</param> public static void WaitBegin(Form parent, stringmsg) { _childBox = newWaitBox { Msg =msg }; _shaderBox = newShaderBox(parent, _childBox); _shaderBox.Show(); Application.DoEvents(); } /// <summary> ///实时更新消息 /// </summary> /// <param name="msg"></param> public static void WaitUpdate(stringmsg) { _childBox.Msg =msg; } /// <summary> ///等待结束 /// </summary> public static voidWaitEnd() { _shaderBox.Close(); _shaderBox = null; _childBox = null; } }
调用:
private void button1_Click(objectsender, EventArgs e) { WaitHelper.WaitBegin(this, "你好,我的朋友!"); for (int q = 0; q < 100000; q++) { textBox1.Text =q.ToString(); Application.DoEvents();//实时响应文本框中的值 //WaitHelper.WaitUpdate(q.ToString()); } WaitHelper.WaitEnd(); }
效果: