From 2337f47091dbcc1a681f63fc18d9b8d28ab22296 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期日, 04 一月 2026 16:12:32 +0800
Subject: [PATCH] 优化登录界面。
---
LB_SmartVision/Program.cs | 72 ++++++++++++++++++++++++++++++++++--
1 files changed, 68 insertions(+), 4 deletions(-)
diff --git a/LB_SmartVision/Program.cs b/LB_SmartVision/Program.cs
index e0369dd..1885513 100644
--- a/LB_SmartVision/Program.cs
+++ b/LB_SmartVision/Program.cs
@@ -1,3 +1,6 @@
+using LB_SmartVisionLoginUI;
+using System.Text;
+
namespace LB_SmartVision
{
internal static class Program
@@ -8,10 +11,71 @@
[STAThread]
static void Main()
{
- // To customize application configuration such as set high DPI settings or default font,
- // see https://aka.ms/applicationconfiguration.
- ApplicationConfiguration.Initialize();
- Application.Run(new VisionForm());
+ try
+ {
+ string halconDir = Environment.GetEnvironmentVariable("HALCONROOT");
+ if (string.IsNullOrEmpty(halconDir) || !Directory.Exists(halconDir))
+ {
+ System.Windows.Forms.MessageBox.Show("环境变量缺少HALCONROOT,请确认是否安装正确!");
+ }
+ else
+ {
+ bool ret;
+ System.Threading.Mutex mutex = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out ret);
+ if (ret)
+ {
+ MainWindow.InstanceLoginandConfirmation().ShowDialog();
+ if (!MainWindow.InstanceLoginandConfirmation().isQuit && MainWindow.InstanceLoginandConfirmation().correctUser)
+ {
+ MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
+ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+ System.Windows.Forms.Application.EnableVisualStyles();
+ System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
+ System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
+ System.Windows.Forms.Application.ThreadException += (sender, e) => HandleException(e.Exception);
+ AppDomain.CurrentDomain.UnhandledException += (sender, e) => HandleException(e.ExceptionObject as Exception);
+ // To customize application configuration such as set high DPI settings or default font,
+ // see https://aka.ms/applicationconfiguration.
+ System.Windows.Forms.Application.Run(new VisionForm());
+ }
+ // Main 为你程序的主窗体,如果是控制台程序不用这句
+ mutex.ReleaseMutex();
+ }
+ else
+ {
+ System.Windows.Forms.MessageBox.Show("有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\r\n这个程序即将退出。", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
+ // 提示信息,可以删除。
+ System.Windows.Forms.Application.Exit();//退出程序
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ if (null != ex)
+ {
+ string strMsg = "InitControl failed. Error Code: " + ex.Message;
+ System.Windows.Forms.MessageBox.Show(strMsg);
+ }
+ else
+ {
+ return;
+ }
+ }
+
+ }
+
+ static void HandleException(Exception ex)
+ {
+ // 记录异常信息
+ string logPath = @"\Logs\app_crash.log";
+ Directory.CreateDirectory(Path.GetDirectoryName(logPath));
+ File.AppendAllText(logPath, $"{DateTime.Now}: {ex}\n");
+
+ // 显示错误信息(可选)
+ System.Windows.Forms.MessageBox.Show($"程序发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
+
+ // 退出程序
+ Environment.Exit(1);
}
}
}
\ No newline at end of file
--
Gitblit v1.9.3