FrmTips.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="FrmTips.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Data;
  20. using System.Drawing;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Forms
  25. {
  26. /// <summary>
  27. /// Class FrmTips.
  28. /// Implements the <see cref="HZH_Controls.Forms.FrmBase" />
  29. /// </summary>
  30. /// <seealso cref="HZH_Controls.Forms.FrmBase" />
  31. public partial class FrmTips : FrmBase
  32. {
  33. /// <summary>
  34. /// The m show align
  35. /// </summary>
  36. private ContentAlignment m_showAlign = ContentAlignment.BottomLeft;
  37. /// <summary>
  38. /// 显示位置
  39. /// </summary>
  40. /// <value>The show align.</value>
  41. public ContentAlignment ShowAlign
  42. {
  43. get { return m_showAlign; }
  44. set { m_showAlign = value; }
  45. }
  46. /// <summary>
  47. /// The m LST tips
  48. /// </summary>
  49. private static List<FrmTips> m_lstTips = new List<FrmTips>();
  50. /// <summary>
  51. /// The m close time
  52. /// </summary>
  53. private int m_CloseTime = 0;
  54. /// <summary>
  55. /// Gets or sets the close time.
  56. /// </summary>
  57. /// <value>The close time.</value>
  58. public int CloseTime
  59. {
  60. get { return m_CloseTime; }
  61. set
  62. {
  63. m_CloseTime = value;
  64. if (value > 0)
  65. timer1.Interval = value;
  66. }
  67. }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="FrmTips" /> class.
  70. /// </summary>
  71. public FrmTips()
  72. {
  73. base.SetStyle(ControlStyles.UserPaint, true);
  74. base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  75. base.SetStyle(ControlStyles.DoubleBuffer, true);
  76. InitializeComponent();
  77. }
  78. #region 清理提示框
  79. /// <summary>
  80. /// 功能描述:清理提示框
  81. /// 作  者:HZH
  82. /// 创建日期:2019-02-28 15:11:03
  83. /// 任务编号:POS
  84. /// </summary>
  85. public static void ClearTips()
  86. {
  87. for (int i = m_lstTips.Count - 1; i >= 0; i--)
  88. {
  89. FrmTips current = m_lstTips[i];
  90. if (!current.IsDisposed)
  91. {
  92. current.Close();
  93. current.Dispose();
  94. }
  95. }
  96. m_lstTips.Clear();
  97. }
  98. #endregion
  99. /// <summary>
  100. /// 重置倒计时
  101. /// </summary>
  102. public void ResetTimer()
  103. {
  104. if (m_CloseTime > 0)
  105. {
  106. timer1.Enabled = false;
  107. timer1.Enabled = true;
  108. }
  109. }
  110. /// <summary>
  111. /// The m last tips
  112. /// </summary>
  113. private static KeyValuePair<string, FrmTips> m_lastTips = new KeyValuePair<string, FrmTips>();
  114. /// <summary>
  115. /// Shows the tips.
  116. /// </summary>
  117. /// <param name="frm">The FRM.</param>
  118. /// <param name="strMsg">The string MSG.</param>
  119. /// <param name="intAutoColseTime">The int automatic colse time.</param>
  120. /// <param name="blnShowCoseBtn">if set to <c>true</c> [BLN show cose BTN].</param>
  121. /// <param name="align">The align.</param>
  122. /// <param name="point">The point.</param>
  123. /// <param name="mode">The mode.</param>
  124. /// <param name="size">The size.</param>
  125. /// <param name="state">The state.</param>
  126. /// <param name="color">The color.</param>
  127. /// <returns>FrmTips.</returns>
  128. public static FrmTips ShowTips(
  129. Form frm,
  130. string strMsg,
  131. int intAutoColseTime = 0,
  132. bool blnShowCoseBtn = true,
  133. ContentAlignment align = ContentAlignment.BottomLeft,
  134. Point? point = null,
  135. TipsSizeMode mode = TipsSizeMode.Small,
  136. Size? size = null,
  137. TipsState state = TipsState.Default)
  138. {
  139. if (m_lastTips.Key == strMsg + state && !m_lastTips.Value.IsDisposed && m_lastTips.Value.Visible)
  140. {
  141. m_lastTips.Value.ResetTimer();
  142. return m_lastTips.Value;
  143. }
  144. else
  145. {
  146. FrmTips frmTips = new FrmTips();
  147. switch (mode)
  148. {
  149. case TipsSizeMode.Small:
  150. frmTips.Size = new Size(350, 35);
  151. break;
  152. case TipsSizeMode.Medium:
  153. frmTips.Size = new Size(350, 50);
  154. break;
  155. case TipsSizeMode.Large:
  156. frmTips.Size = new Size(350, 65);
  157. break;
  158. case TipsSizeMode.None:
  159. if (!size.HasValue)
  160. {
  161. frmTips.Size = new Size(350, 35);
  162. }
  163. else
  164. {
  165. frmTips.Size = size.Value;
  166. }
  167. break;
  168. }
  169. frmTips.BackColor = Color.FromArgb((int)state);
  170. if (state == TipsState.Default)
  171. {
  172. frmTips.lblMsg.ForeColor = SystemColors.ControlText;
  173. }
  174. else
  175. {
  176. frmTips.lblMsg.ForeColor = Color.White;
  177. }
  178. switch (state)
  179. {
  180. case TipsState.Default:
  181. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  182. break;
  183. case TipsState.Success:
  184. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.success;
  185. break;
  186. case TipsState.Info:
  187. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  188. break;
  189. case TipsState.Warning:
  190. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.warning;
  191. break;
  192. case TipsState.Error:
  193. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.error;
  194. break;
  195. default:
  196. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  197. break;
  198. }
  199. frmTips.lblMsg.Text = strMsg;
  200. frmTips.CloseTime = intAutoColseTime;
  201. frmTips.btnClose.Visible = blnShowCoseBtn;
  202. frmTips.ShowAlign = align;
  203. frmTips.Owner = frm;
  204. FrmTips.m_lstTips.Add(frmTips);
  205. FrmTips.ReshowTips();
  206. frmTips.Show(frm);
  207. if (frm != null && !frm.IsDisposed)
  208. {
  209. ControlHelper.SetForegroundWindow(frm.Handle);
  210. }
  211. //frmTips.BringToFront();
  212. m_lastTips = new KeyValuePair<string, FrmTips>(strMsg + state, frmTips);
  213. return frmTips;
  214. }
  215. }
  216. #region 刷新显示
  217. /// <summary>
  218. /// 功能描述:刷新显示
  219. /// 作  者:HZH
  220. /// 创建日期:2019-02-28 15:33:06
  221. /// 任务编号:POS
  222. /// </summary>
  223. public static void ReshowTips()
  224. {
  225. lock (FrmTips.m_lstTips)
  226. {
  227. FrmTips.m_lstTips.RemoveAll(p => p.IsDisposed == true);
  228. var enumerable = from p in FrmTips.m_lstTips
  229. group p by new
  230. {
  231. p.ShowAlign
  232. };
  233. Size size = Screen.PrimaryScreen.Bounds.Size;
  234. foreach (var item in enumerable)
  235. {
  236. List<FrmTips> list = FrmTips.m_lstTips.FindAll((FrmTips p) => p.ShowAlign == item.Key.ShowAlign);
  237. for (int i = 0; i < list.Count; i++)
  238. {
  239. FrmTips frmTips = list[i];
  240. if (frmTips.InvokeRequired)
  241. {
  242. frmTips.BeginInvoke(new MethodInvoker(delegate()
  243. {
  244. switch (item.Key.ShowAlign)
  245. {
  246. case ContentAlignment.BottomCenter:
  247. frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
  248. break;
  249. case ContentAlignment.BottomLeft:
  250. frmTips.Location = new Point(10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
  251. break;
  252. case ContentAlignment.BottomRight:
  253. frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
  254. break;
  255. case ContentAlignment.MiddleCenter:
  256. frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
  257. break;
  258. case ContentAlignment.MiddleLeft:
  259. frmTips.Location = new Point(10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
  260. break;
  261. case ContentAlignment.MiddleRight:
  262. frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
  263. break;
  264. case ContentAlignment.TopCenter:
  265. frmTips.Location = new Point((size.Width - frmTips.Width) / 2, 10 + (i + 1) * (frmTips.Height + 10));
  266. break;
  267. case ContentAlignment.TopLeft:
  268. frmTips.Location = new Point(10, 10 + (i + 1) * (frmTips.Height + 10));
  269. break;
  270. case ContentAlignment.TopRight:
  271. frmTips.Location = new Point(size.Width - frmTips.Width - 10, 10 + (i + 1) * (frmTips.Height + 10));
  272. break;
  273. default:
  274. break;
  275. }
  276. }));
  277. }
  278. else
  279. {
  280. switch (item.Key.ShowAlign)
  281. {
  282. case ContentAlignment.BottomCenter:
  283. frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
  284. break;
  285. case ContentAlignment.BottomLeft:
  286. frmTips.Location = new Point(10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
  287. break;
  288. case ContentAlignment.BottomRight:
  289. frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - 100 - (i + 1) * (frmTips.Height + 10));
  290. break;
  291. case ContentAlignment.MiddleCenter:
  292. frmTips.Location = new Point((size.Width - frmTips.Width) / 2, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
  293. break;
  294. case ContentAlignment.MiddleLeft:
  295. frmTips.Location = new Point(10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
  296. break;
  297. case ContentAlignment.MiddleRight:
  298. frmTips.Location = new Point(size.Width - frmTips.Width - 10, size.Height - (size.Height - list.Count * (frmTips.Height + 10)) / 2 - (i + 1) * (frmTips.Height + 10));
  299. break;
  300. case ContentAlignment.TopCenter:
  301. frmTips.Location = new Point((size.Width - frmTips.Width) / 2, 10 + (i + 1) * (frmTips.Height + 10));
  302. break;
  303. case ContentAlignment.TopLeft:
  304. frmTips.Location = new Point(10, 10 + (i + 1) * (frmTips.Height + 10));
  305. break;
  306. case ContentAlignment.TopRight:
  307. frmTips.Location = new Point(size.Width - frmTips.Width - 10, 10 + (i + 1) * (frmTips.Height + 10));
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. #endregion
  318. /// <summary>
  319. /// Handles the FormClosing event of the FrmTips control.
  320. /// </summary>
  321. /// <param name="sender">The source of the event.</param>
  322. /// <param name="e">The <see cref="FormClosingEventArgs" /> instance containing the event data.</param>
  323. private void FrmTips_FormClosing(object sender, FormClosingEventArgs e)
  324. {
  325. if (m_lastTips.Value == this)
  326. m_lastTips = new KeyValuePair<string, FrmTips>();
  327. m_lstTips.Remove(this);
  328. ReshowTips();
  329. for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
  330. {
  331. if (Application.OpenForms[i].IsDisposed || !Application.OpenForms[i].Visible || Application.OpenForms[i] is FrmTips)
  332. {
  333. continue;
  334. }
  335. else
  336. {
  337. Timer t = new Timer();
  338. t.Interval = 100;
  339. var frm = Application.OpenForms[i];
  340. t.Tick += (a, b) =>
  341. {
  342. t.Enabled = false;
  343. if (!frm.IsDisposed)
  344. ControlHelper.SetForegroundWindow(frm.Handle);
  345. };
  346. t.Enabled = true;
  347. break;
  348. }
  349. }
  350. }
  351. /// <summary>
  352. /// Handles the Load event of the FrmTips control.
  353. /// </summary>
  354. /// <param name="sender">The source of the event.</param>
  355. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  356. private void FrmTips_Load(object sender, EventArgs e)
  357. {
  358. if (m_CloseTime > 0)
  359. {
  360. this.timer1.Interval = m_CloseTime;
  361. this.timer1.Enabled = true;
  362. }
  363. }
  364. /// <summary>
  365. /// Handles the Tick event of the timer1 control.
  366. /// </summary>
  367. /// <param name="sender">The source of the event.</param>
  368. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  369. private void timer1_Tick(object sender, EventArgs e)
  370. {
  371. this.timer1.Enabled = false;
  372. this.Close();
  373. }
  374. /// <summary>
  375. /// Handles the MouseDown event of the btnClose control.
  376. /// </summary>
  377. /// <param name="sender">The source of the event.</param>
  378. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  379. private void btnClose_MouseDown(object sender, MouseEventArgs e)
  380. {
  381. this.timer1.Enabled = false;
  382. this.Close();
  383. }
  384. /// <summary>
  385. /// Shows the tips success.
  386. /// </summary>
  387. /// <param name="frm">The FRM.</param>
  388. /// <param name="strMsg">The string MSG.</param>
  389. /// <returns>FrmTips.</returns>
  390. public static FrmTips ShowTipsSuccess(Form frm, string strMsg)
  391. {
  392. return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Success);
  393. }
  394. /// <summary>
  395. /// Shows the tips error.
  396. /// </summary>
  397. /// <param name="frm">The FRM.</param>
  398. /// <param name="strMsg">The string MSG.</param>
  399. /// <returns>FrmTips.</returns>
  400. public static FrmTips ShowTipsError(Form frm, string strMsg)
  401. {
  402. return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Error);
  403. }
  404. /// <summary>
  405. /// Shows the tips information.
  406. /// </summary>
  407. /// <param name="frm">The FRM.</param>
  408. /// <param name="strMsg">The string MSG.</param>
  409. /// <returns>FrmTips.</returns>
  410. public static FrmTips ShowTipsInfo(Form frm, string strMsg)
  411. {
  412. return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Info);
  413. }
  414. /// <summary>
  415. /// Shows the tips warning.
  416. /// </summary>
  417. /// <param name="frm">The FRM.</param>
  418. /// <param name="strMsg">The string MSG.</param>
  419. /// <returns>FrmTips.</returns>
  420. public static FrmTips ShowTipsWarning(Form frm, string strMsg)
  421. {
  422. return FrmTips.ShowTips(frm, strMsg, 3000, false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Warning);
  423. }
  424. /// <summary>
  425. /// Handles the FormClosed event of the FrmTips control.
  426. /// </summary>
  427. /// <param name="sender">The source of the event.</param>
  428. /// <param name="e">The <see cref="FormClosedEventArgs" /> instance containing the event data.</param>
  429. private void FrmTips_FormClosed(object sender, FormClosedEventArgs e)
  430. {
  431. this.Dispose();
  432. GC.Collect();
  433. }
  434. }
  435. /// <summary>
  436. /// Enum TipsSizeMode
  437. /// </summary>
  438. public enum TipsSizeMode
  439. {
  440. /// <summary>
  441. /// The small
  442. /// </summary>
  443. Small,
  444. /// <summary>
  445. /// The medium
  446. /// </summary>
  447. Medium,
  448. /// <summary>
  449. /// The large
  450. /// </summary>
  451. Large,
  452. /// <summary>
  453. /// The none
  454. /// </summary>
  455. None
  456. }
  457. /// <summary>
  458. /// Enum TipsState
  459. /// </summary>
  460. public enum TipsState
  461. {
  462. Default = -12542209,
  463. Success = -9977286,
  464. Info = -7299687,
  465. Warning = -693140,
  466. Error = -1097849
  467. }
  468. }