winform弹出消息自动消失

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
{
    /// <summary>
    /// 弹出消息展示,
    /// </summary>
    /// 创建时间:2023-7-7 14:33:05, 
    public class ShowMsgHelpter
    {
        static int lastX = 480;
        static int lastY = 88;


        /// <summary>
        /// 弹出消息展示,消息在3.3秒后自动消失
        /// </summary>
        /// <param name="msg"></param>
        /// 创建时间:2023-7-7 14:53:21, 
        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(2, 2, 2, 0);
            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.SetDesktopLocation(lastX, lastY);
            frm.Location = new Point(lastX, lastY);
            //frm.ControlBox = false;
            frm.Text = "消息提示";
            frm.ShowInTaskbar = false;
            frm.MinimizeBox = false;
            frm.MaximizeBox = false;

            lastX += 33;
            lastY += 80;

            //label点击关闭窗口
            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
    {
  
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [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);
           // msg = "runzhiyuantongapserver://152,lisi,wusong";

            //解析浏览器传递的参数,
            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());
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值