【C#】Winform 仿Toast弹出
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
导读 在Winform中,弹窗提示基本都是使用MessageBox.Show(),样式美观度暂且不论,这是一个必须要交互的消息提示框,所以很多时候就会无形之中增加操作的繁琐度。如果开发过Web或者安卓就会知道有一个Toast的消息提示,即短暂提示后就消失,无需操作反馈,在很多情况下是着实好用。。。 本篇在Winform中封装一个类似Toast效果的弹出框,可以设置弹出位置、颜色、自动关闭时间等。 开发环境:.NET Framework版本:4.8 开发工具:Visual Studio 2022
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); 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: } System.Timers.Timer timer = new System.Timers.Timer(autoColseTime); timer.Elapsed += delegate { timer.Stop(); toast?.Invoke(new Action(() => { toast.Close(); })); }; timer.Start(); }
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); }
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 编辑过 |
关键字查询
相关文章
正在查询... |