【C#】Winform 仿Toast彈出
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
導(dǎo)讀 在Winform中,彈窗提示基本都是使用MessageBox.Show(),樣式美觀度暫且不論,這是一個(gè)必須要交互的消息提示框,所以很多時(shí)候就會(huì)無(wú)形之中增加操作的繁瑣度。如果開(kāi)發(fā)過(guò)Web或者安卓就會(huì)知道有一個(gè)Toast的消息提示,即短暫提示后就消失,無(wú)需操作反饋,在很多情況下是著實(shí)好用。。。 本篇在Winform中封裝一個(gè)類(lèi)似Toast效果的彈出框,可以設(shè)置彈出位置、顏色、自動(dòng)關(guān)閉時(shí)間等。 開(kāi)發(fā)環(huán)境:.NET Framework版本:4.8 開(kāi)發(fā)工具: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、實(shí)現(xiàn)的效果 6、下載地址: https://pan.baidu.com/s/1Fgq875Fx1h1q00IQtH6W_Q?pwd=1lma 該文章在 2023/9/18 11:49:40 編輯過(guò) |
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)... |