FrmMain.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Core.ONH.Collection.comm;
  10. namespace Core.ONH.Collection.Receive
  11. {
  12. public partial class FrmMain : Form
  13. {
  14. public FrmMain()
  15. {
  16. InitializeComponent();
  17. SerialPortHelper.Instance().ShowMessage = ( s) =>
  18. {
  19. SafeRefreshControl(txtMessageShow, () =>
  20. {
  21. if (txtMessageShow.Text.Length > 10000)
  22. {
  23. txtMessageShow.Text = "";
  24. }
  25. txtMessageShow.Text = DateTime.Now.ToString("HH:mm:ss") + " " + s + "\r\n" + txtMessageShow.Text;
  26. });
  27. };
  28. }
  29. private void msMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  30. {
  31. switch (e.ClickedItem.Name)
  32. {
  33. case "tsmComSetting"://串口设置窗口
  34. FrmSetting setCom = new FrmSetting();
  35. setCom.ShowDialog();
  36. break;
  37. case "tsmClearData"://数据库连接设置
  38. txtMessageShow.Text = "";
  39. break;
  40. case "tsmExit"://退出
  41. Close();
  42. break;
  43. }
  44. }
  45. /// <summary>
  46. /// 安全刷新界面控件 当为子线程时委托刷新界面
  47. /// </summary>
  48. /// <param name="control"></param>
  49. /// <param name="action"></param>
  50. public void SafeRefreshControl(Control control, Action action)
  51. {
  52. if (control.InvokeRequired)
  53. {
  54. control.Invoke(action);
  55. }
  56. else
  57. action();
  58. }
  59. private void FrmMain_Load(object sender, EventArgs e)
  60. {
  61. SerialPortHelper.Instance().InitRecive();
  62. }
  63. private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
  64. {
  65. if (MessageBox.Show(this, "确认退出系统?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
  66. DialogResult.Yes)
  67. {
  68. SerialPortHelper.Instance().CloseRecive();
  69. }
  70. else
  71. {
  72. e.Cancel = true;
  73. }
  74. }
  75. private void FrmMain_Resize(object sender, EventArgs e)
  76. {
  77. if (this.WindowState == FormWindowState.Minimized)
  78. {
  79. this.WindowState = FormWindowState.Minimized;
  80. this.Visible = false;
  81. this.notifyIcon1.Visible = true;
  82. this.notifyIcon1.ShowBalloonTip(2000,"提示","采集软件将在后台运行",ToolTipIcon.Info);
  83. }
  84. }
  85. private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
  86. {
  87. notifyIcon1.Visible = false;
  88. this.ShowInTaskbar = true;
  89. this.Show();
  90. this.Activate();
  91. this.WindowState = FormWindowState.Normal;
  92. }
  93. }
  94. }