PowerShell实现指定范围放大镜功能(可自由选择区域,可多开)

PowerShell实现指定范围放大镜功能(可自由选择区域,可多开)

复制到PowerShell或者PowerShell ISE里运行即可

win11以下不做PowerShell兼容。

1.自由选择区域、范围放大
2.可选择放大倍数
3.可多开PowerShell脚本,多个放大区域,可跨显示器显示
4.可设置放大镜窗口透明度
5.可更改截取边框颜色
6.不符合规则的输入可能导致报错。
7.只是个PowerShell代码,不是exe程序,代码开源,自己运行/研究,不要问什么成品。

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
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

#---------------------------------------------
# 全局参数(初始默认值,可通过配置窗口修改)
$global:scale = 5 # 放大倍数
$global:opacity = 1.0 # 窗口透明度,0~1
$global:borderColor = [System.Drawing.Color]::Red # 选择区域边框颜色
$global:handleSize = 8 # 调整手柄大小(像素)
$global:minSize = 20 # 选择区域允许的最小尺寸
#---------------------------------------------

#=============================================
$configForm = New-Object System.Windows.Forms.Form
$configForm.Text = "配置参数"
$configForm.Size = New-Object System.Drawing.Size(300, 250)
$configForm.StartPosition = "CenterScreen"

$lblScale = New-Object System.Windows.Forms.Label
$lblScale.Text = "放大倍数:"
$lblScale.Location = New-Object System.Drawing.Point(20, 20)
$lblScale.Size = New-Object System.Drawing.Size(100,20)

$nudScale = New-Object System.Windows.Forms.NumericUpDown
$nudScale.Minimum = 1
$nudScale.Maximum = 10
$nudScale.Value = $global:scale
$nudScale.Location = New-Object System.Drawing.Point(120, 20)
$nudScale.Size = New-Object System.Drawing.Size(100,20)

$lblOpacity = New-Object System.Windows.Forms.Label
$lblOpacity.Text = "透明度(0-1):"
$lblOpacity.Location = New-Object System.Drawing.Point(20, 50)
$lblOpacity.Size = New-Object System.Drawing.Size(100,20)

$nudOpacity = New-Object System.Windows.Forms.NumericUpDown
$nudOpacity.Minimum = 0
$nudOpacity.Maximum = 1
$nudOpacity.DecimalPlaces = 1
$nudOpacity.Increment = 0.1
$nudOpacity.Value = $global:opacity
$nudOpacity.Location = New-Object System.Drawing.Point(120, 50)
$nudOpacity.Size = New-Object System.Drawing.Size(100,20)

$lblBorderColor = New-Object System.Windows.Forms.Label
$lblBorderColor.Text = "边框颜色:"
$lblBorderColor.Location = New-Object System.Drawing.Point(20, 80)
$lblBorderColor.Size = New-Object System.Drawing.Size(100,20)

$cboBorderColor = New-Object System.Windows.Forms.ComboBox
$cboBorderColor.DropDownStyle = "DropDownList"
$cboBorderColor.Items.AddRange(@("Red","Blue","Green","Yellow","Black","White"))
$cboBorderColor.SelectedItem = "Red"
$cboBorderColor.Location = New-Object System.Drawing.Point(120, 80)
$cboBorderColor.Size = New-Object System.Drawing.Size(100,20)

$lblHandleSize = New-Object System.Windows.Forms.Label
$lblHandleSize.Text = "四角手柄大小:"
$lblHandleSize.Location = New-Object System.Drawing.Point(20, 110)
$lblHandleSize.Size = New-Object System.Drawing.Size(100,20)

$nudHandleSize = New-Object System.Windows.Forms.NumericUpDown
$nudHandleSize.Minimum = 4
$nudHandleSize.Maximum = 50
$nudHandleSize.Value = $global:handleSize
$nudHandleSize.Location = New-Object System.Drawing.Point(120, 110)
$nudHandleSize.Size = New-Object System.Drawing.Size(100,20)

$lblMinSize = New-Object System.Windows.Forms.Label
$lblMinSize.Text = "截取框最小尺寸:"
$lblMinSize.Location = New-Object System.Drawing.Point(20, 140)
$lblMinSize.Size = New-Object System.Drawing.Size(100,20)

$nudMinSize = New-Object System.Windows.Forms.NumericUpDown
$nudMinSize.Minimum = 10
$nudMinSize.Maximum = 500
$nudMinSize.Value = $global:minSize
$nudMinSize.Location = New-Object System.Drawing.Point(120, 140)
$nudMinSize.Size = New-Object System.Drawing.Size(100,20)

$btnConfirm = New-Object System.Windows.Forms.Button
$btnConfirm.Text = "确认"
$btnConfirm.Location = New-Object System.Drawing.Point(100, 180)
$btnConfirm.Size = New-Object System.Drawing.Size(80,30)

$btnConfirm.Add_Click({
$global:scale = [int]$nudScale.Value
$global:opacity = [double]$nudOpacity.Value
$global:borderColor = [System.Drawing.Color]::FromName($cboBorderColor.SelectedItem)
$global:handleSize = [int]$nudHandleSize.Value
$global:minSize = [int]$nudMinSize.Value

$zoomForm.Opacity = $global:opacity
$overlayForm.Invalidate()

$newWidth = $rect.Width * $global:scale
$newHeight = $rect.Height * $global:scale
if($newWidth -gt 0 -and $newHeight -gt 0) {
$zoomForm.ClientSize = New-Object System.Drawing.Size($newWidth, $newHeight)
}
})

$configForm.Controls.AddRange(@($lblScale, $nudScale, $lblOpacity, $nudOpacity, $lblBorderColor, $cboBorderColor, $lblHandleSize, $nudHandleSize, $lblMinSize, $nudMinSize, $btnConfirm))


# 初始选择区域(用于截图的区域)
$rect = @{
X = 100
Y = 100
Width = 35
Height = 35
}

$overlayForm = New-Object System.Windows.Forms.Form
$overlayForm.Text = "选择区域"
$overlayForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
$overlayForm.TopMost = $true
$overlayForm.BackColor = [System.Drawing.Color]::Magenta
$overlayForm.TransparencyKey = [System.Drawing.Color]::Magenta
$overlayForm.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
$overlayForm.ClientSize = [System.Drawing.Size]::new(
[System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width,
[System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height
)
$overlayForm.Location = [System.Drawing.Point]::new(0, 0)
$overlayForm.ShowInTaskbar = $false

# 创建放大窗口(用于显示选中区域放大后的内容)
$zoomForm = New-Object System.Windows.Forms.Form
$zoomForm.Text = "放大镜(Alt+9退出)"
$zoomForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::SizableToolWindow

$zoomForm.TopMost = $true
$zoomForm.Add_Deactivate({
$zoomForm.TopMost = $true
})

$zoomForm.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
$zoomForm.Opacity = $global:opacity
$zoomForm.Size = [System.Drawing.Size]::new(400,300)
$zoomForm.ShowInTaskbar = $false

# 初始放大窗口位置(屏幕右侧)
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
$zoomForm.Location = [System.Drawing.Point]::new(
$screen.Width - $zoomForm.Width - 20,
[Math]::Max(20, [Math]::Min($rect.Y, $screen.Height - $zoomForm.Height))
)

$pb = New-Object System.Windows.Forms.PictureBox
$pb.Dock = [System.Windows.Forms.DockStyle]::Fill
$pb.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$zoomForm.Controls.Add($pb)

$mouseState = @{
IsDragging = $false
StartPos = [System.Drawing.Point]::Empty
ResizeEdge = $null
}

$zoomDragState = @{
IsDragging = $false
StartPos = [System.Drawing.Point]::Empty
}

# 全局退出标志
$global:exiting = $false

function Exit-Application {
if ($global:exiting) { return }
$global:exiting = $true
try { $timer.Stop() } catch {}
try { $zoomForm.Close() } catch {}
try { $overlayForm.Close() } catch {}
try { $configForm.Close() } catch {}
if ($pb.Image) { $pb.Image.Dispose() }
[System.Windows.Forms.Application]::Exit()
}

# 注册快捷键(Alt+9 退出)
$registerHotkey = {
param($s, $e)
if ($e.Alt -and ($e.KeyCode -eq [System.Windows.Forms.Keys]::D9)) {
Exit-Application
}
}
$overlayForm.Add_KeyDown($registerHotkey)
$zoomForm.Add_KeyDown($registerHotkey)
$configForm.Add_KeyDown($registerHotkey)
$overlayForm.KeyPreview = $true
$zoomForm.KeyPreview = $true
$configForm.KeyPreview = $true

$overlayForm.Add_Paint({
param($sender, $e)
$e.Graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias

# 绘制红框
$pen = New-Object System.Drawing.Pen($global:borderColor, 2)
$e.Graphics.DrawRectangle($pen, $rect.X, $rect.Y, $rect.Width, $rect.Height)
$pen.Dispose()

# 绘制四角的调整手柄
$handleBrush = New-Object System.Drawing.SolidBrush($global:borderColor)
foreach ($point in @(
[System.Drawing.Point]::new($rect.X, $rect.Y),
[System.Drawing.Point]::new($rect.X + $rect.Width, $rect.Y),
[System.Drawing.Point]::new($rect.X, $rect.Y + $rect.Height),
[System.Drawing.Point]::new($rect.X + $rect.Width, $rect.Y + $rect.Height)
)) {
$e.Graphics.FillRectangle(
$handleBrush,
$point.X - $global:handleSize/2,
$point.Y - $global:handleSize/2,
$global:handleSize,
$global:handleSize
)
}
$handleBrush.Dispose()
})

$overlayForm.Add_MouseDown({
param($s, $e)
$hitTest = @{
Left = ($e.X -ge ($rect.X - $global:handleSize)) -and ($e.X -le ($rect.X + $global:handleSize))
Right = ($e.X -ge ($rect.X + $rect.Width - $global:handleSize)) -and ($e.X -le ($rect.X + $rect.Width + $global:handleSize))
Top = ($e.Y -ge ($rect.Y - $global:handleSize)) -and ($e.Y -le ($rect.Y + $global:handleSize))
Bottom = ($e.Y -ge ($rect.Y + $rect.Height - $global:handleSize)) -and ($e.Y -le ($rect.Y + $rect.Height + $global:handleSize))
}
if ($hitTest.Left -and $hitTest.Top) { $mouseState.ResizeEdge = "LeftTop" }
elseif ($hitTest.Right -and $hitTest.Top) { $mouseState.ResizeEdge = "RightTop" }
elseif ($hitTest.Left -and $hitTest.Bottom) { $mouseState.ResizeEdge = "LeftBottom" }
elseif ($hitTest.Right -and $hitTest.Bottom) { $mouseState.ResizeEdge = "RightBottom" }
elseif ($hitTest.Left) { $mouseState.ResizeEdge = "Left" }
elseif ($hitTest.Right) { $mouseState.ResizeEdge = "Right" }
elseif ($hitTest.Top) { $mouseState.ResizeEdge = "Top" }
elseif ($hitTest.Bottom) { $mouseState.ResizeEdge = "Bottom" }
else {
$mouseState.IsDragging = $true
}
$mouseState.StartPos = $e.Location
$overlayForm.Capture = $true
})

$overlayForm.Add_MouseMove({
param($s, $e)
if (-not $mouseState.IsDragging -and -not $mouseState.ResizeEdge) { return }
$deltaX = $e.X - $mouseState.StartPos.X
$deltaY = $e.Y - $mouseState.StartPos.Y

if ($mouseState.IsDragging) {
$rect.X = [Math]::Max(0, [Math]::Min($rect.X + $deltaX, $overlayForm.Width - $rect.Width))
$rect.Y = [Math]::Max(0, [Math]::Min($rect.Y + $deltaY, $overlayForm.Height - $rect.Height))
}
else {
switch ($mouseState.ResizeEdge) {
{$_ -match "Left"} {
$newWidth = $rect.Width - $deltaX
if ($newWidth -ge $global:minSize) {
$rect.X = [Math]::Max(0, $rect.X + $deltaX)
$rect.Width = $newWidth
}
}
{$_ -match "Right"} {
$rect.Width = [Math]::Min(
[Math]::Max($global:minSize, $rect.Width + $deltaX),
$overlayForm.Width - $rect.X)
}
{$_ -match "Top"} {
$newHeight = $rect.Height - $deltaY
if ($newHeight -ge $global:minSize) {
$rect.Y = [Math]::Max(0, $rect.Y + $deltaY)
$rect.Height = $newHeight
}
}
{$_ -match "Bottom"} {
$rect.Height = [Math]::Min(
[Math]::Max($global:minSize, $rect.Height + $deltaY),
$overlayForm.Height - $rect.Y)
}
}
}
$mouseState.StartPos = $e.Location
$overlayForm.Invalidate()
})

$overlayForm.Add_MouseUp({
$mouseState.IsDragging = $false
$mouseState.ResizeEdge = $null
$overlayForm.Capture = $false
})

$pb.Add_MouseDown({
param($s, $e)
if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
$zoomDragState.IsDragging = $true
$zoomDragState.StartPos = $e.Location
$zoomForm.Capture = $true
}
})
$pb.Add_MouseMove({
param($s, $e)
if ($zoomDragState.IsDragging) {
$numDeltaX = [int]($e.X - $zoomDragState.StartPos.X)
$numDeltaY = [int]($e.Y - $zoomDragState.StartPos.Y)
$newX = [int]($zoomForm.Left) + $numDeltaX
$newY = [int]($zoomForm.Top) + $numDeltaY
$zoomForm.Location = New-Object System.Drawing.Point($newX, $newY)
}
})
$pb.Add_MouseUp({
$zoomDragState.IsDragging = $false
$zoomForm.Capture = $false
})

# 为放大窗口空白区域的拖动添加支持
$zoomForm.Add_MouseDown({
param($s, $e)
if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
$zoomDragState.IsDragging = $true
$zoomDragState.StartPos = $e.Location
$zoomForm.Capture = $true
}
})
$zoomForm.Add_MouseMove({
param($s, $e)
if ($zoomDragState.IsDragging) {
$numDeltaX = [int]($e.X - $zoomDragState.StartPos.X)
$numDeltaY = [int]($e.Y - $zoomDragState.StartPos.Y)
$newX = [int]($zoomForm.Left) + $numDeltaX
$newY = [int]($zoomForm.Top) + $numDeltaY
$zoomForm.Location = New-Object System.Drawing.Point($newX, $newY)
}
})
$zoomForm.Add_MouseUp({
$zoomDragState.IsDragging = $false
$zoomForm.Capture = $false
})

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 50
$timer.Add_Tick({
if ($rect.Width -le 0 -or $rect.Height -le 0) { return }

# 根据当前选择区域和放大倍数更新放大窗口客户区尺寸
$newWidth = $rect.Width * $global:scale
$newHeight = $rect.Height * $global:scale
if ($newWidth -gt 0 -and $newHeight -gt 0) {
$zoomForm.ClientSize = New-Object System.Drawing.Size($newWidth, $newHeight)
}

try {
$bitmap = New-Object System.Drawing.Bitmap([Math]::Max(1, $rect.Width), [Math]::Max(1, $rect.Height))
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen([Math]::Max(0, $rect.X), [Math]::Max(0, $rect.Y), 0, 0, $bitmap.Size)
$graphics.Dispose()

if ($pb.Image) {
$pb.Image.Dispose()
$pb.Image = $null
}
$pb.Image = $bitmap.GetThumbnailImage($zoomForm.ClientSize.Width, $zoomForm.ClientSize.Height, $null, [System.IntPtr]::Zero)
}
catch {
if ($bitmap) { $bitmap.Dispose() }
}
})

$overlayForm.Add_FormClosing({ Exit-Application })
$zoomForm.Add_FormClosing({ Exit-Application })
$configForm.Add_FormClosing({ Exit-Application })

# 启动定时器并显示所有窗口
$timer.Start()
$zoomForm.Show()
$overlayForm.Show()
$configForm.Show()

[System.Windows.Forms.Application]::Run()