ToastForm.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. Text = title;//设置程序标题
  94. LBTitle.Text = title;//设置显示标题
  95. LBText.Text = text;//设置内容
  96. SetType(type);//设置消息类型
  97. TMHide.Interval = time;//设置显示时长
  98. Height = 80;
  99. try
  100. {
  101. int byte_len = Encoding.UTF8.GetBytes(LBText.Text).Length;
  102. if (byte_len > 105)
  103. {
  104. byte_len -= 105;
  105. Height = 100;
  106. while ((byte_len = byte_len - 55) > 0) Height += 20;
  107. }
  108. }
  109. catch { }
  110. TimeSpend = 0;//初始化运行时间,每次执行动画++
  111. SetPosition();//设置初始位置
  112. TMShowAnim.Interval = 10;//设置显示动画执行间隔
  113. SetBorder();//重置边框(因窗口高度发生变化,刷新边框)
  114. }
  115. /// <summary>
  116. /// 设置消息类型
  117. /// </summary>
  118. /// <param name="type"></param>
  119. private void SetType(ToastType type)
  120. {
  121. switch (type)
  122. {
  123. case ToastType.warn:
  124. PBIcon.Image = Resources.toast_warning;
  125. break;
  126. case ToastType.error:
  127. PBIcon.Image = Resources.toast_error;
  128. break;
  129. case ToastType.info:
  130. PBIcon.Image = Resources.toast_info;
  131. break;
  132. default:
  133. PBIcon.Image = Resources.toast_info;
  134. break;
  135. }
  136. }
  137. /// <summary>
  138. /// 设置初始位置
  139. /// </summary>
  140. private void SetPosition()
  141. {
  142. Left = Screen.PrimaryScreen.WorkingArea.Width;
  143. Top = Screen.PrimaryScreen.WorkingArea.Height - Height - 12;
  144. }
  145. #endregion
  146. #region 事件动作
  147. private void PBIcon_Click(object sender, EventArgs e)
  148. {
  149. ClickAction?.Invoke();
  150. ClickAction = null;
  151. HideForm();
  152. }
  153. private void LBTitle_Click(object sender, EventArgs e)
  154. {
  155. ClickAction?.Invoke();
  156. ClickAction = null;
  157. HideForm();
  158. }
  159. private void LBText_Click(object sender, EventArgs e)
  160. {
  161. ClickAction?.Invoke();
  162. ClickAction = null;
  163. HideForm();
  164. }
  165. #endregion
  166. #region 显示提示框
  167. private void TMShowAnim_Tick(object sender, EventArgs e)
  168. {
  169. if (Left > Screen.PrimaryScreen.WorkingArea.Width - Width)
  170. {
  171. Left -= 30;
  172. if (TimeSpend++ > SlowTime) TMShowAnim.Interval += SlowStep;
  173. }
  174. else
  175. {
  176. TMShowAnim.Enabled = false;
  177. }
  178. }
  179. #endregion
  180. #region 隐藏提示框
  181. /// <summary>
  182. /// 执行隐藏窗口动画
  183. /// </summary>
  184. private void HideForm()
  185. {
  186. TimeSpend = 0;
  187. TMShowAnim.Enabled = false;
  188. TMHide.Enabled = false;
  189. TMHideAnim.Enabled = true;//执行隐藏窗口动画
  190. TMHideAnim.Interval = 10;
  191. }
  192. private void TMHide_Tick(object sender, EventArgs e)
  193. {
  194. HideForm();
  195. }
  196. private void TMHideAnim_Tick(object sender, EventArgs e)
  197. {
  198. if (Left < Screen.PrimaryScreen.WorkingArea.Width)
  199. {
  200. Left += 30;
  201. if (TimeSpend++ > SlowTime) TMShowAnim.Interval += SlowStep;
  202. }
  203. else
  204. {
  205. TMHideAnim.Enabled = false;
  206. Hide();
  207. Close();
  208. }
  209. }
  210. #endregion
  211. }
  212. }