MiniForm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Threading;
  5. using GDNXFD.Data;
  6. namespace IntelligentControlForsx
  7. {
  8. public partial class MiniForm : UserControl
  9. {
  10. private BigForm2 bigForm = null;
  11. private Thread monitorAlarmThread = null;
  12. private System.Drawing.Point mouseOffset;
  13. private ToolStripMenuItem currentOpacityItem = null;
  14. public MiniFormLocation miniFormLocation;
  15. private bool isMouseDown = false;
  16. public bool isMouseEnter = false;
  17. public int miniBigFormSpace = 5;
  18. public int miniFormWidth = 371;
  19. public int miniFormHeight = 94;
  20. private int maxRank = 0;
  21. private int preMaxRank = 0;
  22. private int totalAlarms = 0;
  23. /*移动时小球出现在bigForm窗体的位置方向枚举*/
  24. public enum MiniFormLocation
  25. {
  26. topLeft,
  27. topRigh,
  28. bottomLeft,
  29. bottomRight
  30. }
  31. public MiniForm()
  32. {
  33. // Control.CheckForIllegalCrossThreadCalls = false;
  34. InitializeComponent();
  35. StartAlarmMonitor(); //启动报警监视
  36. }
  37. #region 报警监视
  38. /*开始监视报警数据*/
  39. private void StartAlarmMonitor()
  40. {
  41. AlertBackWorker.Instance.Start();
  42. refreshTimer.Start();
  43. }
  44. private void refreshTimer_Tick(object sender, EventArgs e)
  45. {
  46. alarmMonitor();
  47. }
  48. private void alarmMonitor()
  49. {
  50. string thingName = "";
  51. string alarmText = "";
  52. DateTime? alarmDate = null;
  53. Color? color = null;
  54. int rank = 0;
  55. int total = 0;
  56. if (ClientCache.Instance.AlertSnaps != null && ClientCache.Instance.AlertSnaps.Count > 0)
  57. {
  58. AlertSnap snap = ClientCache.Instance.AlertSnaps[0];
  59. thingName = snap.ObjectName;
  60. alarmText = snap.AlertText;
  61. alarmDate = snap.LastUpdateTime;
  62. color = snap.getBackGroundColor();
  63. int.TryParse(snap.Rank, out rank);
  64. total = ClientCache.Instance.AlertSnaps.Count;
  65. }
  66. string timeString = alarmDate.HasValue ? alarmDate.Value.ToString("HH:mm:ss") : "-";
  67. lblAlarmTime.Text = timeString;
  68. lblObjectName.Text = thingName;
  69. lblAlarmText.Text = alarmText;
  70. lblAlarmTime.BackColor = color.HasValue ? color.Value : Color.LightBlue;
  71. lblObjectName.BackColor = color.HasValue ? color.Value : Color.LightBlue;
  72. lblAlarmText.BackColor = color.HasValue ? color.Value : Color.LightBlue;
  73. maxRank = rank;
  74. totalAlarms = total;
  75. paintMiniBallControl();
  76. }
  77. /*刷新报警紧急程度*/
  78. private void paintMiniBallControl()
  79. {
  80. if (totalAlarms < 10)
  81. {
  82. // lblAlarmCount.Location = new System.Drawing.Point(33, 29);
  83. }
  84. else if (totalAlarms < 100)
  85. {
  86. // lblAlarmCount.Location = new System.Drawing.Point(29, 29);
  87. }
  88. else if (totalAlarms < 1000)
  89. {
  90. //lblAlarmCount.Location = new System.Drawing.Point(24, 29);
  91. }
  92. else if (totalAlarms < 10000)
  93. {
  94. //lblAlarmCount.Location = new System.Drawing.Point(19, 29);
  95. }
  96. // lblAlarmCount.Text = totalAlarms + "";
  97. if (preMaxRank != maxRank)
  98. {
  99. preMaxRank = maxRank;
  100. //if (maxRank == 5)
  101. //{
  102. // this.pnlBall.BackgroundImage = global::AlarmUI.Properties.Resources.红色球;
  103. //}
  104. //else if (maxRank == 4)
  105. //{
  106. // this.pnlBall.BackgroundImage = global::AlarmUI.Properties.Resources.橙色球;
  107. //}
  108. //else
  109. //{
  110. // this.pnlBall.BackgroundImage = global::AlarmUI.Properties.Resources.绿色球;
  111. //}
  112. }
  113. }
  114. #endregion
  115. #region 小球的鼠标事件
  116. private void miniBigFormSpace_MouseEnter(object sender, EventArgs e)
  117. {
  118. isMouseEnter = true;
  119. if (bigForm == null || !bigForm.Visible)
  120. showDetailFormTimer.Enabled = true;
  121. }
  122. private void miniBigFormSpace_MouseLeave(object sender, EventArgs e)
  123. {
  124. System.Drawing.Point p = MousePosition;
  125. if (p.X - 10 <= this.Left || p.X + 10 >= this.Left + miniFormWidth || p.Y - 10 <= this.Top || p.Y + 10 >= this.Bottom)
  126. {
  127. isMouseEnter = false;
  128. hideDetailFormTimer.Enabled = true;
  129. }
  130. }
  131. private void miniBigFormSpace_MouseDown(object sender, MouseEventArgs e)
  132. {
  133. //if (e.Button == MouseButtons.Left)
  134. //{
  135. // isMouseDown = true;
  136. // mouseOffset = new System.Drawing.Point(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y);
  137. // this.Cursor = Cursors.SizeAll;
  138. //}
  139. }
  140. private void miniBigFormSpace_MouseUp(object sender, MouseEventArgs e)
  141. {
  142. isMouseDown = false;
  143. this.Cursor = Cursors.Default;
  144. }
  145. private void miniBigFormSpace_MouseMove(object sender, MouseEventArgs e)
  146. {
  147. //if (isMouseDown == true)
  148. //{
  149. // System.Drawing.Point old = this.Location;
  150. // this.Location = getMiniBallMoveLocation();
  151. // if (old.X != this.Location.X || old.Y != this.Location.Y)
  152. // {
  153. // if (bigForm != null && bigForm.Visible)
  154. // hideDetailsForm();
  155. // }
  156. // else
  157. // {
  158. // if (bigForm != null && !bigForm.Visible)
  159. // {
  160. // isMouseEnter = true;
  161. // showDetailFormTimer.Enabled = true;
  162. // }
  163. // }
  164. //}
  165. }
  166. #endregion
  167. #region 小球和bigForm的位置方法
  168. /*小球出现的位置*/
  169. private System.Drawing.Point getMiniBallMoveLocation()
  170. {
  171. int x = MousePosition.X - mouseOffset.X;
  172. int y = MousePosition.Y - mouseOffset.Y;
  173. if (x < 0)
  174. {
  175. x = 0;
  176. }
  177. if (y < 0)
  178. {
  179. y = 0;
  180. }
  181. if (Screen.PrimaryScreen.WorkingArea.Width - x < miniFormWidth)
  182. {
  183. x = Screen.PrimaryScreen.WorkingArea.Width - miniFormWidth;
  184. }
  185. if (Screen.PrimaryScreen.WorkingArea.Height - y < miniFormHeight)
  186. {
  187. y = Screen.PrimaryScreen.WorkingArea.Height - miniFormHeight;
  188. }
  189. return new System.Drawing.Point(x, y);
  190. }
  191. /*获取bigForm出现的位置*/
  192. private System.Drawing.Point getDetailsFormLocation()
  193. {
  194. int x = 0, y = 0;
  195. //System.Drawing.Point miniBallLocation = this.Location;
  196. //if (this.Location.Y >= bigForm.Height) //minBall在bigBall下面
  197. //{
  198. // if (Screen.PrimaryScreen.WorkingArea.Width - this.Location.X <= bigForm.Width)
  199. // {
  200. // x = this.Location.X + miniFormWidth - bigForm.Width;
  201. // miniFormLocation = MiniFormLocation.bottomRight;
  202. // }
  203. // else
  204. // {
  205. // x = this.Location.X;
  206. // miniFormLocation = MiniFormLocation.bottomLeft;
  207. // }
  208. // y = this.Location.Y - bigForm.Height - miniBigFormSpace;
  209. //}
  210. //else if (this.Location.Y < bigForm.Height) //minBall在bigBall上面
  211. //{
  212. // if (Screen.PrimaryScreen.WorkingArea.Width - this.Location.X > bigForm.Width)
  213. // {
  214. // x = this.Location.X;
  215. // miniFormLocation = MiniFormLocation.topLeft;
  216. // }
  217. // else
  218. // {
  219. // x = this.Location.X + miniFormWidth - bigForm.Width;
  220. // miniFormLocation = MiniFormLocation.topRigh;
  221. // }
  222. // y = this.Location.Y + miniFormHeight + miniBigFormSpace;
  223. //}
  224. x = 1920 - bigForm.Width;
  225. y = 1038 - bigForm.Height;
  226. return new System.Drawing.Point(x, y);
  227. }
  228. #endregion
  229. #region 显示和隐藏detailForm的方法和定时器
  230. /*隐藏bigForm*/
  231. private void hideDetailsForm()
  232. {
  233. if (bigForm != null && bigForm.Visible)
  234. {
  235. bigForm.Hide();
  236. }
  237. }
  238. /*显示bigForm*/
  239. private void showDetailsForm()
  240. {
  241. if (bigForm == null)
  242. {
  243. bigForm = new BigForm2(this);
  244. bigForm.Show();
  245. bigForm.Location = getDetailsFormLocation();
  246. }
  247. else if (!bigForm.Visible)
  248. {
  249. bigForm.Location = getDetailsFormLocation();
  250. bigForm.Show();
  251. }
  252. bigForm.refreshThread();
  253. }
  254. /*显示bigForm的定时器*/
  255. private void showDetailFormTimer_Tick(object sender, EventArgs e)
  256. {
  257. if (isMouseEnter)
  258. {
  259. showDetailsForm();
  260. }
  261. showDetailFormTimer.Enabled = false;
  262. }
  263. /*隐藏bigForm的定时器*/
  264. private void hideDetailFormTimer_Tick(object sender, EventArgs e)
  265. {
  266. hideDetailFormTimer.Enabled = false;
  267. if (bigForm != null && bigForm.Visible && !bigForm.isMouseEnter && !isMouseEnter)
  268. {
  269. hideDetailsForm();
  270. }
  271. if (bigForm != null && bigForm.isMouseEnter)
  272. isMouseEnter = false;
  273. }
  274. #endregion
  275. }
  276. }