-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
58 lines (52 loc) · 1.82 KB
/
App.xaml.cs
File metadata and controls
58 lines (52 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
namespace LitePeom
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
[DllImport("user32")]
private static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
protected override void OnStartup(StartupEventArgs e)
{
if (IsRunning())
{
Current.Shutdown();
}
//
// base.OnStartup(e);
}
/// <summary>判断单实例程序是否正在运行</summary>
/// <remarks>若正在运行激活窗口</remarks>
/// copy from https://github.com/BluePointLilac/ContextMenuManager
public static bool IsRunning()
{
using (Process current = Process.GetCurrentProcess())
{
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
using (process)
{
if (process.Id == current.Id) continue;
if (process.MainModule.FileName == current.MainModule.FileName)
{
const int SW_RESTORE = 9;
ShowWindowAsync(process.MainWindowHandle, SW_RESTORE);
SetForegroundWindow(process.MainWindowHandle);
return true;
}
}
}
return false;
}
}
}
}