FormPopup.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. namespace HslCommunication.BasicFramework
  11. {
  12. /// <summary>
  13. /// 一个用于消息弹出显示的类
  14. /// </summary>
  15. public partial class FormPopup : Form
  16. {
  17. //用于维护所有的弹出消息窗口
  18. private static List<FormPopup> FormsPopup = new List<FormPopup>();
  19. /// <summary>
  20. /// 新增一个显示的弹出窗口
  21. /// </summary>
  22. /// <param name="form"></param>
  23. private static void AddNewForm(FormPopup form)
  24. {
  25. try
  26. {
  27. foreach (var m in FormsPopup)
  28. {
  29. m.LocationUpMove();
  30. }
  31. FormsPopup.Add(form);
  32. }
  33. catch
  34. {
  35. }
  36. }
  37. /// <summary>
  38. /// 重置所有弹出窗口的位置
  39. /// </summary>
  40. private static void ResetLocation()
  41. {
  42. try
  43. {
  44. for (int i = 0; i < FormsPopup.Count; i++)
  45. {
  46. FormsPopup[i].LocationUpMove(FormsPopup.Count - 1 - i);
  47. }
  48. }
  49. catch
  50. {
  51. }
  52. }
  53. /// <summary>
  54. /// 实例化一个窗口信息弹出的对象
  55. /// </summary>
  56. public FormPopup()
  57. {
  58. InitializeComponent();
  59. }
  60. /// <summary>
  61. /// 实例化一个窗口信息弹出的对象
  62. /// </summary>
  63. /// <param name="infotext">需要显示的文本</param>
  64. public FormPopup(string infotext)
  65. {
  66. InitializeComponent();
  67. InfoText = infotext;
  68. }
  69. /// <summary>
  70. /// 实例化一个窗口信息弹出的对象
  71. /// </summary>
  72. /// <param name="infotext">需要显示的文本</param>
  73. /// <param name="infocolor">文本的颜色</param>
  74. public FormPopup(string infotext,Color infocolor)
  75. {
  76. InitializeComponent();
  77. InfoText = infotext;
  78. InfoColor = infocolor;
  79. }
  80. /// <summary>
  81. /// 实例化一个窗口信息弹出的对象
  82. /// </summary>
  83. /// <param name="infotext">需要显示的文本</param>
  84. /// <param name="infocolor">文本的颜色</param>
  85. /// <param name="existTime">指定窗口多少时间后消失,单位毫秒</param>
  86. public FormPopup(string infotext, Color infocolor,int existTime)
  87. {
  88. InitializeComponent();
  89. InfoText = infotext;
  90. InfoColor = infocolor;
  91. InfoExistTime = existTime;
  92. }
  93. private string InfoText { get; set; } = "这是一条测试消息";
  94. private Color InfoColor { get; set; } = Color.DimGray;
  95. private int InfoExistTime { get; set; } = -1;
  96. //下面是可用的常量,按照不合的动画结果声明本身须要的
  97. private const int AW_HOR_POSITIVE = 0x0001;//自左向右显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记
  98. private const int AW_HOR_NEGATIVE = 0x0002;//自右向左显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记
  99. private const int AW_VER_POSITIVE = 0x0004;//自顶向下显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记
  100. private const int AW_VER_NEGATIVE = 0x0008;//自下向上显示窗口,该标记可以在迁移转变动画和滑动动画中应用。应用AW_CENTER标记时忽视该标记该标记
  101. private const int AW_CENTER = 0x0010;//若应用了AW_HIDE标记,则使窗口向内重叠;不然向外扩大
  102. private const int AW_HIDE = 0x10000;//隐蔽窗口
  103. private const int AW_ACTIVE = 0x20000;//激活窗口,在应用了AW_HIDE标记后不要应用这个标记
  104. private const int AW_SLIDE = 0x40000;//应用滑动类型动画结果,默认为迁移转变动画类型,当应用AW_CENTER标记时,这个标记就被忽视
  105. private const int AW_BLEND = 0x80000;//应用淡入淡出结果
  106. private void FormPopup_Load(object sender, EventArgs e)
  107. {
  108. label1.Text = InfoText;
  109. label1.ForeColor = InfoColor;
  110. AddNewForm(this);
  111. int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
  112. int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
  113. this.Location = new Point(x, y);//设置窗体在屏幕右下角显示
  114. AnimateWindow(this.Handle, 1000, AW_SLIDE | AW_VER_NEGATIVE);
  115. TopMost = true;
  116. if (InfoExistTime > 100)
  117. {
  118. Timer time = new Timer();
  119. time.Interval = InfoExistTime;
  120. time.Tick += delegate
  121. {
  122. if (IsHandleCreated)
  123. {
  124. time.Dispose();
  125. AnimateWindow(this.Handle, 1000, AW_BLEND | AW_HIDE);
  126. Close();
  127. }
  128. };
  129. time.Start();
  130. }
  131. }
  132. /// <summary>
  133. /// 窗体的位置进行向上调整
  134. /// </summary>
  135. public void LocationUpMove()
  136. {
  137. this.Location = new Point(this.Location.X, this.Location.Y - Height);
  138. }
  139. /// <summary>
  140. /// 窗体的位置进行向上调整
  141. /// </summary>
  142. public void LocationUpMove(int index)
  143. {
  144. this.Location = new Point(this.Location.X,
  145. Screen.PrimaryScreen.WorkingArea.Bottom - this.Height - index * this.Height);
  146. }
  147. private void FormPopup_Closing(object sender, FormClosingEventArgs e)
  148. {
  149. //AnimateWindow(this.Handle, 1000, AW_BLEND | AW_HIDE);
  150. try
  151. {
  152. FormsPopup.Remove(this);
  153. ResetLocation();
  154. }
  155. catch
  156. {
  157. }
  158. Dispose();
  159. }
  160. [DllImport("user32")]
  161. private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
  162. private void FormPopup_Paint(object sender, PaintEventArgs e)
  163. {
  164. Graphics g = e.Graphics;
  165. //绘制标题
  166. g.FillRectangle(Brushes.SkyBlue, new Rectangle(0, 0, Width - 1, 30));
  167. StringFormat sf = new StringFormat()
  168. {
  169. Alignment = StringAlignment.Near,
  170. LineAlignment = StringAlignment.Center,
  171. };
  172. g.DrawString("消息提示:", label2.Font, Brushes.DimGray, new Rectangle(5, 0, Width - 1, 30), sf);
  173. g.DrawRectangle(Pens.DimGray, 0, 0, Width - 1, Height - 1);
  174. }
  175. private void label2_Click(object sender, EventArgs e)
  176. {
  177. //关闭
  178. Close();
  179. }
  180. private void label2_MouseEnter(object sender, EventArgs e)
  181. {
  182. //label2.BackColor = Color.Tomato;
  183. SoftAnimation.BeginBackcolorAnimation(label2, Color.Tomato, 100);
  184. }
  185. private void label2_MouseLeave(object sender, EventArgs e)
  186. {
  187. //label2.BackColor = Color.MistyRose;
  188. SoftAnimation.BeginBackcolorAnimation(label2, Color.MistyRose, 100);
  189. }
  190. }
  191. }