使用效果:
代码:
#region 消息框变量private Timer fadeTimer; // 定义计时器private int fadeSpeed = 2;//淡出速度private NotifyIcon notifyIcon;//气泡通知private int opacityLevel = 10;//不透明度public enum NotificationType{Error,//错误Warning,//警告Info//提示}#endregion#region 消息提醒public void NMessage(string message, NotificationType Type){fadeTimer = new Timer();fadeTimer.Tick += FadeTimer_Tick;notifyIcon = new NotifyIcon();notifyIcon.Visible = true;// 根据类型选择合适的图标和错误类型switch (Type){case NotificationType.Error:notifyIcon.Icon = SystemIcons.Error;notifyIcon.ShowBalloonTip(2000, "错误", message, ToolTipIcon.Error);break;case NotificationType.Warning:notifyIcon.Icon = SystemIcons.Warning;notifyIcon.ShowBalloonTip(2000, "警告", message, ToolTipIcon.Warning);break;default:notifyIcon.Icon = SystemIcons.Information;notifyIcon.ShowBalloonTip(2000, "提示", message, ToolTipIcon.Info);break;}// 显示气球提示框// 开始计时器,渐渐地隐藏消息框fadeTimer.Start();}private void FadeTimer_Tick(object sender, EventArgs e){// 逐渐减小消息框的不透明度opacityLevel -= fadeSpeed;notifyIcon.Visible = opacityLevel > 0;if (opacityLevel <= 0){// 当不透明度减小到0时停止计时器fadeTimer.Stop();// 关闭 NotifyIconif (notifyIcon != null){notifyIcon.Dispose();notifyIcon.Visible = false;}}}#endregion
使用消息框:
NMessage($"修改失败{ex.Message}", NotificationType.Error);//错误提示
NMessage($"删除{Name}成功", NotificationType.Info);//普通提示
NMessage($"删除{Name}成功", NotificationType.Warning);//警告提示