-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart-ScreenRecorder.ps1
More file actions
979 lines (926 loc) · 45.6 KB
/
Start-ScreenRecorder.ps1
File metadata and controls
979 lines (926 loc) · 45.6 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<#PSScriptInfo
.VERSION 1.1.0
.GUID d47eab76-de84-454d-aead-8b61ed3335eb
.AUTHOR Yoshifumi Tsuda
.COPYRIGHT Copyright (c) 2025-2026 Yoshifumi Tsuda. MIT License.
.TAGS Screen Capture Recording Debug Screenshot Clock
.LICENSEURI https://github.com/yotsuda/ScreenRecorder/blob/master/LICENSE
.PROJECTURI https://github.com/yotsuda/ScreenRecorder
.DESCRIPTION Screen capture tool with clock overlay for debugging and log correlation.
#>
param(
[Parameter(DontShow)]
[switch]$Background,
[ValidateRange(1, 60)]
[int]$FPS = 2,
[ValidateRange(0.1, 1.0)]
[double]$Scale = 0.75,
[ValidateRange(1, 100)]
[int]$Quality = 75,
[switch]$SaveMasked,
[Parameter(DontShow)]
[string]$ReadyFile,
[ArgumentCompleter({ '00:05:00' })]
[TimeSpan]$RecordFor,
[string]$OutputPath
)
function Start-ScreenRecorder {
<#
.SYNOPSIS
Starts a screen recorder with a clock overlay for debugging and log correlation.
.DESCRIPTION
Captures screenshots at regular intervals while displaying a large clock overlay.
Designed for correlating screen captures with log timestamps during bug reproduction.
Requires no external dependencies - uses only PowerShell and .NET.
Can be run directly without module installation.
.PARAMETER Background
Runs the recorder in a hidden background process.
.PARAMETER FPS
Frames per second for capture. Default is 2.
.PARAMETER Scale
Scale factor for captured images (0.1 to 1.0). Default is 0.75.
.PARAMETER Quality
JPEG quality for captured images (1-100). Default is 75.
.PARAMETER SaveMasked
Saves masked images (with clock area blacked out) for debugging.
.PARAMETER RecordFor
Starts recording immediately and stops after the specified duration.
.PARAMETER OutputPath
Base directory for saving screenshots. A timestamped subfolder will be created.
If not specified, uses current directory or TEMP folder as fallback.
.EXAMPLE
Start-ScreenRecorder
Starts the recorder in background mode.
.EXAMPLE
Start-ScreenRecorder -FPS 10 -Scale 0.75
Starts with higher frame rate and larger output images.
.EXAMPLE
Start-ScreenRecorder -RecordFor 0:10:00
Starts recording immediately and stops after 10 minutes.
.EXAMPLE
Start-ScreenRecorder -OutputPath D:\Screenshots
Saves screenshots to D:\Screenshots\yyyyMMdd_HHmmss\ folder.
#>
[CmdletBinding()]
param(
[Parameter(DontShow)]
[switch]$Background,
[ValidateRange(1, 60)]
[int]$FPS = 2,
[ValidateRange(0.1, 1.0)]
[double]$Scale = 0.75,
[ValidateRange(1, 100)]
[int]$Quality = 75,
[switch]$SaveMasked,
[Parameter(DontShow)]
[string]$ReadyFile,
[ArgumentCompleter({ '00:05:00' })]
[TimeSpan]$RecordFor,
[string]$OutputPath
)
if (-not $Background) {
Write-Host 'Starting ScreenRecorder... ' -NoNewline
$readyFile = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "sr_ready_$PID.tmp")
$exe = (Get-Process -Id $PID).Path
$scriptPath = $MyInvocation.MyCommand.ScriptBlock.File
if (-not $scriptPath) { $scriptPath = $PSCommandPath }
$procArgs = "-NoProfile -WindowStyle Hidden -File `"$scriptPath`" -Background -FPS $FPS -Scale $Scale -Quality $Quality -ReadyFile `"$readyFile`""
if ($SaveMasked) { $procArgs += " -SaveMasked" }
if ($RecordFor -gt [TimeSpan]::Zero) { $procArgs += " -RecordFor $($RecordFor.ToString())" }
if ($OutputPath) { $procArgs += " -OutputPath `"$OutputPath`"" }
Start-Process $exe -ArgumentList $procArgs -WindowStyle Hidden
# Wait for background to be ready with spinner
$spinner = '|', '/', '-', '\'
$i = 0
$timeout = [DateTime]::Now.AddSeconds(30)
[Console]::CursorVisible = $false
while (-not (Test-Path $readyFile) -and [DateTime]::Now -lt $timeout) {
Write-Host "`b$($spinner[$i++ % 4])" -NoNewline
Start-Sleep -Milliseconds 100
}
[Console]::CursorVisible = $true
Remove-Item $readyFile -ErrorAction SilentlyContinue
Write-Host "`b Ready!"
return
}
Add-Type -AssemblyName PresentationFramework,System.Windows.Forms,System.Drawing
$drawingAsm = [System.Drawing.Bitmap].Assembly.Location
$primAsm = [System.Drawing.Rectangle].Assembly.Location
$winCoreAsm = [System.Drawing.Bitmap].Assembly.GetReferencedAssemblies() |
Where-Object { $_.Name -eq 'System.Private.Windows.Core' } |
ForEach-Object { [System.Reflection.Assembly]::Load($_).Location }
Add-Type -TypeDefinition @"
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
public class DisplayHelper {
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DEVMODE {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion, dmDriverVersion, dmSize, dmDriverExtra;
public int dmFields, dmPositionX, dmPositionY, dmDisplayOrientation, dmDisplayFixedOutput;
public short dmColor, dmDuplex, dmYResolution, dmTTOption, dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel, dmPelsWidth, dmPelsHeight, dmDisplayFlags, dmDisplayFrequency;
}
public const int ENUM_CURRENT_SETTINGS = -1;
[StructLayout(LayoutKind.Sequential)]
public struct RECT { public int Left, Top, Right, Bottom; }
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hWnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);
public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
// Get physical window rect using DWM (always returns physical pixels)
public static RECT GetPhysicalWindowRect(IntPtr hWnd) {
RECT rect;
DwmGetWindowAttribute(hWnd, DWMWA_EXTENDED_FRAME_BOUNDS, out rect, Marshal.SizeOf(typeof(RECT)));
return rect;
}
// Fast FNV-1a hash for bitmap comparison (4x4 sampling for performance)
public static long ComputeImageHash(Bitmap bmp, int exL, int exT, int exR, int exB) {
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
try {
long hash = unchecked((long)0xcbf29ce484222325);
int stride = data.Stride;
int width = bmp.Width;
int height = bmp.Height;
IntPtr scan0 = data.Scan0;
for (int y = 0; y < height; y += 4) {
IntPtr row = IntPtr.Add(scan0, y * stride);
if (y >= exT && y < exB) {
for (int x = 0; x < exL; x += 4) { hash ^= Marshal.ReadInt32(row, x * 4); hash *= 0x100000001b3L; }
for (int x = exR; x < width; x += 4) { hash ^= Marshal.ReadInt32(row, x * 4); hash *= 0x100000001b3L; }
} else {
for (int x = 0; x < width; x += 4) { hash ^= Marshal.ReadInt32(row, x * 4); hash *= 0x100000001b3L; }
}
}
return hash;
} finally {
bmp.UnlockBits(data);
}
}
}
public class BackgroundRecorder {
private Task _task;
private CancellationTokenSource _cts;
private Action<string> _errorCallback;
private int _intervalMs;
private string _outDir;
private bool _saveMasked;
private double _scale;
private IntPtr _windowHandle;
private ImageCodecInfo _jpegCodec;
private EncoderParameters _encoderParams;
private Bitmap _captureBmp, _thumbBmp;
private Graphics _captureG, _thumbG;
private Rectangle _bounds;
private int _thumbW, _thumbH;
private long _prevHash;
private int _saved;
private string _lastError;
private int _quality;
private volatile bool _showOverlay;
// Multi-monitor support
private Rectangle[] _monitorBounds;
private Bitmap[] _monitorBmps;
private Graphics[] _monitorGs;
public int Saved { get { return _saved; } }
public string LastError { get { return _lastError; } }
public void MarkSettingsChanged(int quality) {
_quality = quality;
_encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
_showOverlay = true;
}
public bool Start(Rectangle bounds, Rectangle[] monitorBounds, int thumbW, int thumbH, int fps, int quality, string outDir, bool saveMasked, double scale, IntPtr windowHandle, Action<string> errorCallback) {
// Validate parameters
if (bounds.Width <= 0 || bounds.Height <= 0 || thumbW <= 0 || thumbH <= 0) {
_lastError = "Invalid dimensions: bounds=" + bounds.Width + "x" + bounds.Height + ", thumb=" + thumbW + "x" + thumbH;
return false;
}
// Cleanup any previous resources
Stop();
_bounds = bounds;
_monitorBounds = monitorBounds;
_thumbW = thumbW;
_thumbH = thumbH;
_intervalMs = 1000 / fps;
_outDir = outDir;
_saveMasked = saveMasked;
_scale = scale;
_windowHandle = windowHandle;
_prevHash = 0;
_saved = 0;
_quality = quality;
_errorCallback = errorCallback;
try {
_captureBmp = new Bitmap(bounds.Width, bounds.Height);
try {
_captureG = Graphics.FromImage(_captureBmp);
_thumbBmp = new Bitmap(thumbW, thumbH);
try {
_thumbG = Graphics.FromImage(_thumbBmp);
// Allocate per-monitor bitmaps for multi-monitor capture
if (_monitorBounds != null && _monitorBounds.Length > 1) {
_monitorBmps = new Bitmap[_monitorBounds.Length];
_monitorGs = new Graphics[_monitorBounds.Length];
try {
for (int i = 0; i < _monitorBounds.Length; i++) {
_monitorBmps[i] = new Bitmap(_monitorBounds[i].Width, _monitorBounds[i].Height);
_monitorGs[i] = Graphics.FromImage(_monitorBmps[i]);
}
} catch {
// Clean up partial allocations
for (int i = 0; i < _monitorBmps.Length; i++) {
if (_monitorGs != null && i < _monitorGs.Length && _monitorGs[i] != null) {
_monitorGs[i].Dispose();
_monitorGs[i] = null;
}
if (_monitorBmps[i] != null) {
_monitorBmps[i].Dispose();
_monitorBmps[i] = null;
}
}
throw;
}
}
} catch {
if (_thumbBmp != null) { _thumbBmp.Dispose(); _thumbBmp = null; }
throw;
}
} catch {
if (_captureG != null) { _captureG.Dispose(); _captureG = null; }
if (_captureBmp != null) { _captureBmp.Dispose(); _captureBmp = null; }
throw;
}
} catch (Exception ex) {
_lastError = "Bitmap init failed: " + ex.Message;
return false;
}
_jpegCodec = null;
foreach (var codec in ImageCodecInfo.GetImageEncoders()) {
if (codec.MimeType == "image/jpeg") { _jpegCodec = codec; break; }
}
_encoderParams = new EncoderParameters(1);
_encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
_cts = new CancellationTokenSource();
_task = Task.Run(() => RecordLoop(_cts.Token));
return true;
}
public void Stop() {
// Signal cancellation and wait for task to complete
if (_cts != null) {
_cts.Cancel();
if (_task != null) {
try {
if (!_task.Wait(5000)) {
// Task didn't complete in 5 seconds - force cleanup
_lastError = "Task timeout during stop - forced cleanup";
}
} catch (AggregateException) {
// Task was cancelled - this is expected
}
_task = null;
}
_cts.Dispose();
_cts = null;
}
// Dispose per-monitor resources
if (_monitorGs != null) {
for (int i = 0; i < _monitorGs.Length; i++) {
if (_monitorGs[i] != null) { _monitorGs[i].Dispose(); _monitorGs[i] = null; }
}
_monitorGs = null;
}
if (_monitorBmps != null) {
for (int i = 0; i < _monitorBmps.Length; i++) {
if (_monitorBmps[i] != null) { _monitorBmps[i].Dispose(); _monitorBmps[i] = null; }
}
_monitorBmps = null;
}
if (_thumbG != null) { _thumbG.Dispose(); _thumbG = null; }
if (_thumbBmp != null) { _thumbBmp.Dispose(); _thumbBmp = null; }
if (_captureG != null) { _captureG.Dispose(); _captureG = null; }
if (_captureBmp != null) { _captureBmp.Dispose(); _captureBmp = null; }
if (_encoderParams != null) { _encoderParams.Dispose(); _encoderParams = null; }
}
private void RecordLoop(CancellationToken ct) {
DisplayHelper.RECT _prevRect = new DisplayHelper.RECT();
bool firstFrame = true;
while (!ct.IsCancellationRequested) {
var sw = Stopwatch.StartNew();
try {
// Get exclude rect BEFORE capture (more accurate timing)
var rect = DisplayHelper.GetPhysicalWindowRect(_windowHandle);
int exL = (int)((rect.Left - _bounds.Left) * _scale) / 4 * 4;
int exT = (int)((rect.Top - _bounds.Top) * _scale) / 4 * 4;
int exR = ((int)((rect.Right - _bounds.Left) * _scale) + 3) / 4 * 4;
int exB = ((int)((rect.Bottom - _bounds.Top) * _scale) + 3) / 4 * 4;
// Skip if window is moving (rect changed since last frame)
bool isMoving = !firstFrame && (rect.Left != _prevRect.Left || rect.Top != _prevRect.Top ||
rect.Right != _prevRect.Right || rect.Bottom != _prevRect.Bottom);
_prevRect = rect;
firstFrame = false;
if (isMoving) {
// Skip this frame - window is moving
var skipElapsed = (int)sw.ElapsedMilliseconds;
int skipSleep = _intervalMs - skipElapsed;
if (skipSleep > 0) {
try { Task.Delay(skipSleep, ct).Wait(); }
catch (AggregateException) { return; }
}
continue;
}
// Capture screen
if (_monitorBounds == null || _monitorBounds.Length == 1) {
_captureG.CopyFromScreen(_bounds.Location, Point.Empty, _bounds.Size);
} else {
_captureG.Clear(Color.Black);
for (int i = 0; i < _monitorBounds.Length; i++) {
var b = _monitorBounds[i];
_monitorGs[i].CopyFromScreen(b.Location, Point.Empty, b.Size);
int relX = b.Left - _bounds.Left;
int relY = b.Top - _bounds.Top;
_captureG.DrawImage(_monitorBmps[i], relX, relY);
}
}
_thumbG.DrawImage(_captureBmp, 0, 0, _thumbW, _thumbH);
long hash = DisplayHelper.ComputeImageHash(_thumbBmp, exL, exT, exR, exB);
if (hash != _prevHash || _showOverlay) {
string filename = DateTime.Now.ToString("yyyyMMdd_HHmmss_ff");
if (_saved == 0 || _showOverlay) {
_showOverlay = false;
using (var path = new System.Drawing.Drawing2D.GraphicsPath())
using (var fontFamily = new FontFamily("Consolas"))
{
_thumbG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int fontSize = Math.Max(16, _thumbH / 25);
float lineH = fontSize * 1.3f;
string qname = _quality == 25 ? "Low" : _quality == 50 ? "Medium" : _quality == 75 ? "High" : _quality == 100 ? "Best" : _quality.ToString();
path.AddString("Quality: " + qname, fontFamily, (int)FontStyle.Regular, fontSize, new PointF(0, 0), StringFormat.GenericDefault);
path.AddString("Scale: " + (int)(_scale * 100) + "%", fontFamily, (int)FontStyle.Regular, fontSize, new PointF(0, lineH), StringFormat.GenericDefault);
var bounds = path.GetBounds();
int pad = fontSize * 2 / 3;
// Find corner farthest from clock window
float clockCX = (exL + exR) / 2f, clockCY = (exT + exB) / 2f;
float[][] corners = { new[]{0f,0f}, new[]{1f,0f}, new[]{0f,1f}, new[]{1f,1f} };
int best = 0; float maxDist = 0;
for (int i = 0; i < 4; i++) {
float cx = corners[i][0] * _thumbW, cy = corners[i][1] * _thumbH;
float dx = cx - clockCX, dy = cy - clockCY;
if (dx*dx + dy*dy > maxDist) { maxDist = dx*dx + dy*dy; best = i; }
}
float x = (corners[best][0] == 0) ? pad : _thumbW - bounds.Width - pad * 3;
float y = (corners[best][1] == 0) ? pad : _thumbH - bounds.Height - pad * 3;
var matrix = new System.Drawing.Drawing2D.Matrix();
matrix.Translate(x - bounds.X + pad, y - bounds.Y + pad);
path.Transform(matrix);
var finalBounds = path.GetBounds();
using (var bgBrush = new SolidBrush(Color.FromArgb(180, 0, 0, 0)))
using (var bgPath = new System.Drawing.Drawing2D.GraphicsPath()) {
float bx = finalBounds.X - pad, by = finalBounds.Y - pad, bw = finalBounds.Width + pad * 2, bh = finalBounds.Height + pad * 2, r = fontSize / 2f;
bgPath.AddArc(bx, by, r * 2, r * 2, 180, 90); bgPath.AddArc(bx + bw - r * 2, by, r * 2, r * 2, 270, 90);
bgPath.AddArc(bx + bw - r * 2, by + bh - r * 2, r * 2, r * 2, 0, 90); bgPath.AddArc(bx, by + bh - r * 2, r * 2, r * 2, 90, 90);
bgPath.CloseFigure(); _thumbG.FillPath(bgBrush, bgPath);
}
_thumbG.FillPath(Brushes.White, path);
}
}
_thumbBmp.Save(System.IO.Path.Combine(_outDir, filename + ".jpg"), _jpegCodec, _encoderParams);
if (_saveMasked) {
using (var maskedBmp = new Bitmap(_thumbBmp))
using (var g = Graphics.FromImage(maskedBmp)) {
g.FillRectangle(Brushes.Black, exL, exT, exR - exL, exB - exT);
maskedBmp.Save(System.IO.Path.Combine(_outDir, filename + "_masked.jpg"), _jpegCodec, _encoderParams);
}
}
_saved++;
_prevHash = hash;
}
} catch (Exception ex) {
_lastError = ex.ToString();
if (_errorCallback != null) _errorCallback(_lastError);
}
var elapsed = (int)sw.ElapsedMilliseconds;
int sleep = _intervalMs - elapsed;
if (sleep > 0) {
try { Task.Delay(sleep, ct).Wait(); }
catch (AggregateException) { return; }
}
}
}
}
"@ -ReferencedAssemblies (@($drawingAsm,$primAsm) + @($winCoreAsm | Where-Object { $_ }))
[xml]$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Topmost="True" AllowsTransparency="True" WindowStyle="None" Background="#01000000"
SizeToContent="WidthAndHeight" ResizeMode="NoResize" Left="20" Top="20">
<Window.ContextMenu>
<ContextMenu>
<MenuItem Name="MenuInvisible" IsCheckable="True">
<MenuItem.Header>
<TextBlock><Underline>A</Underline>uto-hide</TextBlock>
</MenuItem.Header>
</MenuItem>
<MenuItem Name="MenuQuality" Header="Quality">
<MenuItem Name="MenuQ25" Header="Low (25)" IsCheckable="True"/>
<MenuItem Name="MenuQ50" Header="Medium (50)" IsCheckable="True"/>
<MenuItem Name="MenuQ75" Header="High (75)" IsCheckable="True" IsChecked="True"/>
<MenuItem Name="MenuQ100" Header="Best (100)" IsCheckable="True"/>
</MenuItem>
<MenuItem Name="MenuScale" Header="Scale">
<MenuItem Name="MenuS50" Header="50%" IsCheckable="True"/>
<MenuItem Name="MenuS75" Header="75%" IsCheckable="True" IsChecked="True"/>
<MenuItem Name="MenuS100" Header="100%" IsCheckable="True"/>
</MenuItem>
<MenuItem Name="MenuFPS" Header="FPS">
<MenuItem Name="MenuF1" Header="1" IsCheckable="True"/>
<MenuItem Name="MenuF2" Header="2" IsCheckable="True" IsChecked="True"/>
<MenuItem Name="MenuF5" Header="5" IsCheckable="True"/>
<MenuItem Name="MenuF10" Header="10" IsCheckable="True"/>
</MenuItem>
<MenuItem Name="MenuExit">
<MenuItem.Header>
<TextBlock>E<Underline>x</Underline>it</TextBlock>
</MenuItem.Header>
</MenuItem>
</ContextMenu>
</Window.ContextMenu>
<Border Name="MainBorder" Background="#AA000000" CornerRadius="8" Padding="10,6">
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Name="Clock" Foreground="White" FontSize="32" FontFamily="Consolas" VerticalAlignment="Center"/>
<StackPanel Margin="8,0,0,0" VerticalAlignment="Center">
<TextBlock Name="RecSpacer" Foreground="#AAAAAA" FontSize="11" Height="14" TextAlignment="Right"/>
<Button Name="BtnToggle" Content="● REC" Width="45" Height="22" FontSize="11" Background="#AA444444" Foreground="White" BorderThickness="0" Padding="0"/>
<TextBlock Name="RecCounter" Text="0" Foreground="#FF6666" FontSize="11" Height="14" TextAlignment="Right" Visibility="Hidden"/>
</StackPanel>
<TextBlock Name="MonitorLabel" Foreground="White" FontSize="10" Margin="4,0,0,0" VerticalAlignment="Center" Cursor="Hand" Visibility="Collapsed"/>
</StackPanel>
</StackPanel>
</Border>
</Window>
"@
$window = [Windows.Markup.XamlReader]::Load([System.Xml.XmlNodeReader]::new($xaml))
$clock = $window.FindName("Clock")
$btnToggle = $window.FindName("BtnToggle")
$mainBorder = $window.FindName("MainBorder")
$recCounter = $window.FindName("RecCounter")
$recSpacer = $window.FindName("RecSpacer")
$script:monitorLabel = $window.FindName("MonitorLabel")
$menuExit = $window.FindName("MenuExit")
$menuInvisible = $window.FindName("MenuInvisible")
$script:quality = $Quality
$script:qualityMenus = @{
25 = $window.FindName("MenuQ25")
50 = $window.FindName("MenuQ50")
75 = $window.FindName("MenuQ75")
100 = $window.FindName("MenuQ100")
}
foreach ($q in $script:qualityMenus.Keys) {
$menu = $script:qualityMenus[$q]
$menu.Tag = $q
$menu.IsChecked = ($q -eq $Quality)
$menu.Add_Click({
param($s,$e)
$script:quality = $s.Tag
foreach ($m in $script:qualityMenus.Values) { $m.IsChecked = ($m -eq $s) }
if ($script:recording) { $script:recorder.MarkSettingsChanged($script:quality) }
})
}
$script:scaleValue = $Scale
$script:scaleMenus = @{
0.5 = $window.FindName("MenuS50")
0.75 = $window.FindName("MenuS75")
1.0 = $window.FindName("MenuS100")
}
foreach ($s in $script:scaleMenus.Keys) {
$menu = $script:scaleMenus[$s]
$menu.Tag = $s
$menu.IsChecked = ($s -eq $Scale)
$menu.Add_Click({
param($sender,$e)
if ($script:recording) { return }
$script:scaleValue = $sender.Tag
foreach ($m in $script:scaleMenus.Values) { $m.IsChecked = ($m -eq $sender) }
Update-CaptureRegion
})
}
$script:fpsValue = $FPS
$script:fpsMenus = @{
1 = $window.FindName("MenuF1")
2 = $window.FindName("MenuF2")
5 = $window.FindName("MenuF5")
10 = $window.FindName("MenuF10")
}
foreach ($f in $script:fpsMenus.Keys) {
$menu = $script:fpsMenus[$f]
$menu.Tag = $f
$menu.IsChecked = ($f -eq $FPS)
$menu.Add_Click({
param($sender,$e)
if ($script:recording) { return }
$script:fpsValue = $sender.Tag
foreach ($m in $script:fpsMenus.Values) { $m.IsChecked = ($m -eq $sender) }
})
}
$script:invisible = $false
$menuExit.Add_Click({ $window.Close() })
$menuInvisible.Add_Checked({ $script:invisible = $true })
$menuInvisible.Add_Unchecked({ $script:invisible = $false; $mainBorder.BeginAnimation([System.Windows.UIElement]::OpacityProperty, $null); $mainBorder.Opacity = 1 })
$window.ContextMenu.Add_Closed({ if ($script:invisible -and -not $window.IsMouseOver) { $mainBorder.BeginAnimation([System.Windows.UIElement]::OpacityProperty, [System.Windows.Media.Animation.DoubleAnimation]::new(0, [TimeSpan]::FromMilliseconds(100))) } })
$menuExit.Parent.Add_KeyDown({ param($s,$e)
if ($e.Key -eq 'X') { $window.Close() }
if ($e.Key -eq 'A') { $menuInvisible.IsChecked = -not $menuInvisible.IsChecked }
})
$window.Add_MouseLeftButtonDown({ $window.DragMove() })
$window.Add_Deactivated({ $window.Topmost = $true })
$window.Add_MouseEnter({ if ($script:invisible) { $mainBorder.BeginAnimation([System.Windows.UIElement]::OpacityProperty, [System.Windows.Media.Animation.DoubleAnimation]::new(1, [TimeSpan]::FromMilliseconds(100))) } })
$window.Add_MouseLeave({ if ($script:invisible -and -not $window.ContextMenu.IsOpen -and -not ($script:monitorMenu -and $script:monitorMenu.IsOpen) -and -not $script:overlayVisible) { $mainBorder.BeginAnimation([System.Windows.UIElement]::OpacityProperty, [System.Windows.Media.Animation.DoubleAnimation]::new(0, [TimeSpan]::FromMilliseconds(100))) } })
function Update-FontSize($size) {
if ($size -lt 12 -or $size -gt 200) { return }
$clock.FontSize = $size
$btnToggle.FontSize = $size * 0.35
$btnToggle.Width = $size * 1.4
$btnToggle.Height = $size * 0.7
$btnToggle.Margin = [System.Windows.Thickness]::new($size * 0.25, 0, 0, 0)
$recCounter.FontSize = $size * 0.35; $recCounter.Height = $size * 0.45
$recSpacer.FontSize = $size * 0.35; $recSpacer.Height = $size * 0.45
$script:monitorLabel.FontSize = $size * 0.3
$script:monitorLabel.Margin = [System.Windows.Thickness]::new($size * 0.25, 0, 0, 0)
$mainBorder.Padding = [System.Windows.Thickness]::new($size * 0.3, $size * 0.2, $size * 0.3, $size * 0.2)
}
$window.Add_MouseWheel({ param($s,$e)
Update-FontSize ($clock.FontSize + ($e.Delta / 30))
})
# Settings file
$script:settingsPath = [System.IO.Path]::Combine($env:APPDATA, 'ScreenRecorder', 'settings.json')
function Save-Settings {
$settings = @{
Left = $window.Left
Top = $window.Top
FontSize = $clock.FontSize
AutoHide = $script:invisible
SelectedMonitors = $script:selectedMonitors
Quality = $script:quality
Scale = $script:scaleValue
FPS = $script:fpsValue
MonitorCount = $script:screens.Count
}
$dir = [System.IO.Path]::GetDirectoryName($script:settingsPath)
if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
$settings | ConvertTo-Json | Set-Content -Path $script:settingsPath -Encoding UTF8
}
function Load-Settings {
if (-not (Test-Path $script:settingsPath)) { return $null }
try { Get-Content -Path $script:settingsPath -Raw | ConvertFrom-Json } catch { Write-Warning "Failed to load settings: $_"; $null }
}
# Monitor setup
$script:screens = [System.Windows.Forms.Screen]::AllScreens
function Get-PhysicalBounds($screen) {
$dm = New-Object DisplayHelper+DEVMODE
$dm.dmSize = [System.Runtime.InteropServices.Marshal]::SizeOf($dm)
[DisplayHelper]::EnumDisplaySettings($screen.DeviceName, [DisplayHelper]::ENUM_CURRENT_SETTINGS, [ref]$dm) | Out-Null
[System.Drawing.Rectangle]::new($dm.dmPositionX, $dm.dmPositionY, $dm.dmPelsWidth, $dm.dmPelsHeight)
}
# Selected monitors (array of indices)
$primaryIdx = [Array]::IndexOf($script:screens, [System.Windows.Forms.Screen]::PrimaryScreen)
$script:selectedMonitors = @($(if ($primaryIdx -ge 0) { $primaryIdx } else { 0 }))
function Update-MonitorLabel {
if ($script:selectedMonitors.Count -eq 0) {
$script:monitorLabel.Text = "None"
} elseif ($script:selectedMonitors.Count -eq 1) {
$idx = $script:selectedMonitors[0]
$script:monitorLabel.Text = if ($script:screens[$idx].Primary) { "Mon $($idx+1)*" } else { "Mon $($idx+1)" }
} else {
$nums = ($script:selectedMonitors | Sort-Object | ForEach-Object { $_ + 1 }) -join '+'
$script:monitorLabel.Text = "Mon $nums"
}
}
function Update-CaptureRegion {
if ($script:selectedMonitors.Count -eq 0) { return }
# Calculate bounding rectangle of all selected monitors
$minX = [int]::MaxValue; $minY = [int]::MaxValue
$maxX = [int]::MinValue; $maxY = [int]::MinValue
foreach ($idx in $script:selectedMonitors) {
$b = Get-PhysicalBounds $script:screens[$idx]
if ($b.Left -lt $minX) { $minX = $b.Left }
if ($b.Top -lt $minY) { $minY = $b.Top }
if ($b.Right -gt $maxX) { $maxX = $b.Right }
if ($b.Bottom -gt $maxY) { $maxY = $b.Bottom }
}
$script:bounds = [System.Drawing.Rectangle]::new($minX, $minY, $maxX - $minX, $maxY - $minY)
$script:w = [int]($script:bounds.Width * $script:scaleValue)
$script:h = [int]($script:bounds.Height * $script:scaleValue)
}
function Show-MonitorOverlay {
param([switch]$ReopenMenu)
$script:pendingMenuReopen = $ReopenMenu.IsPresent
$script:overlayVisible = $true
$dpiScale = [System.Windows.Forms.SystemInformation]::VirtualScreen.Width / [System.Windows.SystemParameters]::VirtualScreenWidth
$overlays = @()
for ($i = 0; $i -lt $script:screens.Count; $i++) {
$scr = $script:screens[$i]
$isSelected = $script:selectedMonitors -contains $i
$wpfLeft = $scr.Bounds.Left / $dpiScale
$wpfTop = $scr.Bounds.Top / $dpiScale
$wpfWidth = $scr.Bounds.Width / $dpiScale
$wpfHeight = $scr.Bounds.Height / $dpiScale
[xml]$overlayXaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
WindowStyle="None" AllowsTransparency="True" Topmost="True"
Background="$( if ($isSelected) { '#88004488' } else { '#88000000' } )"
Left="$wpfLeft" Top="$wpfTop" Width="$wpfWidth" Height="$wpfHeight">
<Grid>
<TextBlock Text="$($i+1)" FontSize="300" FontWeight="Bold"
Foreground="$( if ($isSelected) { '#AAFFFFFF' } else { '#44FFFFFF' } )"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
"@
$overlay = [Windows.Markup.XamlReader]::Load([System.Xml.XmlNodeReader]::new($overlayXaml))
$overlay.Add_MouseLeftButtonDown({
param($s,$e)
$s.Close()
})
$overlay.Add_Closed({
$script:overlayCloseCount++
if ($script:overlayCloseCount -ge $script:overlayTotal) {
$script:overlayVisible = $false
if ($script:pendingMenuReopen) {
$script:pendingMenuReopen = $false
$script:monitorMenu.PlacementTarget = $script:monitorLabel
$script:monitorMenu.Placement = [System.Windows.Controls.Primitives.PlacementMode]::Bottom
$script:monitorMenu.IsOpen = $true
}
}
})
$overlay.Show()
$overlays += $overlay
}
$script:overlayCloseCount = 0
$script:overlayTotal = $overlays.Count
# Auto close after 1.5 seconds
$timer = [System.Windows.Threading.DispatcherTimer]::new()
$timer.Interval = [TimeSpan]::FromMilliseconds(1500)
$timer.Add_Tick({
$timer.Stop(); $timer.IsEnabled = $false
foreach ($o in $overlays) { if ($o.IsVisible) { $o.Close() } }
}.GetNewClosure())
$timer.Start()
}
if ($script:screens.Count -gt 1) {
$script:monitorLabel.Visibility = "Visible"
Update-MonitorLabel
# Create context menu with checkboxes
$script:monitorMenu = [System.Windows.Controls.ContextMenu]::new()
$script:monitorMenu.StaysOpen = $true
for ($i = 0; $i -lt $script:screens.Count; $i++) {
$menuItem = [System.Windows.Controls.MenuItem]::new()
$menuItem.Header = if ($script:screens[$i].Primary) { "Mon $($i+1)*" } else { "Mon $($i+1)" }
$menuItem.IsCheckable = $true
$menuItem.IsChecked = $script:selectedMonitors -contains $i
$menuItem.Tag = $i
$menuItem.StaysOpenOnClick = $true
$menuItem.Add_Click({
param($sender, $e)
$idx = $sender.Tag
if ($sender.IsChecked) {
if ($script:selectedMonitors -notcontains $idx) {
$script:selectedMonitors += $idx
}
} else {
# Prevent unchecking the last one
if ($script:selectedMonitors.Count -le 1) {
$sender.IsChecked = $true
return
}
$script:selectedMonitors = @($script:selectedMonitors | Where-Object { $_ -ne $idx })
}
Update-MonitorLabel
Update-CaptureRegion
})
$script:monitorMenu.Items.Add($menuItem) | Out-Null
}
$script:monitorLabel.ContextMenu = $script:monitorMenu
$script:monitorLabel.Add_MouseLeftButtonDown({
param($sender, $e)
if ($script:recording) {
# Show menu only (no overlay) during recording
$script:monitorMenu.PlacementTarget = $script:monitorLabel
$script:monitorMenu.Placement = [System.Windows.Controls.Primitives.PlacementMode]::Bottom
$script:monitorMenu.IsOpen = $true
} else {
Show-MonitorOverlay -ReopenMenu
}
$e.Handled = $true
})
}
$script:recording = $false
$script:outDir = $null
$script:recordFor = $RecordFor
$script:recordStartTime = $null
# Load saved settings
$savedSettings = Load-Settings
if ($savedSettings) {
# Check if monitor configuration changed
$monitorConfigChanged = $savedSettings.MonitorCount -ne $script:screens.Count
# Window position (skip if monitor config changed)
if (-not $monitorConfigChanged) {
if ($null -ne $savedSettings.Left) { $window.Left = $savedSettings.Left }
if ($null -ne $savedSettings.Top) { $window.Top = $savedSettings.Top }
}
# Font size
if ($savedSettings.FontSize) {
Update-FontSize $savedSettings.FontSize
}
# Auto-hide
if ($savedSettings.AutoHide) { $menuInvisible.IsChecked = $true }
# Quality
if ($savedSettings.Quality -ge 1 -and $savedSettings.Quality -le 100) {
$script:quality = $savedSettings.Quality
foreach ($m in $script:qualityMenus.Values) { $m.IsChecked = ($m.Tag -eq $script:quality) }
}
# Scale
if ($savedSettings.Scale -ge 0.1 -and $savedSettings.Scale -le 1.0) {
$script:scaleValue = $savedSettings.Scale
foreach ($m in $script:scaleMenus.Values) { $m.IsChecked = ($m.Tag -eq $script:scaleValue) }
}
# FPS
if ($savedSettings.FPS -in @(1, 2, 5, 10)) {
$script:fpsValue = $savedSettings.FPS
foreach ($m in $script:fpsMenus.Values) { $m.IsChecked = ($m.Tag -eq $script:fpsValue) }
}
# Selected monitors (skip if monitor config changed)
if (-not $monitorConfigChanged -and $savedSettings.SelectedMonitors) {
$validMonitors = @($savedSettings.SelectedMonitors | Where-Object { $_ -ge 0 -and $_ -lt $script:screens.Count })
if ($validMonitors.Count -gt 0) {
$script:selectedMonitors = $validMonitors
# Update monitor menu checkboxes
if ($script:monitorMenu) {
foreach ($item in $script:monitorMenu.Items) {
$item.IsChecked = $script:selectedMonitors -contains $item.Tag
}
}
Update-MonitorLabel
}
}
}
Update-CaptureRegion
# Background recorder instance
$script:recorder = [BackgroundRecorder]::new()
# Get window handle for physical coordinate calculation
$windowHelper = [System.Windows.Interop.WindowInteropHelper]::new($window)
# Test write access to a directory
function Test-WriteAccess {
param([string]$Path)
try {
$testFile = Join-Path $Path ".sr_test_$PID"
[System.IO.File]::WriteAllText($testFile, 'test')
Remove-Item $testFile -Force
return $true
} catch {
return $false
}
}
$btnToggle.Add_Click({
if (-not $script:recording) {
# Check write access and determine output directory
if ($OutputPath) {
if (Test-WriteAccess $OutputPath) {
$baseDir = $OutputPath
} else {
New-Item -ItemType Directory -Path $OutputPath -Force -ErrorAction SilentlyContinue | Out-Null
if (Test-WriteAccess $OutputPath) {
$baseDir = $OutputPath
} else {
[System.Windows.MessageBox]::Show("Output path is not writable: $OutputPath", "Error", "OK", "Error")
return
}
}
$script:outDir = Join-Path $baseDir (Get-Date -Format 'yyyyMMdd_HHmmss')
} else {
$currentDir = Get-Location
if (Test-WriteAccess $currentDir) {
$baseDir = $currentDir.Path
} else {
$baseDir = Join-Path $env:TEMP 'ScreenRecorder'
New-Item -ItemType Directory -Path $baseDir -Force -ErrorAction SilentlyContinue | Out-Null
[System.Windows.MessageBox]::Show("Current directory is not writable.`n`nSaving to: $baseDir", "Warning", "OK", "Warning")
}
$script:outDir = Join-Path $baseDir "ScreenCaptures\$(Get-Date -Format 'yyyyMMdd_HHmmss')"
}
# Start recording
New-Item -ItemType Directory -Path $script:outDir -Force | Out-Null
# Build monitor bounds array
$monitorBoundsArray = @()
foreach ($idx in $script:selectedMonitors) {
$monitorBoundsArray += Get-PhysicalBounds $script:screens[$idx]
}
# Error callback for recording errors
$errorHandler = {
param([string]$errorMsg)
$window.Dispatcher.Invoke({
[System.Windows.MessageBox]::Show("Recording error occurred:`n`n$errorMsg", "Recording Error", "OK", "Error")
# Stop recording on error
if ($script:recording) {
$btnToggle.RaiseEvent([System.Windows.RoutedEventArgs]::new([System.Windows.Controls.Primitives.ButtonBase]::ClickEvent))
}
})
}
$started = $script:recorder.Start($script:bounds, [System.Drawing.Rectangle[]]$monitorBoundsArray, $script:w, $script:h, $script:fpsValue, $script:quality, $script:outDir, $SaveMasked, $script:scaleValue, $windowHelper.Handle, $errorHandler)
if (-not $started) {
[System.Windows.MessageBox]::Show("Recording failed: $($script:recorder.LastError)", "Error", "OK", "Error")
Remove-Item -Path $script:outDir -Force -ErrorAction SilentlyContinue
return
}
$script:recording = $true
$btnToggle.Content = "■ STOP"
$btnToggle.Foreground = [System.Windows.Media.Brushes]::Red
$script:monitorLabel.Opacity = 0.5
foreach ($m in $script:scaleMenus.Values) { $m.IsEnabled = $false }
foreach ($m in $script:fpsMenus.Values) { $m.IsEnabled = $false }
if ($script:monitorMenu) { foreach ($m in $script:monitorMenu.Items) { $m.IsEnabled = $false } }
$recCounter.Text = "0"; $recCounter.Visibility = "Visible"
if ($script:recordFor -gt [TimeSpan]::Zero) {
$script:recordStartTime = [DateTime]::Now
$recSpacer.Text = $script:recordFor.ToString('hh\:mm\:ss')
}
} else {
# Stop recording
$script:recorder.Stop()
$script:recording = $false
$script:monitorLabel.Opacity = 1.0
foreach ($m in $script:scaleMenus.Values) { $m.IsEnabled = $true }
foreach ($m in $script:fpsMenus.Values) { $m.IsEnabled = $true }
if ($script:monitorMenu) { foreach ($m in $script:monitorMenu.Items) { $m.IsEnabled = $true } }
$recCounter.Visibility = "Hidden"
$recSpacer.Text = ""
$script:recordStartTime = $null
$script:recordFor = [TimeSpan]::Zero
if ($script:stopTimer) { $script:stopTimer.Stop(); $script:stopTimer = $null }
$btnToggle.Content = "● REC"
$btnToggle.Foreground = [System.Windows.Media.Brushes]::White
Start-Process explorer $script:outDir
}
})
$clockTimer = New-Object System.Windows.Threading.DispatcherTimer
$clockTimer.Interval = [TimeSpan]::FromMilliseconds(100)
$clockTimer.Add_Tick({
$clock.Text = (Get-Date).ToString("HH:mm:ss.f")
if ($script:recording) {
$recCounter.Text = $script:recorder.Saved
if ($script:recordStartTime) {
$elapsed = [DateTime]::Now - $script:recordStartTime
$remaining = $script:recordFor - $elapsed
if ($remaining -lt [TimeSpan]::Zero) { $remaining = [TimeSpan]::Zero }
$recSpacer.Text = $remaining.ToString('hh\:mm\:ss')
}
}
})
$clockTimer.Start()
$window.Add_Closed({
$clockTimer.Stop()
Save-Settings
if ($script:recording) { $script:recorder.Stop(); Start-Process explorer $script:outDir }
})
if ($ReadyFile) { New-Item -Path $ReadyFile -ItemType File -Force | Out-Null }
# Auto-start recording if -RecordFor is specified
if ($RecordFor -gt [TimeSpan]::Zero) {
# Hide window immediately if Auto-hide is enabled
if ($script:invisible) {
$mainBorder.Opacity = 0
}
$window.Add_ContentRendered({
# Initialize clock and wait for UI rendering to complete
$clock.Text = (Get-Date).ToString("HH:mm:ss.f")
$window.Dispatcher.BeginInvoke([System.Windows.Threading.DispatcherPriority]::Loaded, [Action]{
$btnToggle.RaiseEvent([System.Windows.RoutedEventArgs]::new([System.Windows.Controls.Primitives.ButtonBase]::ClickEvent))
# Set up auto-stop timer
$script:stopTimer = [System.Windows.Threading.DispatcherTimer]::new()
$script:stopTimer.Interval = $RecordFor
$script:stopTimer.Add_Tick({
$script:stopTimer.Stop()
if ($script:recording) {
$btnToggle.RaiseEvent([System.Windows.RoutedEventArgs]::new([System.Windows.Controls.Primitives.ButtonBase]::ClickEvent))
}
$window.Close()
})
$script:stopTimer.Start()
})
})
}
$window.ShowDialog()
}
# Run only when invoked directly (not dot-sourced or imported as module)
if ($MyInvocation.InvocationName -notin '.', '') {
Start-ScreenRecorder @PSBoundParameters
}