From a5994c85a43e4121bb75d6dde818580556658287 Mon Sep 17 00:00:00 2001 From: maqiao <625215135@qq.com> Date: Thu, 14 Nov 2024 14:27:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8D=E5=8F=AF=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=90=AF=E5=8A=A8=E8=BD=AF=E4=BB=B6=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DM_Weight/App.xaml | 2 +- DM_Weight/App.xaml.cs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/DM_Weight/App.xaml b/DM_Weight/App.xaml index ef8d445..704fc50 100644 --- a/DM_Weight/App.xaml +++ b/DM_Weight/App.xaml @@ -6,7 +6,7 @@ d1p1:Ignorable="d" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:local="clr-namespace:DM_Weight" - xmlns:prism="http://prismlibrary.com/"> + xmlns:prism="http://prismlibrary.com/" Startup="PrismApplication_Startup"> diff --git a/DM_Weight/App.xaml.cs b/DM_Weight/App.xaml.cs index 198f1c4..3c8d840 100644 --- a/DM_Weight/App.xaml.cs +++ b/DM_Weight/App.xaml.cs @@ -24,6 +24,8 @@ using System.Windows.Interop; using System.Windows.Threading; using System.Timers; using DM_Weight.HIKVISION; +using System.Diagnostics; +using System.Runtime.InteropServices; namespace DM_Weight { @@ -277,5 +279,42 @@ namespace DM_Weight logger.Info("结束APP-RegisterTypes"); } + + private void PrismApplication_Startup(object sender, StartupEventArgs e) + { + //获取欲启动程序名 + string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; + //检查程序是否已经启动,已经启动则显示提示退出程序 + if (System.Diagnostics.Process.GetProcessesByName(processName).Length > 1) + { + //系统在运行 + RaiseOtherProcess(); + Application.Current.Shutdown(); + return; + } + } + private static void RaiseOtherProcess() + { + Process proc = Process.GetCurrentProcess(); + foreach (Process otherProc in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)) + { + if (proc.Id != otherProc.Id) + { + IntPtr hWnd = otherProc.MainWindowHandle; + if (IsIconic(hWnd)) + { + ShowWindowAsync(hWnd, 9); + } + SetForegroundWindow(hWnd); + break; + } + } + } + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); + [DllImport("user32.dll")] + private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); + [DllImport("user32.dll")] + private static extern bool IsIconic(IntPtr hWnd); } }