官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

这个窗体继承子基类窗体FrmBase,如果你对FrmBase还不了解,请移步 (十七)c#Winform自定义控件-基类窗体 查看

气泡需要支持多个位置显示,也就是说四面八方都可以显示,并且支持样式,自定义大小,另外具有内置的4中模式(成功,错误,警告,正常)

开始

定义一些枚举

  1. public enum TipsSizeMode
  2. {
  3. Small,
  4. Medium,
  5. Large,
  6. None
  7. }
  8. public enum TipsState
  9. {
  10. Default = -,
  11. Success = -,
  12. Info = -,
  13. Warning = -,
  14. Error = -
  15. }

添加Form,命名FrmTips,继承自FrmBase

属性

  1. private ContentAlignment m_showAlign = ContentAlignment.BottomLeft;
  2.  
  3. /// <summary>
  4. /// 显示位置
  5. /// </summary>
  6. public ContentAlignment ShowAlign
  7. {
  8. get { return m_showAlign; }
  9. set { m_showAlign = value; }
  10. }
  11.  
  12. private static List<FrmTips> m_lstTips = new List<FrmTips>();
  13.  
  14. private int m_CloseTime = ;
  15.  
  16. public int CloseTime
  17. {
  18. get { return m_CloseTime; }
  19. set
  20. {
  21. m_CloseTime = value;
  22. if (value > )
  23. timer1.Interval = value;
  24. }
  25. }

提供一些公共函数

  1. #region 清理提示框
  2. /// <summary>
  3. /// 功能描述:清理提示框
  4. /// 作  者:HZH
  5. /// 创建日期:2019-02-28 15:11:03
  6. /// 任务编号:POS
  7. /// </summary>
  8. public static void ClearTips()
  9. {
  10. for (int i = m_lstTips.Count - ; i >= ; i--)
  11. {
  12. FrmTips current = m_lstTips[i];
  13. if (!current.IsDisposed)
  14. {
  15. current.Close();
  16. current.Dispose();
  17. }
  18. }
  19. m_lstTips.Clear();
  20. }
  21. #endregion
  22.  
  23. /// <summary>
  24. /// 重置倒计时
  25. /// </summary>
  26. public void ResetTimer()
  27. {
  28. if (m_CloseTime > )
  29. {
  30. timer1.Enabled = false;
  31. timer1.Enabled = true;
  32. }
  33. }

提供的静态函数

  1. #region 清理提示框
  2. /// <summary>
  3. /// 功能描述:清理提示框
  4. /// 作  者:HZH
  5. /// 创建日期:2019-02-28 15:11:03
  6. /// 任务编号:POS
  7. /// </summary>
  8. public static void ClearTips()
  9. {
  10. for (int i = m_lstTips.Count - ; i >= ; i--)
  11. {
  12. FrmTips current = m_lstTips[i];
  13. if (!current.IsDisposed)
  14. {
  15. current.Close();
  16. current.Dispose();
  17. }
  18. }
  19. m_lstTips.Clear();
  20. }
  21. #endregion
  22.  
  23. private static KeyValuePair<string, FrmTips> m_lastTips = new KeyValuePair<string, FrmTips>();
  24.  
  25. public static FrmTips ShowTips(
  26. Form frm,
  27. string strMsg,
  28. int intAutoColseTime = ,
  29. bool blnShowCoseBtn = true,
  30. ContentAlignment align = ContentAlignment.BottomLeft,
  31. Point? point = null,
  32. TipsSizeMode mode = TipsSizeMode.Small,
  33. Size? size = null,
  34. TipsState state = TipsState.Default)
  35. {
  36.  
  37. if (m_lastTips.Key == strMsg + state && !m_lastTips.Value.IsDisposed && m_lastTips.Value.Visible)
  38. {
  39. m_lastTips.Value.ResetTimer();
  40. return m_lastTips.Value;
  41. }
  42. else
  43. {
  44. FrmTips frmTips = new FrmTips();
  45. switch (mode)
  46. {
  47. case TipsSizeMode.Small:
  48. frmTips.Size = new Size(, );
  49. break;
  50. case TipsSizeMode.Medium:
  51. frmTips.Size = new Size(, );
  52. break;
  53. case TipsSizeMode.Large:
  54. frmTips.Size = new Size(, );
  55. break;
  56. case TipsSizeMode.None:
  57. if (!size.HasValue)
  58. {
  59. frmTips.Size = new Size(, );
  60. }
  61. else
  62. {
  63. frmTips.Size = size.Value;
  64. }
  65. break;
  66. }
  67. frmTips.BackColor = Color.FromArgb((int)state);
  68.  
  69. if (state == TipsState.Default)
  70. {
  71. frmTips.lblMsg.ForeColor = SystemColors.ControlText;
  72. }
  73. else
  74. {
  75. frmTips.lblMsg.ForeColor = Color.White;
  76. }
  77. switch (state)
  78. {
  79. case TipsState.Default:
  80. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  81. break;
  82. case TipsState.Success:
  83. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.success;
  84. break;
  85. case TipsState.Info:
  86. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  87. break;
  88. case TipsState.Warning:
  89. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.warning;
  90. break;
  91. case TipsState.Error:
  92. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.error;
  93. break;
  94. default:
  95. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  96. break;
  97. }
  98.  
  99. frmTips.lblMsg.Text = strMsg;
  100. frmTips.CloseTime = intAutoColseTime;
  101. frmTips.btnClose.Visible = blnShowCoseBtn;
  102.  
  103. frmTips.ShowAlign = align;
  104. frmTips.Owner = frm;
  105. FrmTips.m_lstTips.Add(frmTips);
  106. FrmTips.ReshowTips();
  107. frmTips.Show(frm);
  108. if (frm != null && !frm.IsDisposed)
  109. {
  110. ControlHelper.SetForegroundWindow(frm.Handle);
  111. }
  112. //frmTips.BringToFront();
  113. m_lastTips = new KeyValuePair<string, FrmTips>(strMsg + state, frmTips);
  114. return frmTips;
  115. }
  116. }
  117.  
  118. #region 刷新显示
  119. /// <summary>
  120. /// 功能描述:刷新显示
  121. /// 作  者:HZH
  122. /// 创建日期:2019-02-28 15:33:06
  123. /// 任务编号:POS
  124. /// </summary>
  125. public static void ReshowTips()
  126. {
  127. lock (FrmTips.m_lstTips)
  128. {
  129. FrmTips.m_lstTips.RemoveAll(p => p.IsDisposed == true);
  130. var enumerable = from p in FrmTips.m_lstTips
  131. group p by new
  132. {
  133. p.ShowAlign
  134. };
  135. Size size = Screen.PrimaryScreen.Bounds.Size;
  136. foreach (var item in enumerable)
  137. {
  138. List<FrmTips> list = FrmTips.m_lstTips.FindAll((FrmTips p) => p.ShowAlign == item.Key.ShowAlign);
  139. for (int i = ; i < list.Count; i++)
  140. {
  141. FrmTips frmTips = list[i];
  142. if (frmTips.InvokeRequired)
  143. {
  144. frmTips.BeginInvoke(new MethodInvoker(delegate()
  145. {
  146. switch (item.Key.ShowAlign)
  147. {
  148. case ContentAlignment.BottomCenter:
  149. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - - (i + ) * (frmTips.Height + ));
  150. break;
  151. case ContentAlignment.BottomLeft:
  152. frmTips.Location = new Point(, size.Height - - (i + ) * (frmTips.Height + ));
  153. break;
  154. case ContentAlignment.BottomRight:
  155. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - - (i + ) * (frmTips.Height + ));
  156. break;
  157. case ContentAlignment.MiddleCenter:
  158. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  159. break;
  160. case ContentAlignment.MiddleLeft:
  161. frmTips.Location = new Point(, size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  162. break;
  163. case ContentAlignment.MiddleRight:
  164. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  165. break;
  166. case ContentAlignment.TopCenter:
  167. frmTips.Location = new Point((size.Width - frmTips.Width) / , + (i + ) * (frmTips.Height + ));
  168. break;
  169. case ContentAlignment.TopLeft:
  170. frmTips.Location = new Point(, + (i + ) * (frmTips.Height + ));
  171. break;
  172. case ContentAlignment.TopRight:
  173. frmTips.Location = new Point(size.Width - frmTips.Width - , + (i + ) * (frmTips.Height + ));
  174. break;
  175. default:
  176. break;
  177. }
  178. }));
  179. }
  180. else
  181. {
  182. switch (item.Key.ShowAlign)
  183. {
  184. case ContentAlignment.BottomCenter:
  185. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - - (i + ) * (frmTips.Height + ));
  186. break;
  187. case ContentAlignment.BottomLeft:
  188. frmTips.Location = new Point(, size.Height - - (i + ) * (frmTips.Height + ));
  189. break;
  190. case ContentAlignment.BottomRight:
  191. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - - (i + ) * (frmTips.Height + ));
  192. break;
  193. case ContentAlignment.MiddleCenter:
  194. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  195. break;
  196. case ContentAlignment.MiddleLeft:
  197. frmTips.Location = new Point(, size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  198. break;
  199. case ContentAlignment.MiddleRight:
  200. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  201. break;
  202. case ContentAlignment.TopCenter:
  203. frmTips.Location = new Point((size.Width - frmTips.Width) / , + (i + ) * (frmTips.Height + ));
  204. break;
  205. case ContentAlignment.TopLeft:
  206. frmTips.Location = new Point(, + (i + ) * (frmTips.Height + ));
  207. break;
  208. case ContentAlignment.TopRight:
  209. frmTips.Location = new Point(size.Width - frmTips.Width - , + (i + ) * (frmTips.Height + ));
  210. break;
  211. default:
  212. break;
  213. }
  214. }
  215.  
  216. }
  217. }
  218. }
  219. }
  220. #endregion
  221.  
  222. public static FrmTips ShowTipsSuccess(Form frm, string strMsg)
  223. {
  224. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Success);
  225. }
  226.  
  227. public static FrmTips ShowTipsError(Form frm, string strMsg)
  228. {
  229. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Error);
  230. }
  231.  
  232. public static FrmTips ShowTipsInfo(Form frm, string strMsg)
  233. {
  234. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Info);
  235. }
  236. public static FrmTips ShowTipsWarning(Form frm, string strMsg)
  237. {
  238. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Warning);
  239. }

一些事件处理

  1. private void FrmTips_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3. if (m_lastTips.Value == this)
  4. m_lastTips = new KeyValuePair<string, FrmTips>();
  5. m_lstTips.Remove(this);
  6. ReshowTips();
  7.  
  8. for (int i = Application.OpenForms.Count - ; i >= ; i--)
  9. {
  10. if (Application.OpenForms[i].IsDisposed || !Application.OpenForms[i].Visible || Application.OpenForms[i] is FrmTips)
  11. {
  12. continue;
  13. }
  14. else
  15. {
  16. Timer t = new Timer();
  17. t.Interval = ;
  18. var frm = Application.OpenForms[i];
  19. t.Tick += (a, b) =>
  20. {
  21. t.Enabled = false;
  22. if (!frm.IsDisposed)
  23. ControlHelper.SetForegroundWindow(frm.Handle);
  24. };
  25. t.Enabled = true;
  26. break;
  27. }
  28. }
  29. }
  30.  
  31. private void FrmTips_Load(object sender, EventArgs e)
  32. {
  33. if (m_CloseTime > )
  34. {
  35. this.timer1.Interval = m_CloseTime;
  36. this.timer1.Enabled = true;
  37. }
  38. }
  39.  
  40. private void timer1_Tick(object sender, EventArgs e)
  41. {
  42. this.timer1.Enabled = false;
  43. this.Close();
  44. }
  45.  
  46. private void btnClose_MouseDown(object sender, MouseEventArgs e)
  47. {
  48. this.timer1.Enabled = false;
  49. this.Close();
  50. }
  51. private void FrmTips_FormClosed(object sender, FormClosedEventArgs e)
  52. {
  53. this.Dispose();
  54. GC.Collect();
  55. }

最后看一下完整代码

  1. // 版权所有 黄正辉 交流群:568015492 QQ:623128629
  2. // 文件名称:FrmTips.cs
  3. // 创建日期:2019-08-15 16:04:54
  4. // 功能描述:FrmTips
  5. // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Windows.Forms;
  14.  
  15. namespace HZH_Controls.Forms
  16. {
  17. public partial class FrmTips : FrmBase
  18. {
  19. private ContentAlignment m_showAlign = ContentAlignment.BottomLeft;
  20.  
  21. /// <summary>
  22. /// 显示位置
  23. /// </summary>
  24. public ContentAlignment ShowAlign
  25. {
  26. get { return m_showAlign; }
  27. set { m_showAlign = value; }
  28. }
  29.  
  30. private static List<FrmTips> m_lstTips = new List<FrmTips>();
  31.  
  32. private int m_CloseTime = ;
  33.  
  34. public int CloseTime
  35. {
  36. get { return m_CloseTime; }
  37. set
  38. {
  39. m_CloseTime = value;
  40. if (value > )
  41. timer1.Interval = value;
  42. }
  43. }
  44.  
  45. public FrmTips()
  46. {
  47. base.SetStyle(ControlStyles.UserPaint, true);
  48. base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  49. base.SetStyle(ControlStyles.DoubleBuffer, true);
  50. InitializeComponent();
  51. }
  52.  
  53. #region 清理提示框
  54. /// <summary>
  55. /// 功能描述:清理提示框
  56. /// 作  者:HZH
  57. /// 创建日期:2019-02-28 15:11:03
  58. /// 任务编号:POS
  59. /// </summary>
  60. public static void ClearTips()
  61. {
  62. for (int i = m_lstTips.Count - ; i >= ; i--)
  63. {
  64. FrmTips current = m_lstTips[i];
  65. if (!current.IsDisposed)
  66. {
  67. current.Close();
  68. current.Dispose();
  69. }
  70. }
  71. m_lstTips.Clear();
  72. }
  73. #endregion
  74.  
  75. /// <summary>
  76. /// 重置倒计时
  77. /// </summary>
  78. public void ResetTimer()
  79. {
  80. if (m_CloseTime > )
  81. {
  82. timer1.Enabled = false;
  83. timer1.Enabled = true;
  84. }
  85. }
  86. private static KeyValuePair<string, FrmTips> m_lastTips = new KeyValuePair<string, FrmTips>();
  87.  
  88. public static FrmTips ShowTips(
  89. Form frm,
  90. string strMsg,
  91. int intAutoColseTime = ,
  92. bool blnShowCoseBtn = true,
  93. ContentAlignment align = ContentAlignment.BottomLeft,
  94. Point? point = null,
  95. TipsSizeMode mode = TipsSizeMode.Small,
  96. Size? size = null,
  97. TipsState state = TipsState.Default)
  98. {
  99.  
  100. if (m_lastTips.Key == strMsg + state && !m_lastTips.Value.IsDisposed && m_lastTips.Value.Visible)
  101. {
  102. m_lastTips.Value.ResetTimer();
  103. return m_lastTips.Value;
  104. }
  105. else
  106. {
  107. FrmTips frmTips = new FrmTips();
  108. switch (mode)
  109. {
  110. case TipsSizeMode.Small:
  111. frmTips.Size = new Size(, );
  112. break;
  113. case TipsSizeMode.Medium:
  114. frmTips.Size = new Size(, );
  115. break;
  116. case TipsSizeMode.Large:
  117. frmTips.Size = new Size(, );
  118. break;
  119. case TipsSizeMode.None:
  120. if (!size.HasValue)
  121. {
  122. frmTips.Size = new Size(, );
  123. }
  124. else
  125. {
  126. frmTips.Size = size.Value;
  127. }
  128. break;
  129. }
  130. frmTips.BackColor = Color.FromArgb((int)state);
  131.  
  132. if (state == TipsState.Default)
  133. {
  134. frmTips.lblMsg.ForeColor = SystemColors.ControlText;
  135. }
  136. else
  137. {
  138. frmTips.lblMsg.ForeColor = Color.White;
  139. }
  140. switch (state)
  141. {
  142. case TipsState.Default:
  143. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  144. break;
  145. case TipsState.Success:
  146. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.success;
  147. break;
  148. case TipsState.Info:
  149. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  150. break;
  151. case TipsState.Warning:
  152. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.warning;
  153. break;
  154. case TipsState.Error:
  155. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.error;
  156. break;
  157. default:
  158. frmTips.pctStat.Image = HZH_Controls.Properties.Resources.alarm;
  159. break;
  160. }
  161.  
  162. frmTips.lblMsg.Text = strMsg;
  163. frmTips.CloseTime = intAutoColseTime;
  164. frmTips.btnClose.Visible = blnShowCoseBtn;
  165.  
  166. frmTips.ShowAlign = align;
  167. frmTips.Owner = frm;
  168. FrmTips.m_lstTips.Add(frmTips);
  169. FrmTips.ReshowTips();
  170. frmTips.Show(frm);
  171. if (frm != null && !frm.IsDisposed)
  172. {
  173. ControlHelper.SetForegroundWindow(frm.Handle);
  174. }
  175. //frmTips.BringToFront();
  176. m_lastTips = new KeyValuePair<string, FrmTips>(strMsg + state, frmTips);
  177. return frmTips;
  178. }
  179. }
  180.  
  181. #region 刷新显示
  182. /// <summary>
  183. /// 功能描述:刷新显示
  184. /// 作  者:HZH
  185. /// 创建日期:2019-02-28 15:33:06
  186. /// 任务编号:POS
  187. /// </summary>
  188. public static void ReshowTips()
  189. {
  190. lock (FrmTips.m_lstTips)
  191. {
  192. FrmTips.m_lstTips.RemoveAll(p => p.IsDisposed == true);
  193. var enumerable = from p in FrmTips.m_lstTips
  194. group p by new
  195. {
  196. p.ShowAlign
  197. };
  198. Size size = Screen.PrimaryScreen.Bounds.Size;
  199. foreach (var item in enumerable)
  200. {
  201. List<FrmTips> list = FrmTips.m_lstTips.FindAll((FrmTips p) => p.ShowAlign == item.Key.ShowAlign);
  202. for (int i = ; i < list.Count; i++)
  203. {
  204. FrmTips frmTips = list[i];
  205. if (frmTips.InvokeRequired)
  206. {
  207. frmTips.BeginInvoke(new MethodInvoker(delegate()
  208. {
  209. switch (item.Key.ShowAlign)
  210. {
  211. case ContentAlignment.BottomCenter:
  212. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - - (i + ) * (frmTips.Height + ));
  213. break;
  214. case ContentAlignment.BottomLeft:
  215. frmTips.Location = new Point(, size.Height - - (i + ) * (frmTips.Height + ));
  216. break;
  217. case ContentAlignment.BottomRight:
  218. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - - (i + ) * (frmTips.Height + ));
  219. break;
  220. case ContentAlignment.MiddleCenter:
  221. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  222. break;
  223. case ContentAlignment.MiddleLeft:
  224. frmTips.Location = new Point(, size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  225. break;
  226. case ContentAlignment.MiddleRight:
  227. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  228. break;
  229. case ContentAlignment.TopCenter:
  230. frmTips.Location = new Point((size.Width - frmTips.Width) / , + (i + ) * (frmTips.Height + ));
  231. break;
  232. case ContentAlignment.TopLeft:
  233. frmTips.Location = new Point(, + (i + ) * (frmTips.Height + ));
  234. break;
  235. case ContentAlignment.TopRight:
  236. frmTips.Location = new Point(size.Width - frmTips.Width - , + (i + ) * (frmTips.Height + ));
  237. break;
  238. default:
  239. break;
  240. }
  241. }));
  242. }
  243. else
  244. {
  245. switch (item.Key.ShowAlign)
  246. {
  247. case ContentAlignment.BottomCenter:
  248. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - - (i + ) * (frmTips.Height + ));
  249. break;
  250. case ContentAlignment.BottomLeft:
  251. frmTips.Location = new Point(, size.Height - - (i + ) * (frmTips.Height + ));
  252. break;
  253. case ContentAlignment.BottomRight:
  254. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - - (i + ) * (frmTips.Height + ));
  255. break;
  256. case ContentAlignment.MiddleCenter:
  257. frmTips.Location = new Point((size.Width - frmTips.Width) / , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  258. break;
  259. case ContentAlignment.MiddleLeft:
  260. frmTips.Location = new Point(, size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  261. break;
  262. case ContentAlignment.MiddleRight:
  263. frmTips.Location = new Point(size.Width - frmTips.Width - , size.Height - (size.Height - list.Count * (frmTips.Height + )) / - (i + ) * (frmTips.Height + ));
  264. break;
  265. case ContentAlignment.TopCenter:
  266. frmTips.Location = new Point((size.Width - frmTips.Width) / , + (i + ) * (frmTips.Height + ));
  267. break;
  268. case ContentAlignment.TopLeft:
  269. frmTips.Location = new Point(, + (i + ) * (frmTips.Height + ));
  270. break;
  271. case ContentAlignment.TopRight:
  272. frmTips.Location = new Point(size.Width - frmTips.Width - , + (i + ) * (frmTips.Height + ));
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278.  
  279. }
  280. }
  281. }
  282. }
  283. #endregion
  284.  
  285. private void FrmTips_FormClosing(object sender, FormClosingEventArgs e)
  286. {
  287. if (m_lastTips.Value == this)
  288. m_lastTips = new KeyValuePair<string, FrmTips>();
  289. m_lstTips.Remove(this);
  290. ReshowTips();
  291.  
  292. for (int i = Application.OpenForms.Count - ; i >= ; i--)
  293. {
  294. if (Application.OpenForms[i].IsDisposed || !Application.OpenForms[i].Visible || Application.OpenForms[i] is FrmTips)
  295. {
  296. continue;
  297. }
  298. else
  299. {
  300. Timer t = new Timer();
  301. t.Interval = ;
  302. var frm = Application.OpenForms[i];
  303. t.Tick += (a, b) =>
  304. {
  305. t.Enabled = false;
  306. if (!frm.IsDisposed)
  307. ControlHelper.SetForegroundWindow(frm.Handle);
  308. };
  309. t.Enabled = true;
  310. break;
  311. }
  312. }
  313. }
  314.  
  315. private void FrmTips_Load(object sender, EventArgs e)
  316. {
  317. if (m_CloseTime > )
  318. {
  319. this.timer1.Interval = m_CloseTime;
  320. this.timer1.Enabled = true;
  321. }
  322. }
  323.  
  324. private void timer1_Tick(object sender, EventArgs e)
  325. {
  326. this.timer1.Enabled = false;
  327. this.Close();
  328. }
  329.  
  330. private void btnClose_MouseDown(object sender, MouseEventArgs e)
  331. {
  332. this.timer1.Enabled = false;
  333. this.Close();
  334. }
  335.  
  336. public static FrmTips ShowTipsSuccess(Form frm, string strMsg)
  337. {
  338. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Success);
  339. }
  340.  
  341. public static FrmTips ShowTipsError(Form frm, string strMsg)
  342. {
  343. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Error);
  344. }
  345.  
  346. public static FrmTips ShowTipsInfo(Form frm, string strMsg)
  347. {
  348. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Info);
  349. }
  350. public static FrmTips ShowTipsWarning(Form frm, string strMsg)
  351. {
  352. return FrmTips.ShowTips(frm, strMsg, , false, ContentAlignment.BottomCenter, null, TipsSizeMode.Large, null, TipsState.Warning);
  353. }
  354.  
  355. private void FrmTips_FormClosed(object sender, FormClosedEventArgs e)
  356. {
  357. this.Dispose();
  358. GC.Collect();
  359. }
  360.  
  361. }
  362.  
  363. public enum TipsSizeMode
  364. {
  365. Small,
  366. Medium,
  367. Large,
  368. None
  369. }
  370. public enum TipsState
  371. {
  372. Default = -,
  373. Success = -,
  374. Info = -,
  375. Warning = -,
  376. Error = -
  377. }
  378. }
  1. namespace HZH_Controls.Forms
  2. {
  3. partial class FrmTips
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.components = new System.ComponentModel.Container();
  32. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmTips));
  33. this.lblMsg = new System.Windows.Forms.Label();
  34. this.btnClose = new System.Windows.Forms.PictureBox();
  35. this.pctStat = new System.Windows.Forms.PictureBox();
  36. this.timer1 = new System.Windows.Forms.Timer(this.components);
  37. ((System.ComponentModel.ISupportInitialize)(this.btnClose)).BeginInit();
  38. ((System.ComponentModel.ISupportInitialize)(this.pctStat)).BeginInit();
  39. this.SuspendLayout();
  40. //
  41. // lblMsg
  42. //
  43. this.lblMsg.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  44. | System.Windows.Forms.AnchorStyles.Left)
  45. | System.Windows.Forms.AnchorStyles.Right)));
  46. this.lblMsg.BackColor = System.Drawing.Color.Transparent;
  47. this.lblMsg.Font = new System.Drawing.Font("微软雅黑", 12F);
  48. this.lblMsg.Location = new System.Drawing.Point(, );
  49. this.lblMsg.Name = "lblMsg";
  50. this.lblMsg.Size = new System.Drawing.Size(, );
  51. this.lblMsg.TabIndex = ;
  52. this.lblMsg.Text = "提示信息";
  53. this.lblMsg.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  54. //
  55. // btnClose
  56. //
  57. this.btnClose.Anchor = System.Windows.Forms.AnchorStyles.Right;
  58. this.btnClose.BackColor = System.Drawing.Color.Transparent;
  59. this.btnClose.Image = global::HZH_Controls.Properties.Resources.qty_delete;
  60. this.btnClose.Location = new System.Drawing.Point(, );
  61. this.btnClose.Name = "btnClose";
  62. this.btnClose.Size = new System.Drawing.Size(, );
  63. this.btnClose.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  64. this.btnClose.TabIndex = ;
  65. this.btnClose.TabStop = false;
  66. this.btnClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClose_MouseDown);
  67. //
  68. // pctStat
  69. //
  70. this.pctStat.Anchor = System.Windows.Forms.AnchorStyles.Left;
  71. this.pctStat.BackColor = System.Drawing.Color.Transparent;
  72. this.pctStat.Image = global::HZH_Controls.Properties.Resources.alarm;
  73. this.pctStat.Location = new System.Drawing.Point(, );
  74. this.pctStat.Name = "pctStat";
  75. this.pctStat.Size = new System.Drawing.Size(, );
  76. this.pctStat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  77. this.pctStat.TabIndex = ;
  78. this.pctStat.TabStop = false;
  79. //
  80. // timer1
  81. //
  82. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  83. //
  84. // FrmTips
  85. //
  86. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  87. this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
  88. this.ClientSize = new System.Drawing.Size(, );
  89. this.Controls.Add(this.btnClose);
  90. this.Controls.Add(this.lblMsg);
  91. this.Controls.Add(this.pctStat);
  92. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  93. this.IsFullSize = false;
  94. this.IsShowRegion = true;
  95. this.Name = "FrmTips";
  96. this.ShowIcon = false;
  97. this.ShowInTaskbar = false;
  98. this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  99. this.Text = "FrmTips";
  100. this.TopMost = true;
  101. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmTips_FormClosing);
  102. this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmTips_FormClosed);
  103. this.Load += new System.EventHandler(this.FrmTips_Load);
  104. ((System.ComponentModel.ISupportInitialize)(this.btnClose)).EndInit();
  105. ((System.ComponentModel.ISupportInitialize)(this.pctStat)).EndInit();
  106. this.ResumeLayout(false);
  107.  
  108. }
  109.  
  110. #endregion
  111.  
  112. private System.Windows.Forms.PictureBox pctStat;
  113. private System.Windows.Forms.Label lblMsg;
  114. private System.Windows.Forms.PictureBox btnClose;
  115. private System.Windows.Forms.Timer timer1;
  116. }
  117. }

用处及效果

用处:向用户提示一些信息,但是这些信息又不是非常重要,不需要用户确定的时候可以用到这个东西

效果:

调用示例

  1. FrmTips.ShowTipsError(this, "Error提示信息");
  2. FrmTips.ShowTipsInfo(this, "Info提示信息");
  3. FrmTips.ShowTipsSuccess(this, "Success提示信息");
  4. FrmTips.ShowTipsWarning(this, "Warning提示信息");

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

(二十一)c#Winform自定义控件-气泡提示的更多相关文章

  1. (三十二)c#Winform自定义控件-表格

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  3. (四十二)c#Winform自定义控件-进度条扩展

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  4. (二)c#Winform自定义控件-按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  5. (二十二)c#Winform自定义控件-半透明窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (五十一)c#Winform自定义控件-文字提示-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  7. (十二)c#Winform自定义控件-分页控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (五十二)c#Winform自定义控件-LED数字

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (六十二)c#Winform自定义控件-警灯(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. 浅谈 Attention 机制的理解

    什么是注意力机制? 注意力机制模仿了生物观察行为的内部过程,即一种将内部经验和外部感觉对齐从而增加部分区域的观察精细度的机制.例如人的视觉在处理一张图片时,会通过快速扫描全局图像,获得需要重点关注的目 ...

  2. 硬件笔记之Thinkpad T470P更换2K屏幕

    0x00 前言 手上的Thinkpad T470P屏幕是1920x1080的屏幕,色域范围NTSC 45%,作为一块办公用屏是正常配置,但是考虑到色彩显示和色域范围,计划升级到2K屏幕. 2k屏幕参数 ...

  3. KVM :vnc 远程控制kvm创建虚拟机

    一.vnc远程控制服务器 前期准备: 1.编辑/etc/hosts vi /etc/hosts 10.1.16.32 kvm 2.关闭防火墙 service iptables stop 3.关闭sel ...

  4. java学习笔记(基础篇)—java数组

    一:什么是数组,什么时候使用数组? 数组是用来保存一组数据类型相同的元素的有序集合,数组中的每个数据称为元素.有序集合可以按照顺序或者下标取数组中的元素. 在Java中,数组也是Java对象.数组中的 ...

  5. Storm 实时读取本地文件操作(模拟分析网络日志)

    WebLogProduct 产生日志类 package top.wintp.weblog; import java.io.FileNotFoundException; import java.io.F ...

  6. nginx配置目录访问&用户名密码控制

    背景 项目上需要一些共享目录让外地同事可以网页访问对应的文件,且受权限控制: 现有环境: centos nginx 你可以了解到以下内容: 配置nginx开启目录访问 并配置nginx用户名和密码进行 ...

  7. MVC设计模式与Java Web经典三层架构

    MVC设计模式 MVC的概念 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控 ...

  8. 《VR入门系列教程》之3---运动追踪与输入设备

    运动追踪设备    第二种可以使人脑相信它真实处于虚拟世界的关键技术就是运动追踪技术,它可以通过追踪头部的运动状态实时更新渲染的场景.这与我们在真实世界中观看周围非常类似.    高速的惯性测量单元( ...

  9. swift对象存储

    swift对象存储 简介 OpenStack Object Storage(Swift)是OpenStack开源云计算项目的子项目之一,被称为对象存储,提供了强大的扩展性.冗余和持久性.对象存储,用于 ...

  10. linux初学者-输出输入管理

      1.输出重定向 在linux中,因为用户的权限不同,所以访问某些文件或者目录会被拒绝而形成错误输出,这些错误的输出也会显示出来.一般正确输出的编号为1,错误输出的编号为2.如下图,在普通用户stu ...