BringWindowToFront.cs
2.2 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
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Runtime.InteropServices;
namespace Unity.PlasticSCM.Editor.Tool
{
internal static class BringWindowToFront
{
internal static void ForWindowsProcess(int processId)
{
IntPtr handle = FindMainWindowForProcess(processId);
if (IsIconic(handle))
ShowWindow(handle, SW_RESTORE);
SetForegroundWindow(handle);
}
static IntPtr FindMainWindowForProcess(int processId)
{
IntPtr result = IntPtr.Zero;
EnumWindows(delegate (IntPtr wnd, IntPtr param)
{
uint windowProcessId = 0;
GetWindowThreadProcessId(wnd, out windowProcessId);
if (windowProcessId == processId &&
IsMainWindow(wnd))
{
result = wnd;
return false;
}
return true;
}, IntPtr.Zero);
return result;
}
static bool IsMainWindow(IntPtr handle)
{
return GetWindow(new HandleRef(null, handle), GW_OWNER) == IntPtr.Zero
&& IsWindowVisible(new HandleRef(null, handle));
}
// Delegate to filter which windows to include
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool IsWindowVisible(HandleRef hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr handle);
const int GW_OWNER = 4;
const int SW_RESTORE = 9;
}
}