ToastForm.cs 8.0 KB

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