Repository navigation
Expand file tree
/
Copy pathSet-CascadedWindow_BETA.ps1
More file actions
314 lines (247 loc) · 11.2 KB
/
Copy pathSet-CascadedWindow_BETA.ps1
File metadata and controls
314 lines (247 loc) · 11.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<#
.SYNOPSIS
Skript zur Wiederherstellung der "Fenster überlappend anzeigen"-Funktion unter Windows 11.
.DESCRIPTION
Bietet globales und prozessspezifisches Kaskadieren von Fenstern.
Option 1 gruppiert die Fenster automatisch nach Prozess (jeder Prozess bekommt
seinen eigenen Kaskaden-Cluster) und erlaubt bei mehreren Bildschirmen die Wahl
des Zielmonitors.
Die Monitor-Erkennung und Fenstergröße (85% der Auflösung des gewählten
Bildschirms) laufen komplett über die Win32-API (EnumDisplayMonitors /
GetMonitorInfo), um Abhängigkeiten von Windows.Forms zu vermeiden.
Unterstützt Multi-Window-Apps wie Outlook.
#>
# --- 1. Win32 API Definition ---
if (-not ([System.Management.Automation.PSTypeName]"Win32Functions.Win11Fix").Type) {
$methods = @'
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct MONITORINFO
{
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
}
public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, IntPtr lprcMonitor, IntPtr dwData);
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern ushort CascadeWindows(IntPtr hwndParent, uint wHow, IntPtr lpRect, uint cKids, IntPtr[] lpKids);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
'@
Add-Type -MemberDefinition $methods -Name "Win11Fix" -Namespace "Win32Functions"
}
$Win32 = [Win32Functions.Win11Fix]
$SW_RESTORE = 9
$SWP_SHOWWINDOW = 0x0040
$MONITORINFOF_PRIMARY = 0x00000001
# Prozesse, die von der globalen Kaskade (Option 1) NIEMALS angefasst werden sollen,
# z.B. weil sie auf Fenstergröße/-position empfindlich reagieren (Remote-Tools etc.).
# Bei Bedarf einfach ergänzen (Name ohne .exe, wie in Get-Process ausgegeben).
$ExcludedProcessNames = @(
"TeamViewer",
"TeamViewer_Service"
)
# Minimale Fenstergröße als Sicherheitsnetz - verhindert 0x0/zu kleine Fenster
# falls eine Monitor-Auflösung aus irgendeinem Grund nicht korrekt ermittelt werden konnte.
$MinWindowWidth = 400
$MinWindowHeight = 300
# --- 2. Monitor-Erkennung (reine Win32-API, kein Windows.Forms) ---
function Get-MonitorList {
$monitorList = New-Object System.Collections.Generic.List[PSObject]
$callback = {
param($hMonitor, $hdcMonitor, $lprcMonitor, $dwData)
$mi = New-Object -TypeName "Win32Functions.Win11Fix+MONITORINFO"
# WICHTIG: SizeOf braucht eine tatsächliche Struct-Instanz, nicht den Type selbst -
# sonst greift die falsche Overload und cbSize/GetMonitorInfo liefern Fehler bzw. 0-Werte.
$mi.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($mi)
$success = $Win32::GetMonitorInfo($hMonitor, [ref]$mi)
if ($success -and ($mi.rcMonitor.Right - $mi.rcMonitor.Left) -gt 0 -and ($mi.rcMonitor.Bottom - $mi.rcMonitor.Top) -gt 0) {
$monitorList.Add([PSCustomObject]@{
Handle = $hMonitor
Left = $mi.rcMonitor.Left
Top = $mi.rcMonitor.Top
Width = $mi.rcMonitor.Right - $mi.rcMonitor.Left
Height = $mi.rcMonitor.Bottom - $mi.rcMonitor.Top
IsPrimary = [bool]($mi.dwFlags -band $MONITORINFOF_PRIMARY)
}) | Out-Null
}
# Immer $true zurückgeben, damit die Enumeration weiterer Monitore nicht abbricht
return $true
}
$enumProc = [Win32Functions.Win11Fix+MonitorEnumProc]$callback
[void]$Win32::EnumDisplayMonitors([IntPtr]::Zero, [IntPtr]::Zero, $enumProc, [IntPtr]::Zero)
# Fallback, falls die Enumeration ausnahmsweise nichts liefert
if ($monitorList.Count -eq 0) {
$videoConfig = Get-CimInstance Win32_VideoController |
Select-Object CurrentHorizontalResolution, CurrentVerticalResolution |
Select-Object -First 1
$fallbackWidth = $videoConfig.CurrentHorizontalResolution
$fallbackHeight = $videoConfig.CurrentVerticalResolution
if (-not $fallbackWidth) { $fallbackWidth = 1920; $fallbackHeight = 1080 }
$monitorList.Add([PSCustomObject]@{
Handle = [IntPtr]::Zero
Left = 0
Top = 0
Width = $fallbackWidth
Height = $fallbackHeight
IsPrimary = $true
}) | Out-Null
}
for ($i = 0; $i -lt $monitorList.Count; $i++) {
$monitorList[$i] | Add-Member -NotePropertyName Index -NotePropertyValue ($i + 1)
}
return $monitorList
}
function Select-TargetMonitor {
$monitors = Get-MonitorList
if ($monitors.Count -le 1) {
return $monitors[0]
}
$selected = $monitors |
Select-Object Index, Width, Height, Left, Top, IsPrimary |
Out-GridView -Title "Zielbildschirm für die Fensteranordnung wählen" -OutputMode Single
if (-not $selected) {
return ($monitors | Where-Object { $_.IsPrimary } | Select-Object -First 1)
}
return ($monitors | Where-Object { $_.Index -eq $selected.Index })
}
# --- 3. Hilfsfunktionen ---
function Get-AllWindowHandles {
param([uint32]$TargetProcessId)
$handles = New-Object System.Collections.Generic.List[IntPtr]
$enumProc = [Win32Functions.Win11Fix+EnumWindowsProc] {
param($hWnd, $lParam)
$currentP_Id = 0
[void]$Win32::GetWindowThreadProcessId($hWnd, [ref]$currentP_Id)
if ($currentP_Id -eq $TargetProcessId -and $Win32::IsWindowVisible($hWnd)) {
$sb = New-Object System.Text.StringBuilder 256
[void]$Win32::GetWindowText($hWnd, $sb, $sb.Capacity)
if ($sb.ToString().Length -gt 0) {
$handles.Add($hWnd)
}
}
return $true
}
[void]$Win32::EnumWindows($enumProc, [IntPtr]::Zero)
return $handles
}
function Restore-And-Focus {
param([Parameter(Mandatory = $true)]$hWnd)
if ($Win32::IsIconic($hWnd)) {
[void]$Win32::ShowWindow($hWnd, $SW_RESTORE)
}
[void]$Win32::SetForegroundWindow($hWnd)
}
function Set-CascadeForHandles {
param(
[Parameter(Mandatory = $true)][System.Collections.Generic.List[IntPtr]]$Handles,
[Parameter(Mandatory = $true)][int]$StartX,
[Parameter(Mandatory = $true)][int]$StartY,
[Parameter(Mandatory = $true)][int]$MonLeft,
[Parameter(Mandatory = $true)][int]$MonTop,
[Parameter(Mandatory = $true)][int]$MonWidth,
[Parameter(Mandatory = $true)][int]$MonHeight,
[Parameter(Mandatory = $true)][int]$WinWidth,
[Parameter(Mandatory = $true)][int]$WinHeight,
[int]$Offset = 35
)
$x = $StartX
$y = $StartY
foreach ($h in $Handles) {
Restore-And-Focus -hWnd $h
[void]$Win32::SetWindowPos($h, [IntPtr]::Zero, $x, $y, $WinWidth, $WinHeight, $SWP_SHOWWINDOW)
$x += $Offset
$y += $Offset
if ($x -gt ($MonLeft + $MonWidth - 300)) { $x = $StartX }
if ($y -gt ($MonTop + $MonHeight - 300)) { $y = $StartY }
}
}
# --- 4. Hauptfunktionen ---
function Invoke-GlobalCascade {
$targetMonitor = Select-TargetMonitor
$monLeft = $targetMonitor.Left
$monTop = $targetMonitor.Top
$monWidth = $targetMonitor.Width
$monHeight = $targetMonitor.Height
$winWidth = [Math]::Max([int]($monWidth * 0.85), $MinWindowWidth)
$winHeight = [Math]::Max([int]($monHeight * 0.85), $MinWindowHeight)
$processes = Get-Process |
Where-Object { $_.MainWindowHandle -ne 0 -and $_.Id -ne $PID -and $ExcludedProcessNames -notcontains $_.ProcessName }
$processStepX = 60
$processStepY = 40
$maxOffsetX = [Math]::Max($monWidth - $winWidth - 40, 40)
$maxOffsetY = [Math]::Max($monHeight - $winHeight - 40, 40)
$processIndex = 0
foreach ($p in $processes) {
$handles = Get-AllWindowHandles -TargetProcessId $p.Id
if ($handles.Count -eq 0) { continue }
$startX = $monLeft + 40 + (($processIndex * $processStepX) % $maxOffsetX)
$startY = $monTop + 40 + (($processIndex * $processStepY) % $maxOffsetY)
Set-CascadeForHandles -Handles $handles -StartX $startX -StartY $startY `
-MonLeft $monLeft -MonTop $monTop -MonWidth $monWidth -MonHeight $monHeight `
-WinWidth $winWidth -WinHeight $winHeight
$processIndex++
}
}
function Invoke-ProcessSpecificCascade {
$selected = Get-Process |
Where-Object { $_.MainWindowTitle -ne "" } |
Select-Object ProcessName, Id, MainWindowTitle |
Out-GridView -Title "Prozess für Überlappung wählen" -OutputMode Single
if ($selected) {
$targetMonitor = Select-TargetMonitor
$monLeft = $targetMonitor.Left
$monTop = $targetMonitor.Top
$monWidth = $targetMonitor.Width
$monHeight = $targetMonitor.Height
$winWidth = [Math]::Max([int]($monWidth * 0.85), $MinWindowWidth)
$winHeight = [Math]::Max([int]($monHeight * 0.85), $MinWindowHeight)
$targetP_Ids = Get-Process -Name $selected.ProcessName | Select-Object -ExpandProperty Id
foreach ($p_id_entry in $targetP_Ids) {
$allHandles = Get-AllWindowHandles -TargetProcessId $p_id_entry
Set-CascadeForHandles -Handles $allHandles -StartX ($monLeft + 50) -StartY ($monTop + 50) `
-MonLeft $monLeft -MonTop $monTop -MonWidth $monWidth -MonHeight $monHeight `
-WinWidth $winWidth -WinHeight $winHeight
}
}
}
# --- 5. Menüführung ---
Clear-Host
Write-Host "--- Windows 11 Fenster-Manager (Final) ---" -ForegroundColor Cyan
Write-Host "1: Alle Fenster nach Prozess gruppiert überlappen (Global)"
Write-Host "2: Bestimmten Prozess wählen (z.B. Outlook)"
Write-Host "Q: Beenden"
$userInput = Read-Host "Eingabe"
switch ($userInput) {
"1" { Invoke-GlobalCascade }
"2" { Invoke-ProcessSpecificCascade }
"Q" { exit }
default { Write-Host "Ungültige Auswahl." -ForegroundColor Red }
}