LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】Winform 仿Toast弹出

admin
2023年9月18日 11:49 本文热度 309

导读

在Winform中,弹窗提示基本都是使用MessageBox.Show(),样式美观度暂且不论,这是一个必须要交互的消息提示框,所以很多时候就会无形之中增加操作的繁琐度。如果开发过Web或者安卓就会知道有一个Toast的消息提示,即短暂提示后就消失,无需操作反馈,在很多情况下是着实好用。。。

本篇在Winform中封装一个类似Toast效果的弹出框,可以设置弹出位置、颜色、自动关闭时间等。






开发环境:.NET Framework版本:4.8

开发工具:Visual Studio 2022

  1. 新建一个窗体Toast去除边框属性,设置为合适的弹出框大小,并在其上放置一个Label控件

  2. 将窗体的构造函数设为私有并开放出来一个Show函数用来设置属性并实例化弹出。

public static void Show(string msg, Color backColor, Color foreColor, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000)

{

  Toast toast = new Toast();

  toast.StartPosition = FormStartPosition.CenterScreen;

  toast.ShowInTaskbar = false;

  toast.BackColor = backColor;

  toast.SetProperty(msg, foreColor);
  toast.Show();

  Rectangle rect = Screen.PrimaryScreen.WorkingArea;

  switch (location)

  {

      case ShowLocation.Top: toast.Location = new Point((rect.Width - toast.Width) / 2, 10);

          break;

      case ShowLocation.Bottom: toast.Location = new Point((rect.Width - toast.Width) / 2, rect.Height - toast.Height - 10);

          break;

      case ShowLocation.RightBottom: toast.Location = new Point(rect.Width - toast.Width - 10, rect.Height - toast.Height - 10);

          break;

      default:
          break;

  }

  System.Timers.Timer timer = new System.Timers.Timer(autoColseTime);

  timer.Elapsed += delegate

  {

      timer.Stop();

      toast?.Invoke(new Action(() =>

      {

          toast.Close();

      }));

  };

  timer.Start();

}

  1. 为了方便调用,分别再提供SuccessWarningError三个函数,用来对不同的效果设置不同的属性。

public static void Success(string msg, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000)

{

Show(msg, Color.fromArgb(103, 194, 58), Color.White, location, autoColseTime);

}

 

public static void Warning(string msg, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000)

{

Show(msg, Color.fromArgb(230, 162, 60), Color.White, location, autoColseTime);

}

 

public static void Error(string msg, ShowLocation location = ShowLocation.Center, int autoColseTime = 2000)

{

Show(msg, Color.fromArgb(245, 108, 108), Color.White, location, autoColseTime);

}

  1. 调用

private void button1_Click(object sender, EventArgs e)        {

Toast.Success("", ShowLocation.Top);

Toast.Error("", ShowLocation.Bottom);

Toast.Warning("右下", ShowLocation.RightBottom);

Toast.Show("", Color.fromArgb(200, 0, 0, 0), Color.White);

}

5、实现的效果


6、下载地址:

https://pan.baidu.com/s/1Fgq875Fx1h1q00IQtH6W_Q?pwd=1lma


该文章在 2023/9/18 11:49:40 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved