FloatForm.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Oreo.NetMonitor.Commons;
  2. using Oreo.NetMonitor.Services;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using Y.Utils.DataUtils.UnitConvertUtils;
  14. namespace Oreo.NetMonitor.Views
  15. {
  16. public partial class FloatForm : Form
  17. {
  18. NetDetailForm monitor;
  19. NetReportForm report;
  20. public FloatForm()
  21. {
  22. InitializeComponent();
  23. }
  24. private void FloatForm_Load(object sender, EventArgs e)
  25. {
  26. #region 窗口设置
  27. this.ShowInTaskbar = false;
  28. FormBorderStyle = FormBorderStyle.None;
  29. BackgroundImageLayout = ImageLayout.None;
  30. Color c = Color.Green;
  31. TransparencyKey = c;
  32. Width = 140;
  33. Height = 60;
  34. int iActulaWidth = Screen.PrimaryScreen.Bounds.Width;
  35. int iActulaHeight = Screen.PrimaryScreen.Bounds.Height;
  36. this.Left = iActulaWidth - this.Width - 50;
  37. this.Top = iActulaHeight - this.Height - 100;
  38. #endregion
  39. try
  40. {
  41. CpuLoad();
  42. NetFlow();
  43. ProConn();
  44. UI();
  45. }
  46. catch { }
  47. }
  48. #region 菜单功能
  49. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  50. {
  51. MessageBox.Show("技术支持:诺亚信息", "帮助");
  52. }
  53. private void toolStripMenuItem1_Click(object sender, EventArgs e)
  54. {
  55. if (monitor == null || monitor.IsDisposed)
  56. {
  57. monitor = new NetDetailForm();
  58. monitor.Show();
  59. }
  60. }
  61. #endregion
  62. #region 开启检测
  63. private void CpuLoad()
  64. {
  65. R.Log.v("CpuLoad 1");
  66. if (NetWorkService.CpuLoadLoop)
  67. {
  68. R.Log.v("CpuLoad 2");
  69. NetWorkService.CpuLoadLoop = false;
  70. }
  71. else
  72. {
  73. R.Log.v("CpuLoad 3");
  74. NetWorkService.CpuLoadLoop = true;
  75. NetWorkService.StartCpuLoad();
  76. }
  77. }
  78. private void NetFlow()
  79. {
  80. if (NetWorkService.NetFlowLoop)
  81. {
  82. NetWorkService.NetFlowLoop = false;
  83. }
  84. else
  85. {
  86. NetWorkService.NetFlowLoop = true;
  87. NetWorkService.StartNetFlow();
  88. }
  89. }
  90. private void ProConn()
  91. {
  92. if (NetWorkService.ProConnLoop)
  93. {
  94. NetWorkService.ProConnLoop = false;
  95. }
  96. else
  97. {
  98. NetWorkService.ProConnLoop = true;
  99. NetWorkService.StartConnectCheck();
  100. }
  101. }
  102. #endregion
  103. #region 刷新界面
  104. private void UI()
  105. {
  106. Task.Factory.StartNew(() =>
  107. {
  108. try
  109. {
  110. while (!this.IsDisposed)
  111. {
  112. this.Invoke(new Action(() =>
  113. {
  114. if (!this.IsDisposed)
  115. {
  116. label1.Text = ByteConvertTool.Fmt(NetWorkService.NowSent) + "/s";
  117. label2.Text = ByteConvertTool.Fmt(NetWorkService.NowReceived) + "/s";
  118. label6.Text = Math.Floor(NetWorkService.CpuLoad) + "%";
  119. //流量超出报警
  120. if ((NetWorkService.UnitSent + NetWorkService.UnitReceived) > NetWorkService.FlowThreshold)
  121. {
  122. if (report == null || report.IsDisposed)
  123. {
  124. report = new NetReportForm();
  125. report.Show();
  126. }
  127. }
  128. }
  129. }));
  130. Thread.Sleep(1000);
  131. }
  132. }
  133. catch
  134. { }
  135. });
  136. }
  137. #endregion
  138. #region 窗体拖动及右键菜单
  139. Point mouseOff;//鼠标移动位置变量
  140. bool leftFlag;//标签是否为左键
  141. private void FloatForm_MouseMove(object sender, MouseEventArgs e)
  142. {
  143. if (leftFlag)
  144. {
  145. Point mouseSet = Control.MousePosition;
  146. mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置
  147. Location = mouseSet;
  148. }
  149. }
  150. private void FloatForm_MouseDown(object sender, MouseEventArgs e)
  151. {
  152. if (e.Button == MouseButtons.Left)
  153. {
  154. mouseOff = new Point(-e.X, -e.Y); //得到变量的值
  155. leftFlag = true; //点击左键按下时标注为true;
  156. }
  157. }
  158. private void FloatForm_MouseUp(object sender, MouseEventArgs e)
  159. {
  160. if (leftFlag)
  161. {
  162. leftFlag = false;//释放鼠标后标注为false;
  163. }
  164. if (e.Button == MouseButtons.Right)
  165. {
  166. contextMenuStrip1.Show(this, e.Location);
  167. }
  168. }
  169. #endregion
  170. private void label5_Click(object sender, EventArgs e)
  171. {
  172. //Task.Factory.StartNew(() =>
  173. //{
  174. // bool send_res = Tools.SendServerMsg("37", "37");
  175. //});
  176. }
  177. }
  178. }