-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
61 lines (48 loc) · 1.76 KB
/
App.xaml.cs
File metadata and controls
61 lines (48 loc) · 1.76 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
59
60
61
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using Application = System.Windows.Application;
namespace DevTools
{
public partial class App : Application
{
private static readonly string MutexName = "DevTools_SingleInstance_Mutex";
private static System.Threading.Mutex? _mutex;
private const int HWND_BROADCAST = 0xffff;
private static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME_DEVTOOLS");
[DllImport("user32.dll")]
private static extern int RegisterWindowMessage(string message);
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_RESTORE = 9;
public static int ShowMeMessage => WM_SHOWME;
protected override void OnStartup(StartupEventArgs e)
{
bool createdNew;
_mutex = new System.Threading.Mutex(true, MutexName, out createdNew);
if (!createdNew)
{
ActivateExistingInstance();
Shutdown();
return;
}
base.OnStartup(e);
}
private void ActivateExistingInstance()
{
PostMessage((IntPtr)HWND_BROADCAST, WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
}
protected override void OnExit(ExitEventArgs e)
{
_mutex?.ReleaseMutex();
_mutex?.Dispose();
base.OnExit(e);
}
}
}