回答編集履歴
1
追記
answer
CHANGED
@@ -31,4 +31,57 @@
|
|
31
31
|
$window
|
32
32
|
$window = [Platform]::GetWindow($window, [GetWindowType]::Next)
|
33
33
|
}
|
34
|
+
```
|
35
|
+
|
36
|
+
#追記
|
37
|
+
|
38
|
+
上記スクリプトを少し変更し、一番手前の Excel のウィンドウが最前面に出ることを確認しました。
|
39
|
+
|
40
|
+
```ps1
|
41
|
+
Add-Type -TypeDefinition @"
|
42
|
+
using System;
|
43
|
+
using System.Runtime.InteropServices;
|
44
|
+
|
45
|
+
public class Platform
|
46
|
+
{
|
47
|
+
[DllImport("user32.dll", SetLastError = true)]
|
48
|
+
public static extern IntPtr GetWindow(IntPtr hwnd, GetWindowType uCmd);
|
49
|
+
|
50
|
+
[DllImport("user32.dll", SetLastError = true)]
|
51
|
+
public static extern IntPtr GetDesktopWindow();
|
52
|
+
|
53
|
+
[DllImport("kernel32.dll")]
|
54
|
+
public static extern uint GetLastError();
|
55
|
+
|
56
|
+
[DllImport("user32.dll")]
|
57
|
+
[return: MarshalAs(UnmanagedType.Bool)]
|
58
|
+
public static extern bool SetForegroundWindow(IntPtr hwnd);
|
59
|
+
}
|
60
|
+
|
61
|
+
public enum GetWindowType : uint
|
62
|
+
{
|
63
|
+
First = 0,
|
64
|
+
Last = 1,
|
65
|
+
Next = 2,
|
66
|
+
Prev = 3,
|
67
|
+
Owner = 4,
|
68
|
+
Child = 5,
|
69
|
+
EnabledPopup = 6
|
70
|
+
}
|
71
|
+
"@
|
72
|
+
function EnumerateWindows
|
73
|
+
{
|
74
|
+
$window = [Platform]::GetWindow([Platform]::GetDesktopWindow(), [GetWindowType]::Child)
|
75
|
+
while ($window -ne [IntPtr]::Zero)
|
76
|
+
{
|
77
|
+
$window
|
78
|
+
$window = [Platform]::GetWindow($window, [GetWindowType]::Next)
|
79
|
+
}
|
80
|
+
}
|
81
|
+
$handles = @(Get-Process | Where-Object { $_.Name -eq 'Excel' } | Foreach-Object { $_.MainWindowHandle })
|
82
|
+
if ($handles -eq $null) {
|
83
|
+
return
|
84
|
+
}
|
85
|
+
$handle = EnumerateWindows | Where-Object { $handles.Contains($_) } | Select-Object -First 1
|
86
|
+
[Platform]::SetForegroundWindow($handle)
|
34
87
|
```
|