winform弹出消息自动消失
- 弹出消息后,在指定时间毫秒后消失.
- 消息中包含异常消息,自动一直展示,点击关闭显示;
效果如图
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Management;
namespace Whl.Tool
{
public class ShowMsgHelpter
{
static int lastX = 480;
static int lastY = 88;
public static void ShowMsgAutoHide(string msg)
{
Label label = new Label();
label.Text = msg;
label.AutoSize = true;
label.BackColor = System.Drawing.Color.White;
label.Dock = System.Windows.Forms.DockStyle.Fill;
label.Font = new System.Drawing.Font("宋体", 23F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
label.Location = new System.Drawing.Point(2, 2);
label.Margin = new System.Windows.Forms.Padding(20);
label.Name = "label578838";
label.Size = new System.Drawing.Size(316, 51);
label.TabIndex = 999;
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Form frm = new Form();
frm.Padding = new System.Windows.Forms.Padding(20);
frm.Width = 400;
frm.Height = 60;
frm.AutoSize = true;
frm.FormBorderStyle = FormBorderStyle.None;
frm.BackColor = System.Drawing.Color.GreenYellow;
frm.StartPosition = FormStartPosition.Manual;
frm.Location=new Point(lastX, lastY);
frm.ControlBox = false;
frm.ShowInTaskbar = false;
lastX += 100;
lastY += 88;
label.Click += (sendr, ev) =>
{
frm.Close();
};
if (msg.Contains("失败") || msg.Contains("错误") || msg.Contains("异常"))
{
label.ForeColor = System.Drawing.Color.Red;
frm.Click += (sendr, ev) =>
{
frm.Close();
};
}
else
{
System.Timers.Timer timer = new System.Timers.Timer(3333);
timer.AutoReset = false;
timer.Elapsed += (a, e) =>
{
frm.BeginInvoke(new Action(() => {
frm.Close();
}));
};
timer.Start();
}
frm.Controls.Add(label);
frm.Show();
}
}
}