AutoSMTRXYSetting.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace FileWatcher
  5. {
  6. public partial class AutoSMTRXYSetting : Form
  7. {
  8. /// <summary>
  9. /// 设置参数解锁密码
  10. /// </summary>
  11. private const string SettingPassword = "1234";
  12. /// <summary>
  13. /// 是否已通过密码解锁
  14. /// </summary>
  15. private bool UnLocked = false;
  16. public AutoSMTRXYSetting()
  17. {
  18. InitializeComponent();
  19. StartPosition = FormStartPosition.CenterScreen;
  20. }
  21. private void AutoSMTRXYSetting_Load(object sender, EventArgs e)
  22. {
  23. //低分辨率屏幕下若窗口超出可视区域,则整体等比缩小,保证按钮完整可见
  24. Rectangle workArea = Screen.GetWorkingArea(this);
  25. if (this.Height > workArea.Height || this.Width > workArea.Width)
  26. {
  27. float scale = Math.Min((float)workArea.Width / this.Width, (float)workArea.Height / this.Height);
  28. this.Scale(new SizeF(scale, scale));
  29. }
  30. ComPort.Text = GetCacheString("ComPort");
  31. BaudRate.Text = GetCacheString("BaudRate");
  32. Read.Text = GetCacheString("Read");
  33. Write.Text = GetCacheString("Write");
  34. Stop.Text = GetCacheString("Stop");
  35. ToZero.Text = GetCacheString("ToZero");
  36. UseScan.Text = GetCacheString("UseScan");
  37. CountType.Text = GetCacheString("CountType");
  38. if (BaudRate.Text == "")
  39. {
  40. BaudRate.Text = "9600";
  41. }
  42. if (CountType.Text == "")
  43. {
  44. CountType.Text = "PLC";
  45. }
  46. LockControls();
  47. }
  48. private string GetCacheString(string ParamName)
  49. {
  50. object value = BaseUtil.GetCacheData(ParamName);
  51. return value == null ? "" : value.ToString();
  52. }
  53. /// <summary>
  54. /// 锁定设置项(未输入密码前不可编辑)
  55. /// </summary>
  56. private void LockControls()
  57. {
  58. UnLocked = false;
  59. ComPort.Enabled = false;
  60. BaudRate.Enabled = false;
  61. Read.Enabled = false;
  62. Write.Enabled = false;
  63. Stop.Enabled = false;
  64. ToZero.Enabled = false;
  65. UseScan.Enabled = false;
  66. CountType.Enabled = false;
  67. Password.Text = "";
  68. Confirm.Text = "确定";
  69. }
  70. /// <summary>
  71. /// 密码正确后解锁设置项
  72. /// </summary>
  73. private void UnLockControls()
  74. {
  75. UnLocked = true;
  76. ComPort.Enabled = true;
  77. BaudRate.Enabled = true;
  78. Read.Enabled = true;
  79. Write.Enabled = true;
  80. Stop.Enabled = true;
  81. ToZero.Enabled = true;
  82. UseScan.Enabled = true;
  83. CountType.Enabled = true;
  84. Password.Text = "";
  85. Confirm.Text = "保存";
  86. }
  87. private void Confirm_Click(object sender, EventArgs e)
  88. {
  89. if (!UnLocked)
  90. {
  91. //未解锁时先校验密码
  92. if (Password.Text == SettingPassword)
  93. {
  94. UnLockControls();
  95. }
  96. else
  97. {
  98. MessageBox.Show(this, "密码不对", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  99. }
  100. return;
  101. }
  102. //解锁状态下点击则保存并关闭
  103. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  104. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  105. BaseUtil.SetCacheData("Read", Read.Text);
  106. BaseUtil.SetCacheData("Write", Write.Text);
  107. BaseUtil.SetCacheData("Stop", Stop.Text);
  108. BaseUtil.SetCacheData("ToZero", ToZero.Text);
  109. BaseUtil.SetCacheData("UseScan", UseScan.Text);
  110. BaseUtil.SetCacheData("CountType", CountType.Text);
  111. MessageBox.Show(this, "保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  112. DialogResult = DialogResult.OK;
  113. Close();
  114. }
  115. private void Cancel_Click(object sender, EventArgs e)
  116. {
  117. Close();
  118. }
  119. }
  120. }