winform弹出消息自动消失
- 弹出消息后,在指定时间毫秒后消失.
- 消息中包含异常消息,自动一直展示,点击关闭显示;
效果如图
![在这里插入图片描述](https://img-blog.csdnimg.cn/c1fb69af00d84945801979cf0f019377.png)
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 Form 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("宋体", 18F, 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.TopMost = true;
frm.Padding = new System.Windows.Forms.Padding(18);
frm.Width = 400;
frm.Height = 60;
frm.AutoSize = true;
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
frm.BackColor = System.Drawing.Color.White;
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(lastX, lastY);
frm.Text = "消息提示";
frm.ShowInTaskbar = false;
frm.MinimizeBox = false;
frm.MaximizeBox = false;
lastX += 33;
lastY += 80;
label.Click += (sendr, ev) =>
{
frm.Close();
};
if (msg.Contains("成功"))
{
label.ForeColor = System.Drawing.Color.Green;
}
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<Form>(k =>
{
k.Close();
}), frm);
};
timer.Start();
}
frm.Controls.Add(label);
frm.Show();
if (Screen.PrimaryScreen.Bounds.Width - frm.Location.X < 700
|| Screen.PrimaryScreen.Bounds.Height - frm.Location.Y<388
)
{
lastX = 480;
lastY = 88;
}
return frm;
}
}
}
调用
ShowMsgHelpter.ShowMsgAutoHide("点击了窗口尺寸变化"+DateTime.Now.Ticks);
Main方法调用:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Whl.Tool;
namespace ZpenSample
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (processes.Length > 1)
{
MessageBox.Show("此程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Thread.Sleep(1000);
Environment.Exit(1);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string msg = string.Join(";", args);
string str1 = msg.Replace("runzhiyuantongapserver://", "");
string[] arr = str1.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 0)
{
Application.Run(ShowMsgHelpter.ShowMsgAutoHide("错误:没有获取到班级id"));
return;
}
Application.Run(new FormZpenSample());
}
}
}