151 lines
4.3 KiB
C#
151 lines
4.3 KiB
C#
using Modbus.Device;
|
||
using Sunny.UI;
|
||
using System.Diagnostics;
|
||
using System.Net.Sockets;
|
||
using System.Timers;
|
||
using 外科辅料和患者防护罩激光抗性测试仪.Data;
|
||
|
||
namespace 外科辅料和患者防护罩激光抗性测试仪
|
||
{
|
||
public partial class MainForm : UIForm
|
||
{
|
||
private Stopwatch pressStopwatch;
|
||
private const int LONG_PRESS_THRESHOLD = 1000; // 1000毫秒=1秒
|
||
|
||
|
||
System.Timers.Timer Timer;
|
||
System.Timers.Timer _datetimeTimer;
|
||
|
||
|
||
public MainForm()
|
||
{
|
||
InitializeComponent();
|
||
InitDateTimeTimer();
|
||
pressStopwatch = new Stopwatch();
|
||
|
||
}
|
||
|
||
private void InitDateTimeTimer()
|
||
{
|
||
_datetimeTimer = new System.Timers.Timer(1000);
|
||
// 绑定定时器触发事件(核心:刷新时间)
|
||
_datetimeTimer.Elapsed += DateTimeTimer_Elapsed;
|
||
// 设置定时器启动时立即执行一次
|
||
_datetimeTimer.AutoReset = true;
|
||
_datetimeTimer.Enabled = true;
|
||
// 初始化时手动刷新一次(避免窗体加载后1秒才显示时间)
|
||
UpdateDateTimeLabel();
|
||
}
|
||
|
||
|
||
private void DateTimeTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||
{
|
||
if (uiLabel6.InvokeRequired)
|
||
{
|
||
uiLabel6.Invoke(new Action(UpdateDateTimeLabel));
|
||
}
|
||
else
|
||
{
|
||
UpdateDateTimeLabel();
|
||
}
|
||
}
|
||
|
||
private void UpdateDateTimeLabel()
|
||
{
|
||
string currentDateTime = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
|
||
uiLabel6.Text = currentDateTime;
|
||
}
|
||
|
||
|
||
|
||
private void SwitchToForm<T>() where T : Form, new()
|
||
{
|
||
using (T form = new T())
|
||
{
|
||
this.Hide();
|
||
form.ShowDialog();
|
||
if (!this.IsDisposed)
|
||
{
|
||
this.Show();
|
||
this.Activate();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
// 关键:关闭窗体时销毁定时器,释放资源(防止内存泄漏)
|
||
if (_datetimeTimer != null)
|
||
{
|
||
_datetimeTimer.Elapsed -= DateTimeTimer_Elapsed;
|
||
_datetimeTimer.Stop();
|
||
_datetimeTimer.Dispose();
|
||
}
|
||
// 退出应用程序
|
||
if (e.CloseReason == CloseReason.UserClosing)
|
||
{
|
||
ModbusResourceManager.Instance?.Dispose();
|
||
Application.Exit();
|
||
}
|
||
}
|
||
|
||
private void uiButton1_Click(object sender, EventArgs e)
|
||
{
|
||
this.SwitchToForm<PrimaryIgnitionForm>();//初次
|
||
}
|
||
|
||
private void uiButton3_Click(object sender, EventArgs e)
|
||
{
|
||
this.SwitchToForm<PenetrationForm>();//穿透
|
||
}
|
||
|
||
private void uiButton2_Click(object sender, EventArgs e)
|
||
{
|
||
this.SwitchToForm<SecondaryIgnitionForm>();//二次
|
||
}
|
||
|
||
private void uiButton6_MouseUp(object sender, MouseEventArgs e)
|
||
{
|
||
if (e.Button == MouseButtons.Left)
|
||
{
|
||
pressStopwatch.Stop();
|
||
if (pressStopwatch.ElapsedMilliseconds >= LONG_PRESS_THRESHOLD)
|
||
{
|
||
// 执行长按操作
|
||
EnterFunction();
|
||
}
|
||
//else
|
||
//{
|
||
// // 执行点击操作(可选)
|
||
// ClickFunction();
|
||
//}
|
||
}
|
||
}
|
||
|
||
private void EnterFunction()
|
||
{
|
||
// 长按后进入的功能
|
||
this.SwitchToForm<ParameterSetting>();//参数
|
||
}
|
||
|
||
private void uiButton6_MouseDown(object sender, MouseEventArgs e)
|
||
{
|
||
if (e.Button == MouseButtons.Left)
|
||
{
|
||
pressStopwatch.Restart();
|
||
}
|
||
}
|
||
|
||
private void uiButton5_Click(object sender, EventArgs e)
|
||
{
|
||
this.SwitchToForm<ManualDebugForm>();//手动调试
|
||
}
|
||
|
||
private void uiButton4_Click(object sender, EventArgs e)
|
||
{
|
||
this.SwitchToForm<ReportForm>();//测试报告
|
||
}
|
||
|
||
}
|
||
}
|