-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyScreenAligner.ps1
More file actions
334 lines (303 loc) · 12.8 KB
/
Copy pathEasyScreenAligner.ps1
File metadata and controls
334 lines (303 loc) · 12.8 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Easy Screen Aligner v0.1.0 - MIT License
# See the LICENSE file in this repository for the complete license text.
# --- Part 1: Setup calibration overlay on each monitor (transparent background) ---
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Show calibration instructions popup at launch.
$popupMessage = "Calibration Instructions:`n`n- Use the red line to align your monitors.`n- Drag the red line along the highlighted axis.`n- Click any green Apply button to confirm all monitors at once.`n- Press ESC at any time to cancel calibration."
[System.Windows.Forms.MessageBox]::Show($popupMessage, "Calibration Instructions", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
# Global arrays for results and forms, plus a flag for cancellation.
$global:MonitorResults = @()
$global:CalibForms = @()
$global:CalibrationCanceled = $false
# Determine monitor arrangement to decide whether we align vertically or horizontally.
$screens = [System.Windows.Forms.Screen]::AllScreens
if ($screens.Count -eq 0) {
Write-Host "No monitors detected. Exiting." -ForegroundColor Red
exit
}
$minLeft = ($screens | ForEach-Object { $_.Bounds.X } | Measure-Object -Minimum).Minimum
$maxRight = ($screens | ForEach-Object { $_.Bounds.X + $_.Bounds.Width } | Measure-Object -Maximum).Maximum
$minTop = ($screens | ForEach-Object { $_.Bounds.Y } | Measure-Object -Minimum).Minimum
$maxBottom = ($screens | ForEach-Object { $_.Bounds.Y + $_.Bounds.Height } | Measure-Object -Maximum).Maximum
$widthSpan = $maxRight - $minLeft
$heightSpan = $maxBottom - $minTop
$global:CalibrationAxis = if (($screens.Count -eq 1) -or ($widthSpan -ge $heightSpan)) { 'Y' } else { 'X' }
if ($global:CalibrationAxis -eq 'Y') {
Write-Host "Detected side-by-side monitors; using horizontal calibration lines."
} else {
Write-Host "Detected vertically stacked monitors; using vertical calibration lines."
}
$lineThickness = 10
$hitTolerance = 12
# For each screen, open a borderless form that covers it.
foreach ($screen in $screens) {
$form = New-Object System.Windows.Forms.Form
$form.FormBorderStyle = 'None'
$form.TopMost = $true
$form.StartPosition = 'Manual'
$form.Location = $screen.Bounds.Location
$form.Size = $screen.Bounds.Size
# Use Magenta as the transparent color so the desktop shows through.
$form.BackColor = [System.Drawing.Color]::Magenta
$form.TransparencyKey = [System.Drawing.Color]::Magenta
$form.KeyPreview = $true
$form.Add_KeyDown({
param($sender, $e)
if ($e.KeyCode -eq [System.Windows.Forms.Keys]::Escape) {
$global:CalibrationCanceled = $true
foreach ($f in $global:CalibForms) {
if ($f.Visible) { $f.Close() }
}
Write-Host "Calibration canceled via ESC." -ForegroundColor Yellow
}
})
# Start with the red line at mid-screen.
$initialLine = if ($global:CalibrationAxis -eq 'Y') {
[math]::Round($form.Height / 2)
} else {
[math]::Round($form.Width / 2)
}
$form.Tag = [ordered]@{
Screen = $screen
LineValue = $initialLine
Dragging = $false
Offset = 0
}
# Paint event: Draw a semi-transparent, thicker red line, horizontal or vertical.
$form.Add_Paint({
param($sender, $e)
$pen = New-Object System.Drawing.Pen([System.Drawing.Color]::FromArgb(220,255,0,0), $lineThickness)
$lineValue = $sender.Tag.LineValue
if ($global:CalibrationAxis -eq 'Y') {
$e.Graphics.DrawLine($pen, 0, $lineValue, $sender.Width, $lineValue)
} else {
$e.Graphics.DrawLine($pen, $lineValue, 0, $lineValue, $sender.Height)
}
})
# Allow dragging the line along the detected axis.
$form.Add_MouseDown({
param($sender, $e)
if ($global:CalibrationAxis -eq 'Y') {
if ([math]::Abs($e.Y - $sender.Tag.LineValue) -le $hitTolerance) {
$sender.Tag.Dragging = $true
$sender.Tag.Offset = $e.Y - $sender.Tag.LineValue
}
} else {
if ([math]::Abs($e.X - $sender.Tag.LineValue) -le $hitTolerance) {
$sender.Tag.Dragging = $true
$sender.Tag.Offset = $e.X - $sender.Tag.LineValue
}
}
})
$form.Add_MouseMove({
param($sender, $e)
if ($sender.Tag.Dragging) {
if ($global:CalibrationAxis -eq 'Y') {
$newLine = $e.Y - $sender.Tag.Offset
$sender.Tag.LineValue = [math]::Max(0, [math]::Min($sender.Height, $newLine))
} else {
$newLine = $e.X - $sender.Tag.Offset
$sender.Tag.LineValue = [math]::Max(0, [math]::Min($sender.Width, $newLine))
}
$sender.Invalidate()
}
})
$form.Add_MouseUp({
param($sender, $e)
$sender.Tag.Dragging = $false
})
# Prominent "Apply All" button that finalizes all monitors simultaneously.
$button = New-Object System.Windows.Forms.Button
$button.Text = "Apply All"
$button.Width = 160
$button.Height = 60
$button.Font = New-Object System.Drawing.Font("Segoe UI", 14, [System.Drawing.FontStyle]::Bold)
$button.Location = New-Object System.Drawing.Point(10,10)
$button.BackColor = [System.Drawing.Color]::FromArgb(255, 46, 204, 113)
$button.ForeColor = [System.Drawing.Color]::White
$button.FlatStyle = 'Popup'
$button.Add_Click({
param($sender, $e)
if ($global:MonitorResults.Count -gt 0) { return }
$global:MonitorResults = @()
foreach ($frm in $global:CalibForms) {
if ($null -ne $frm -and -not $frm.IsDisposed) {
$global:MonitorResults += [PSCustomObject]@{
Screen = $frm.Tag.Screen
LineValue = $frm.Tag.LineValue
}
if ($frm.Visible) {
$frm.Close()
}
}
}
})
$form.Controls.Add($button)
$global:CalibForms += $form
$form.Show()
}
# Wait for all calibration windows to close.
while (($global:CalibForms | Where-Object { $_.Visible }).Count -gt 0) {
Start-Sleep -Milliseconds 100
[System.Windows.Forms.Application]::DoEvents() | Out-Null
}
if ($global:CalibrationCanceled) {
Write-Host "Calibration canceled. Exiting." -ForegroundColor Yellow
exit
}
if ($global:MonitorResults.Count -eq 0) {
Write-Host "No calibration data was captured. Exiting." -ForegroundColor Yellow
exit
}
# --- Part 2: Compute new positions along the detected calibration axis ---
# Use the primary monitor's red line absolute value as the desired alignment.
$primaryResult = $global:MonitorResults | Where-Object { $_.Screen.Primary } | Select-Object -First 1
$useYAxis = $global:CalibrationAxis -eq 'Y'
if ($primaryResult) {
$desiredAbsolute = if ($useYAxis) {
$primaryResult.Screen.Bounds.Y + $primaryResult.LineValue
} else {
$primaryResult.Screen.Bounds.X + $primaryResult.LineValue
}
Write-Host "Primary monitor detected; using its red line position ($desiredAbsolute) as reference."
} else {
# Fallback: use the median of all absolute positions.
$absPositions = $global:MonitorResults | ForEach-Object {
if ($useYAxis) {
$_.Screen.Bounds.Y + $_.LineValue
} else {
$_.Screen.Bounds.X + $_.LineValue
}
}
$sorted = $absPositions | Sort-Object
$medianIndex = [math]::Floor($sorted.Count / 2)
$desiredAbsolute = $sorted[$medianIndex]
Write-Host "No primary monitor flagged; using median red line position ($desiredAbsolute) as reference."
}
# Calculate adjustments: primary monitor remains unchanged; for others, adjust so absolute line matches desired.
$global:DisplayAdjustments = @()
foreach ($result in $global:MonitorResults) {
if ($useYAxis) {
$currentBase = $result.Screen.Bounds.Y
} else {
$currentBase = $result.Screen.Bounds.X
}
$localLine = $result.LineValue
if ($result.Screen.Primary) {
$newBase = $currentBase # Keep primary monitor unchanged.
} else {
$newBase = $desiredAbsolute - $localLine
}
$delta = $newBase - $currentBase
$global:DisplayAdjustments += [PSCustomObject]@{
DeviceName = $result.Screen.DeviceName
OldCoordinate = $currentBase
NewCoordinate = $newBase
Delta = $delta
Screen = $result.Screen
Axis = $global:CalibrationAxis
}
if ($useYAxis) {
Write-Host "For monitor $($result.Screen.DeviceName): Old top=$currentBase, local line=$localLine, new top=$newBase (delta=$delta)"
} else {
Write-Host "For monitor $($result.Screen.DeviceName): Old left=$currentBase, local line=$localLine, new left=$newBase (delta=$delta)"
}
}
# --- Part 3: Update display configuration via Windows API (axis-aware offsets) ---
# Compile a helper C# class. We update only DM_POSITION, leaving orientation fields untouched.
$code = @"
using System;
using System.Runtime.InteropServices;
public class DisplayConfig {
private const int ENUM_CURRENT_SETTINGS = -1;
private const int DM_POSITION = 0x00000020;
private const int CDS_UPDATEREGISTRY = 0x00000001;
private const int CDS_NORESET = 0x10000000;
private const int DISP_CHANGE_SUCCESSFUL = 0;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct POINTL {
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DEVMODE {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public POINTL dmPosition;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, IntPtr lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
public static bool SetDisplayPosition(string deviceName, int newX, int newY) {
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
if (!EnumDisplaySettings(deviceName, ENUM_CURRENT_SETTINGS, ref dm))
return false;
dm.dmFields |= DM_POSITION;
dm.dmPosition.x = newX;
dm.dmPosition.y = newY;
int result = ChangeDisplaySettingsEx(deviceName, ref dm, IntPtr.Zero, CDS_UPDATEREGISTRY | CDS_NORESET, IntPtr.Zero);
return result == DISP_CHANGE_SUCCESSFUL;
}
public static bool ApplyDisplayChanges() {
int result = ChangeDisplaySettingsEx(null, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero);
return result == DISP_CHANGE_SUCCESSFUL;
}
}
"@
# Compile the helper.
Add-Type -TypeDefinition $code -PassThru | Out-Null
# For each monitor adjustment, update the relevant axis while preserving the untouched coordinate.
foreach ($adj in $global:DisplayAdjustments) {
$device = $adj.DeviceName
if ($adj.Axis -eq 'Y') {
$newX = $adj.Screen.Bounds.X
$newY = $adj.NewCoordinate
} else {
$newX = $adj.NewCoordinate
$newY = $adj.Screen.Bounds.Y
}
Write-Host "Adjusting $device : setting position to ($newX, $newY)..."
$ok = [DisplayConfig]::SetDisplayPosition($device, $newX, $newY)
if (-not $ok) {
Write-Host "Failed to update position for $device" -ForegroundColor Red
}
}
if ([DisplayConfig]::ApplyDisplayChanges()) {
Write-Host "Display configuration updated successfully."
} else {
Write-Host "Failed to apply display changes." -ForegroundColor Red
}
exit