| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace FileWatcher
- {
- public partial class AutoSMTRXYSetting : Form
- {
- /// <summary>
- /// 设置参数解锁密码
- /// </summary>
- private const string SettingPassword = "1234";
- /// <summary>
- /// 是否已通过密码解锁
- /// </summary>
- private bool UnLocked = false;
- public AutoSMTRXYSetting()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- private void AutoSMTRXYSetting_Load(object sender, EventArgs e)
- {
- //低分辨率屏幕下若窗口超出可视区域,则整体等比缩小,保证按钮完整可见
- Rectangle workArea = Screen.GetWorkingArea(this);
- if (this.Height > workArea.Height || this.Width > workArea.Width)
- {
- float scale = Math.Min((float)workArea.Width / this.Width, (float)workArea.Height / this.Height);
- this.Scale(new SizeF(scale, scale));
- }
- ComPort.Text = GetCacheString("ComPort");
- BaudRate.Text = GetCacheString("BaudRate");
- Read.Text = GetCacheString("Read");
- Write.Text = GetCacheString("Write");
- Stop.Text = GetCacheString("Stop");
- ToZero.Text = GetCacheString("ToZero");
- UseScan.Text = GetCacheString("UseScan");
- CountType.Text = GetCacheString("CountType");
- if (BaudRate.Text == "")
- {
- BaudRate.Text = "9600";
- }
- if (CountType.Text == "")
- {
- CountType.Text = "PLC";
- }
- LockControls();
- }
- private string GetCacheString(string ParamName)
- {
- object value = BaseUtil.GetCacheData(ParamName);
- return value == null ? "" : value.ToString();
- }
- /// <summary>
- /// 锁定设置项(未输入密码前不可编辑)
- /// </summary>
- private void LockControls()
- {
- UnLocked = false;
- ComPort.Enabled = false;
- BaudRate.Enabled = false;
- Read.Enabled = false;
- Write.Enabled = false;
- Stop.Enabled = false;
- ToZero.Enabled = false;
- UseScan.Enabled = false;
- CountType.Enabled = false;
- Password.Text = "";
- Confirm.Text = "确定";
- }
- /// <summary>
- /// 密码正确后解锁设置项
- /// </summary>
- private void UnLockControls()
- {
- UnLocked = true;
- ComPort.Enabled = true;
- BaudRate.Enabled = true;
- Read.Enabled = true;
- Write.Enabled = true;
- Stop.Enabled = true;
- ToZero.Enabled = true;
- UseScan.Enabled = true;
- CountType.Enabled = true;
- Password.Text = "";
- Confirm.Text = "保存";
- }
- private void Confirm_Click(object sender, EventArgs e)
- {
- if (!UnLocked)
- {
- //未解锁时先校验密码
- if (Password.Text == SettingPassword)
- {
- UnLockControls();
- }
- else
- {
- MessageBox.Show(this, "密码不对", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- return;
- }
- //解锁状态下点击则保存并关闭
- BaseUtil.SetCacheData("ComPort", ComPort.Text);
- BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
- BaseUtil.SetCacheData("Read", Read.Text);
- BaseUtil.SetCacheData("Write", Write.Text);
- BaseUtil.SetCacheData("Stop", Stop.Text);
- BaseUtil.SetCacheData("ToZero", ToZero.Text);
- BaseUtil.SetCacheData("UseScan", UseScan.Text);
- BaseUtil.SetCacheData("CountType", CountType.Text);
- MessageBox.Show(this, "保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- DialogResult = DialogResult.OK;
- Close();
- }
- private void Cancel_Click(object sender, EventArgs e)
- {
- Close();
- }
- }
- }
|