ToastForm.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Azylee.WinformSkin.APIUtils;
  2. using Azylee.WinformSkin.FormUI.NoTitle;
  3. using Azylee.WinformSkin.Properties;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace Azylee.WinformSkin.FormUI.Toast
  13. {
  14. public partial class ToastForm : NoTitleForm
  15. {
  16. public enum ToastType { warn, error, info }
  17. private static ToastForm form = new ToastForm();
  18. private Action ClickAction = null;
  19. /// <summary>
  20. /// 弹出提示框
  21. /// </summary>
  22. /// <param name="title">标题</param>
  23. /// <param name="text">内容</param>
  24. /// <param name="type">类型:w,e,i</param>
  25. /// <param name="time">显示时间:毫秒</param>
  26. /// <param name="clickAction">点击触发动作反馈</param>
  27. public static void Display(string title, string text, char type, int time, Action clickAction = null)
  28. {
  29. try
  30. {
  31. if (form == null || form.IsDisposed)
  32. form = new ToastForm();
  33. ToastType tt = ToastType.info;
  34. if (type == 'w' || type == 'W') tt = ToastType.warn;
  35. if (type == 'e' || type == 'E') tt = ToastType.error;
  36. form.SetContent(title, text, tt, time);//设置提示框:标题、文本、类型、时间
  37. form.ClickAction = clickAction;//设置单击触发事件
  38. form.Toast();
  39. }
  40. catch { }
  41. }
  42. /// <summary>
  43. /// 弹出提示框
  44. /// </summary>
  45. /// <param name="title">标题</param>
  46. /// <param name="text">内容</param>
  47. /// <param name="type">类型</param>
  48. /// <param name="clickAction">点击动作</param>
  49. /// <param name="time">显示时间:秒</param>
  50. public static void Display(string title, string text, ToastType type = ToastType.info, Action clickAction = null, short time = 5)
  51. {
  52. try
  53. {
  54. if (form == null || form.IsDisposed)
  55. form = new ToastForm();
  56. form.SetContent(title, text, type, time * 1000);//设置提示框:标题、文本、类型、时间
  57. form.ClickAction = clickAction;//设置单击触发事件
  58. form.Toast();
  59. }
  60. catch { }
  61. }
  62. private int TimeSpend = 0;
  63. private int SlowTime = 8;
  64. private int SlowStep = 10;
  65. private ToastForm()
  66. {
  67. InitializeComponent();
  68. }
  69. private void ToastForm_Load(object sender, EventArgs e)
  70. {
  71. SetPosition();//设置初始位置为右下角,开始栏上方12px
  72. ShowInTaskbar = false;//不在任务栏显示
  73. TopMost = true;//显示到最上层窗体
  74. }
  75. private void Toast()
  76. {
  77. Show();//显示窗口
  78. TMHideAnim.Enabled = false;//隐藏动画禁用(防止冲突)
  79. TMHide.Enabled = false;//隐藏计时器禁用(防止冲突)
  80. TMShowAnim.Enabled = true;//启动显示动画
  81. TMHide.Enabled = true;//开始隐藏倒计时
  82. }
  83. #region 初始化设置
  84. /// <summary>
  85. /// 初始化设置,设置要显示的内容
  86. /// </summary>
  87. /// <param name="title">标题</param>
  88. /// <param name="text">内容</param>
  89. /// <param name="type">类型:w,e,i</param>
  90. /// <param name="time">显示时间:ms</param>
  91. private void SetContent(string title, string text, ToastType type, int time)
  92. {
  93. TimeSpend = 0;//初始化运行时间,每次执行动画++
  94. SetPosition();//设置初始位置
  95. TMShowAnim.Interval = 10;//设置显示动画执行间隔
  96. Text = title;//设置程序标题
  97. LBTitle.Text = title;//设置显示标题
  98. LBText.Text = text;//设置内容
  99. SetType(type);//设置消息类型
  100. TMHide.Interval = time;//设置显示时长
  101. }
  102. /// <summary>
  103. /// 设置消息类型
  104. /// </summary>
  105. /// <param name="type"></param>
  106. private void SetType(ToastType type)
  107. {
  108. switch (type)
  109. {
  110. case ToastType.warn:
  111. PBIcon.Image = Resources.toast_warning;
  112. break;
  113. case ToastType.error:
  114. PBIcon.Image = Resources.toast_error;
  115. break;
  116. case ToastType.info:
  117. PBIcon.Image = Resources.toast_info;
  118. break;
  119. default:
  120. PBIcon.Image = Resources.toast_info;
  121. break;
  122. }
  123. }
  124. /// <summary>
  125. /// 设置初始位置
  126. /// </summary>
  127. private void SetPosition()
  128. {
  129. Left = Screen.PrimaryScreen.WorkingArea.Width;
  130. Top = Screen.PrimaryScreen.WorkingArea.Height - Height - 12;
  131. }
  132. #endregion
  133. #region 事件动作
  134. private void PBIcon_Click(object sender, EventArgs e)
  135. {
  136. ClickAction?.Invoke();
  137. ClickAction = null;
  138. HideForm();
  139. }
  140. private void LBTitle_Click(object sender, EventArgs e)
  141. {
  142. ClickAction?.Invoke();
  143. ClickAction = null;
  144. HideForm();
  145. }
  146. private void LBText_Click(object sender, EventArgs e)
  147. {
  148. ClickAction?.Invoke();
  149. ClickAction = null;
  150. HideForm();
  151. }
  152. #endregion
  153. #region 显示提示框
  154. private void TMShowAnim_Tick(object sender, EventArgs e)
  155. {
  156. if (Left > Screen.PrimaryScreen.WorkingArea.Width - Width)
  157. {
  158. Left -= 30;
  159. if (TimeSpend++ > SlowTime) TMShowAnim.Interval += SlowStep;
  160. }
  161. else
  162. {
  163. TMShowAnim.Enabled = false;
  164. }
  165. }
  166. #endregion
  167. #region 隐藏提示框
  168. /// <summary>
  169. /// 执行隐藏窗口动画
  170. /// </summary>
  171. private void HideForm()
  172. {
  173. TimeSpend = 0;
  174. TMShowAnim.Enabled = false;
  175. TMHide.Enabled = false;
  176. TMHideAnim.Enabled = true;//执行隐藏窗口动画
  177. TMHideAnim.Interval = 10;
  178. }
  179. private void TMHide_Tick(object sender, EventArgs e)
  180. {
  181. HideForm();
  182. }
  183. private void TMHideAnim_Tick(object sender, EventArgs e)
  184. {
  185. if (Left < Screen.PrimaryScreen.WorkingArea.Width)
  186. {
  187. Left += 30;
  188. if (TimeSpend++ > SlowTime) TMShowAnim.Interval += SlowStep;
  189. }
  190. else
  191. {
  192. TMHideAnim.Enabled = false;
  193. Hide();
  194. Close();
  195. }
  196. }
  197. #endregion
  198. }
  199. }