Skip to content

Commit 895d022

Browse files
committed
Add window minimization functionality for better screenshots
1 parent 0ece925 commit 895d022

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

.github/workflows/screenshot_demo.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ jobs:
1010
uses: actions/checkout@v4
1111
- name: "Take Screenshot"
1212
run: |
13+
# Minimize the CMD window
14+
. "./scripts/utils/Minimize.ps1"
15+
Minimize-Window "Administrator: C:\actions"
1316
# Take a screenshot of the Office application
1417
. "./scripts/utils/Screenshot.ps1"
1518
Take-Screenshot -OutputPath "Screenshot.png"

Main.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ foreach ($folder in $folders) {
4848
}
4949
}
5050

51-
5251
# We need to open and close the Office applications before we can enable VBOM
5352
Write-Host "Open and close Office applications"
5453
. "$PSScriptRoot/scripts/Open-Close-Office.ps1" $officeApps
@@ -61,6 +60,13 @@ foreach ($app in $officeApps) {
6160
Write-Host "========================="
6261
}
6362

63+
# To get better screenshots we need to minimize the "Administrator" CMD window
64+
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
65+
. "$scriptPath/utils/Minimize.ps1"
66+
67+
Minimize-Window "Administrator: C:\actions"
68+
69+
6470
foreach ($folder in $folders) {
6571
$app = Get-OfficeApp -FileExtension $folder.Substring($folder.LastIndexOf('.') + 1)
6672

scripts/utils/Minimize.ps1

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This function should minimize the window using the Win32 API
2+
# It receives a string with the first part of the window title
3+
# and minimizes the window if it finds a match among the open windows
4+
function Minimize-Window {
5+
param (
6+
[string]$windowTitlePart
7+
)
8+
9+
Write-Host "Starting window minimization process..." -ForegroundColor Cyan
10+
Write-Host "Looking for windows with title containing: '$windowTitlePart'" -ForegroundColor Cyan
11+
12+
# Get the handle of the current process
13+
$currentProcess = Get-Process -Id $PID
14+
$currentProcessHandle = $currentProcess.MainWindowHandle
15+
16+
# Get all open windows
17+
$windows = Get-Process | Where-Object { $_.MainWindowHandle -ne 0 }
18+
19+
Write-Host "Found $($windows.Count) windows with valid handles" -ForegroundColor Gray
20+
21+
$matchFound = $false
22+
23+
foreach ($window in $windows) {
24+
Write-Verbose "Checking window: $($window.MainWindowTitle) (Handle: $($window.MainWindowHandle))"
25+
26+
if ($window.MainWindowTitle -like "*$windowTitlePart*") {
27+
Write-Host "Match found! Window title: $($window.MainWindowTitle)" -ForegroundColor Green
28+
29+
# Minimize the window
30+
try {
31+
[void][System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(
32+
[System.Runtime.InteropServices.Marshal]::GetProcAddress(
33+
[System.Runtime.InteropServices.Marshal]::GetHINSTANCE([System.Reflection.Assembly]::GetExecutingAssembly().GetModules()[0]),
34+
"ShowWindow"
35+
),
36+
[System.IntPtr]
37+
)($window.MainWindowHandle, 2) # 2 = SW_MINIMIZE
38+
Write-Host "Successfully minimized window: $($window.MainWindowTitle)" -ForegroundColor Green
39+
$matchFound = $true
40+
}
41+
catch {
42+
Write-Host "Error minimizing window: $($_.Exception.Message)" -ForegroundColor Red
43+
}
44+
}
45+
}
46+
47+
# If no match was found, return a list of all window handles
48+
if (-not $matchFound) {
49+
Write-Host "No windows found matching the pattern: '$windowTitlePart'" -ForegroundColor Yellow
50+
Write-Host "Here's a list of all available windows:" -ForegroundColor Yellow
51+
52+
$windowList = $windows | ForEach-Object {
53+
[PSCustomObject]@{
54+
Title = $_.MainWindowTitle
55+
Handle = $_.MainWindowHandle
56+
ProcessName = $_.ProcessName
57+
Id = $_.Id
58+
}
59+
}
60+
61+
$windowList | Format-Table -AutoSize
62+
return $windowList
63+
}
64+
}

0 commit comments

Comments
 (0)