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); } }