@inkbite/shotink 0.0.0-stage → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +108 -2
- package/README.zh.md +109 -0
- package/capture.ps1 +2266 -0
- package/client.js +791 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +4 -0
- package/index.js +565 -0
- package/locale/en.json +13 -0
- package/locale/zh.json +13 -0
- package/package.json +63 -6
package/capture.ps1
ADDED
|
@@ -0,0 +1,2266 @@
|
|
|
1
|
+
param(
|
|
2
|
+
[string]$OutDir = '',
|
|
3
|
+
[int]$TimeoutSeconds = 120,
|
|
4
|
+
[switch]$FullScreen,
|
|
5
|
+
[int]$DelaySeconds = 0,
|
|
6
|
+
# Resident mode: stay alive, take commands from stdin, one overlay per command.
|
|
7
|
+
# A cold powershell.exe costs ~1.2s before any pixel is grabbed; keeping the
|
|
8
|
+
# process (and its compiled types) warm is what makes the button feel instant.
|
|
9
|
+
[switch]$Serve,
|
|
10
|
+
# Host process to watch: if it dies, a full-screen overlay must not survive it.
|
|
11
|
+
[int]$ParentPid = 0,
|
|
12
|
+
# System-wide hotkey spec ('alt+shift+s') or 'off' for window-only capture.
|
|
13
|
+
[string]$Hotkey = 'off',
|
|
14
|
+
# Floating ball: 'on' keeps a small always-on-top button on the desktop that
|
|
15
|
+
# starts a capture with one click, 'off' removes it.
|
|
16
|
+
[string]$Float = 'on'
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
$ErrorActionPreference = 'Stop'
|
|
20
|
+
|
|
21
|
+
# Keep captured pixels and overlay coordinates aligned on scaled displays.
|
|
22
|
+
# System-DPI awareness only survives a single scaling factor: on a laptop panel
|
|
23
|
+
# at 150% plus an external 100% monitor, hover rectangles drift and the captured
|
|
24
|
+
# frame comes back scaled. Prefer per-monitor v2 and fall back on older builds
|
|
25
|
+
# (the call is a no-op once the process already carries an awareness setting).
|
|
26
|
+
try {
|
|
27
|
+
Add-Type -TypeDefinition @'
|
|
28
|
+
using System;
|
|
29
|
+
using System.Runtime.InteropServices;
|
|
30
|
+
public static class DshDpi {
|
|
31
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr context);
|
|
32
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
33
|
+
public static void Enable() {
|
|
34
|
+
try { if (SetProcessDpiAwarenessContext(new IntPtr(-4))) return; } catch { }
|
|
35
|
+
try { SetProcessDPIAware(); } catch { }
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
'@ -ErrorAction Stop
|
|
39
|
+
[DshDpi]::Enable()
|
|
40
|
+
} catch { }
|
|
41
|
+
|
|
42
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
43
|
+
Add-Type -AssemblyName System.Drawing
|
|
44
|
+
|
|
45
|
+
# System-wide hotkey: a separate STA thread owns a hidden window so RegisterHotKey
|
|
46
|
+
# works while the resident loop sits blocked on stdin. A press just prints HOTKEY;
|
|
47
|
+
# the host turns that into one capture, so the gesture behaves exactly like the
|
|
48
|
+
# button while another app has focus.
|
|
49
|
+
try {
|
|
50
|
+
Add-Type -ReferencedAssemblies System.Windows.Forms, System.Drawing -TypeDefinition @'
|
|
51
|
+
using System;
|
|
52
|
+
using System.Runtime.InteropServices;
|
|
53
|
+
using System.Windows.Forms;
|
|
54
|
+
|
|
55
|
+
public static class DshHotkey {
|
|
56
|
+
private const int WM_HOTKEY = 0x0312;
|
|
57
|
+
private const int HOTKEY_ID = 0x4451;
|
|
58
|
+
[DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
|
|
59
|
+
[DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
|
|
60
|
+
|
|
61
|
+
private sealed class Pump : Form {
|
|
62
|
+
protected override void WndProc(ref Message m) {
|
|
63
|
+
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == HOTKEY_ID) {
|
|
64
|
+
try { Console.Out.WriteLine("HOTKEY"); Console.Out.Flush(); } catch { }
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
base.WndProc(ref m);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private static Pump pump;
|
|
72
|
+
private static System.Threading.Thread thread;
|
|
73
|
+
private static int currentMods = -1;
|
|
74
|
+
private static int currentKey = -1;
|
|
75
|
+
// Last registration outcome, so PowerShell can report it instead of guessing.
|
|
76
|
+
public static string Last = "none";
|
|
77
|
+
|
|
78
|
+
public static void Start(int modifiers, int key) {
|
|
79
|
+
// The renderer re-pushes its stored combo on every load; rebinding the same
|
|
80
|
+
// keys would race with the old pump and lose the registration.
|
|
81
|
+
if (pump != null && currentMods == modifiers && currentKey == key) {
|
|
82
|
+
Last = "on";
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
Stop();
|
|
86
|
+
Last = "pending";
|
|
87
|
+
var form = new Pump();
|
|
88
|
+
form.FormBorderStyle = FormBorderStyle.None;
|
|
89
|
+
form.ShowInTaskbar = false;
|
|
90
|
+
form.Opacity = 0;
|
|
91
|
+
form.Width = 1;
|
|
92
|
+
form.Height = 1;
|
|
93
|
+
form.StartPosition = FormStartPosition.Manual;
|
|
94
|
+
form.Location = new System.Drawing.Point(-4000, -4000);
|
|
95
|
+
var runner = new System.Threading.Thread(delegate() {
|
|
96
|
+
try {
|
|
97
|
+
form.Handle.ToString();
|
|
98
|
+
bool ok = RegisterHotKey(form.Handle, HOTKEY_ID, modifiers, key);
|
|
99
|
+
Last = ok ? "on" : "fail";
|
|
100
|
+
try { Console.Out.WriteLine(ok ? "HOTKEYSTATE|on" : "HOTKEYSTATE|fail"); Console.Out.Flush(); } catch { }
|
|
101
|
+
Application.Run(form);
|
|
102
|
+
} catch (Exception error) {
|
|
103
|
+
Last = "error:" + error.GetType().Name;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
runner.IsBackground = true;
|
|
107
|
+
runner.SetApartmentState(System.Threading.ApartmentState.STA);
|
|
108
|
+
currentMods = modifiers;
|
|
109
|
+
currentKey = key;
|
|
110
|
+
thread = runner;
|
|
111
|
+
runner.Start();
|
|
112
|
+
pump = form;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public static void Stop() {
|
|
116
|
+
var form = pump;
|
|
117
|
+
pump = null;
|
|
118
|
+
var runner = thread;
|
|
119
|
+
thread = null;
|
|
120
|
+
currentMods = -1;
|
|
121
|
+
currentKey = -1;
|
|
122
|
+
Last = "off";
|
|
123
|
+
if (form == null) { return; }
|
|
124
|
+
try {
|
|
125
|
+
// Synchronous: the next Start must not race the old registration away.
|
|
126
|
+
form.Invoke(new Action(delegate() {
|
|
127
|
+
try { UnregisterHotKey(form.Handle, HOTKEY_ID); } catch { }
|
|
128
|
+
Application.ExitThread();
|
|
129
|
+
}));
|
|
130
|
+
} catch { }
|
|
131
|
+
try { if (runner != null) runner.Join(500); } catch { }
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
'@ -ErrorAction Stop
|
|
135
|
+
} catch { }
|
|
136
|
+
|
|
137
|
+
# Floating ball: a 48px disc pinned above every window that starts a capture on
|
|
138
|
+
# one click, so a shot is one gesture away even when DSH sits behind another app.
|
|
139
|
+
# It runs on its own STA thread - the resident loop is blocked on stdin - and it
|
|
140
|
+
# never activates, because taking focus would pull DSH forward only to have it
|
|
141
|
+
# hidden again by the capture.
|
|
142
|
+
try {
|
|
143
|
+
Add-Type -ReferencedAssemblies System.Windows.Forms, System.Drawing -TypeDefinition @'
|
|
144
|
+
using System;
|
|
145
|
+
using System.Drawing;
|
|
146
|
+
using System.Drawing.Drawing2D;
|
|
147
|
+
using System.IO;
|
|
148
|
+
using System.Runtime.InteropServices;
|
|
149
|
+
using System.Windows.Forms;
|
|
150
|
+
|
|
151
|
+
public static class DshFloat {
|
|
152
|
+
private const int WS_EX_NOACTIVATE = 0x08000000;
|
|
153
|
+
private const int WS_EX_TOOLWINDOW = 0x00000080;
|
|
154
|
+
private const int WS_EX_TOPMOST = 0x00000008;
|
|
155
|
+
[DllImport("user32.dll")] private static extern IntPtr SetCapture(IntPtr hWnd);
|
|
156
|
+
[DllImport("user32.dll")] private static extern bool ReleaseCapture();
|
|
157
|
+
|
|
158
|
+
private static string posFile;
|
|
159
|
+
private static string flagFile;
|
|
160
|
+
private static Ball ball;
|
|
161
|
+
private static System.Threading.Thread thread;
|
|
162
|
+
private static bool suspended;
|
|
163
|
+
|
|
164
|
+
public static string Last = "none";
|
|
165
|
+
|
|
166
|
+
private sealed class Ball : Form {
|
|
167
|
+
public bool UserVisible = true;
|
|
168
|
+
private bool dragging;
|
|
169
|
+
private bool moved;
|
|
170
|
+
private Point grabCursor;
|
|
171
|
+
private Point grabOrigin;
|
|
172
|
+
private bool hover;
|
|
173
|
+
private ContextMenuStrip popup;
|
|
174
|
+
|
|
175
|
+
public Ball() {
|
|
176
|
+
FormBorderStyle = FormBorderStyle.None;
|
|
177
|
+
StartPosition = FormStartPosition.Manual;
|
|
178
|
+
ShowInTaskbar = false;
|
|
179
|
+
TopMost = true;
|
|
180
|
+
AutoScaleMode = AutoScaleMode.None;
|
|
181
|
+
DoubleBuffered = true;
|
|
182
|
+
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
|
|
183
|
+
Size = new Size(48, 48);
|
|
184
|
+
Opacity = 0.95;
|
|
185
|
+
Cursor = Cursors.Hand;
|
|
186
|
+
// Held as a field rather than assigned to Form.ContextMenuStrip: this window
|
|
187
|
+
// never activates, so the shell's WM_CONTEXTMENU path does not fire reliably
|
|
188
|
+
// and the menu is raised by hand from the right button instead.
|
|
189
|
+
popup = new ContextMenuStrip();
|
|
190
|
+
popup.Items.Add("隐藏悬浮球", null, delegate { HideByUser(); });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
protected override CreateParams CreateParams {
|
|
194
|
+
get {
|
|
195
|
+
var cp = base.CreateParams;
|
|
196
|
+
cp.ExStyle |= WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
|
|
197
|
+
return cp;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// WinForms still rescales a form to the monitor DPI once the handle exists,
|
|
202
|
+
// even with AutoScaleMode.None on this build. The ball must stay 48 physical
|
|
203
|
+
// pixels to sit next to the toolbar icons at the same visual size.
|
|
204
|
+
protected override void OnHandleCreated(EventArgs e) {
|
|
205
|
+
base.OnHandleCreated(e);
|
|
206
|
+
if (Width != 48 || Height != 48) { Size = new Size(48, 48); }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
protected override void OnResize(EventArgs e) {
|
|
210
|
+
base.OnResize(e);
|
|
211
|
+
using (var path = new GraphicsPath()) {
|
|
212
|
+
path.AddEllipse(0, 0, Width, Height);
|
|
213
|
+
var old = Region;
|
|
214
|
+
Region = new Region(path);
|
|
215
|
+
if (old != null) { old.Dispose(); }
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
protected override void OnPaint(PaintEventArgs e) {
|
|
220
|
+
var g = e.Graphics;
|
|
221
|
+
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
222
|
+
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
223
|
+
using (var path = new GraphicsPath()) {
|
|
224
|
+
path.AddEllipse(0, 0, Width - 1, Height - 1);
|
|
225
|
+
using (var brush = new SolidBrush(hover ? Color.White : Color.FromArgb(246, 248, 250))) {
|
|
226
|
+
g.FillPath(brush, path);
|
|
227
|
+
}
|
|
228
|
+
using (var pen = new Pen(Color.FromArgb(206, 212, 219), 1f)) {
|
|
229
|
+
g.DrawPath(pen, path);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
var ink = Color.FromArgb(38, 43, 51);
|
|
233
|
+
using (var pen = new Pen(ink, 1.6f)) {
|
|
234
|
+
pen.LineJoin = LineJoin.Round;
|
|
235
|
+
using (var body = new GraphicsPath()) {
|
|
236
|
+
body.AddArc(11, 16, 6, 6, 180, 90);
|
|
237
|
+
body.AddArc(31, 16, 6, 6, 270, 90);
|
|
238
|
+
body.AddArc(31, 30, 6, 6, 0, 90);
|
|
239
|
+
body.AddArc(11, 30, 6, 6, 90, 90);
|
|
240
|
+
body.CloseFigure();
|
|
241
|
+
g.DrawPath(pen, body);
|
|
242
|
+
}
|
|
243
|
+
g.DrawLines(pen, new PointF[] { new PointF(18f, 16f), new PointF(19.6f, 12.6f), new PointF(28.4f, 12.6f), new PointF(30f, 16f) });
|
|
244
|
+
g.DrawEllipse(pen, 19.4f, 19.4f, 9.2f, 9.2f);
|
|
245
|
+
using (var dot = new SolidBrush(ink)) { g.FillEllipse(dot, 23.2f, 23.2f, 2.4f, 2.4f); }
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
protected override void OnMouseDown(MouseEventArgs e) {
|
|
250
|
+
if (e.Button == MouseButtons.Left) {
|
|
251
|
+
dragging = true;
|
|
252
|
+
moved = false;
|
|
253
|
+
grabCursor = Cursor.Position;
|
|
254
|
+
grabOrigin = Location;
|
|
255
|
+
SetCapture(Handle);
|
|
256
|
+
}
|
|
257
|
+
base.OnMouseDown(e);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
protected override void OnMouseMove(MouseEventArgs e) {
|
|
261
|
+
if (dragging) {
|
|
262
|
+
var now = Cursor.Position;
|
|
263
|
+
var dx = now.X - grabCursor.X;
|
|
264
|
+
var dy = now.Y - grabCursor.Y;
|
|
265
|
+
if (!moved && (Math.Abs(dx) > 4 || Math.Abs(dy) > 4)) { moved = true; }
|
|
266
|
+
if (moved) { Location = new Point(grabOrigin.X + dx, grabOrigin.Y + dy); }
|
|
267
|
+
}
|
|
268
|
+
base.OnMouseMove(e);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
protected override void OnMouseUp(MouseEventArgs e) {
|
|
272
|
+
if (e.Button == MouseButtons.Left && dragging) {
|
|
273
|
+
dragging = false;
|
|
274
|
+
ReleaseCapture();
|
|
275
|
+
if (moved) { DshFloat.SavePos(Location); } else { Fire(); }
|
|
276
|
+
}
|
|
277
|
+
if (e.Button == MouseButtons.Right) {
|
|
278
|
+
try { popup.Show(Cursor.Position); } catch { }
|
|
279
|
+
}
|
|
280
|
+
base.OnMouseUp(e);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
protected override void OnMouseEnter(EventArgs e) { hover = true; Invalidate(); base.OnMouseEnter(e); }
|
|
284
|
+
protected override void OnMouseLeave(EventArgs e) { hover = false; Invalidate(); base.OnMouseLeave(e); }
|
|
285
|
+
|
|
286
|
+
// One click, one gesture: the host turns this line into a capture on the
|
|
287
|
+
// same path as the hotkey, so both entries behave identically downstream.
|
|
288
|
+
private void Fire() {
|
|
289
|
+
try { Console.Out.WriteLine("FLOAT"); Console.Out.Flush(); } catch { }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private void HideByUser() {
|
|
293
|
+
UserVisible = false;
|
|
294
|
+
Hide();
|
|
295
|
+
WriteFlag(false);
|
|
296
|
+
try { Console.Out.WriteLine("FLOATSTATE|off"); Console.Out.Flush(); } catch { }
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
public static void Start(bool visible) {
|
|
301
|
+
if (ball != null) { SetVisible(visible); return; }
|
|
302
|
+
var temp = Path.GetTempPath();
|
|
303
|
+
posFile = Path.Combine(temp, "dsh-screenshot-float-pos.txt");
|
|
304
|
+
flagFile = Path.Combine(temp, "dsh-screenshot-float.txt");
|
|
305
|
+
var form = new Ball();
|
|
306
|
+
form.UserVisible = visible;
|
|
307
|
+
form.Location = LoadPos(form.Size);
|
|
308
|
+
ball = form;
|
|
309
|
+
Last = "pending";
|
|
310
|
+
var runner = new System.Threading.Thread(delegate() {
|
|
311
|
+
try {
|
|
312
|
+
form.Handle.ToString();
|
|
313
|
+
Last = "on";
|
|
314
|
+
if (!form.UserVisible) { form.Hide(); }
|
|
315
|
+
try { Console.Out.WriteLine("FLOATSTATE|ok|on"); Console.Out.Flush(); } catch { }
|
|
316
|
+
Application.Run(form);
|
|
317
|
+
} catch (Exception error) {
|
|
318
|
+
Last = "error:" + error.GetType().Name;
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
runner.IsBackground = true;
|
|
322
|
+
runner.SetApartmentState(System.Threading.ApartmentState.STA);
|
|
323
|
+
thread = runner;
|
|
324
|
+
runner.Start();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
public static void SetVisible(bool visible) {
|
|
328
|
+
var form = ball;
|
|
329
|
+
Last = visible ? "on" : "off";
|
|
330
|
+
if (form == null) { return; }
|
|
331
|
+
try {
|
|
332
|
+
form.Invoke(new Action(delegate() {
|
|
333
|
+
form.UserVisible = visible;
|
|
334
|
+
if (visible) {
|
|
335
|
+
if (!suspended) { form.Show(); form.TopMost = true; }
|
|
336
|
+
} else {
|
|
337
|
+
form.Hide();
|
|
338
|
+
}
|
|
339
|
+
WriteFlag(visible);
|
|
340
|
+
}));
|
|
341
|
+
} catch { }
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// The ball floats above everything, so it steps out of the frame for the grab
|
|
345
|
+
// and comes back afterwards - unless the user hid it on purpose.
|
|
346
|
+
public static void Suspend() {
|
|
347
|
+
var form = ball;
|
|
348
|
+
if (form == null || suspended) { return; }
|
|
349
|
+
suspended = true;
|
|
350
|
+
try { form.Invoke(new Action(delegate() { if (form.UserVisible) { form.Hide(); } })); } catch { }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
public static void Resume() {
|
|
354
|
+
var form = ball;
|
|
355
|
+
if (form == null || !suspended) { return; }
|
|
356
|
+
suspended = false;
|
|
357
|
+
try { form.Invoke(new Action(delegate() { if (form.UserVisible) { form.Show(); form.TopMost = true; } })); } catch { }
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
public static void Stop() {
|
|
361
|
+
var form = ball;
|
|
362
|
+
var runner = thread;
|
|
363
|
+
ball = null;
|
|
364
|
+
thread = null;
|
|
365
|
+
suspended = false;
|
|
366
|
+
Last = "off";
|
|
367
|
+
if (form == null) { return; }
|
|
368
|
+
try { form.Invoke(new Action(delegate() { Application.ExitThread(); })); } catch { }
|
|
369
|
+
try { if (runner != null) { runner.Join(500); } } catch { }
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private static Point LoadPos(Size size) {
|
|
373
|
+
var area = Screen.PrimaryScreen.WorkingArea;
|
|
374
|
+
var fallback = new Point(area.Right - 48 - 40, area.Bottom - 48 - 120);
|
|
375
|
+
try {
|
|
376
|
+
if (File.Exists(posFile)) {
|
|
377
|
+
var parts = File.ReadAllText(posFile).Trim().Split(',');
|
|
378
|
+
int x, y;
|
|
379
|
+
if (parts.Length == 2 && int.TryParse(parts[0], out x) && int.TryParse(parts[1], out y)) {
|
|
380
|
+
return Clamp(new Point(x, y), size);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
} catch { }
|
|
384
|
+
return fallback;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private static Point Clamp(Point p, Size size) {
|
|
388
|
+
var area = SystemInformation.VirtualScreen;
|
|
389
|
+
var x = Math.Max(area.Left, Math.Min(p.X, area.Right - size.Width));
|
|
390
|
+
var y = Math.Max(area.Top, Math.Min(p.Y, area.Bottom - size.Height));
|
|
391
|
+
return new Point(x, y);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
internal static void SavePos(Point p) {
|
|
395
|
+
try { File.WriteAllText(posFile, p.X + "," + p.Y); } catch { }
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
private static void WriteFlag(bool visible) {
|
|
399
|
+
try { if (flagFile != null) { File.WriteAllText(flagFile, visible ? "on" : "off"); } } catch { }
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
'@ -ErrorAction Stop
|
|
403
|
+
} catch { }
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
# Bind (or unbind) the global hotkey from a spec such as 'alt+shift+s'.
|
|
407
|
+
# The outcome also lands in a temp file, so a binding that quietly fails (usually
|
|
408
|
+
# another app already owning the combo) is visible without a console attached.
|
|
409
|
+
$script:HotkeyStateFile = Join-Path ([IO.Path]::GetTempPath()) 'dsh-screenshot-hotkey.txt'
|
|
410
|
+
function Write-HotkeyState([string]$Spec, [string]$State) {
|
|
411
|
+
try { [IO.File]::WriteAllText($script:HotkeyStateFile, "spec=$Spec state=$State`n") } catch { }
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function Set-Hotkey {
|
|
415
|
+
param([string]$Spec = 'off')
|
|
416
|
+
try {
|
|
417
|
+
if ([string]::IsNullOrWhiteSpace($Spec) -or $Spec.Trim() -eq 'off') {
|
|
418
|
+
[DshHotkey]::Stop()
|
|
419
|
+
Write-HotkeyState $Spec 'off'
|
|
420
|
+
return
|
|
421
|
+
}
|
|
422
|
+
$mods = 0
|
|
423
|
+
$vk = 0
|
|
424
|
+
foreach ($part in $Spec.ToLowerInvariant().Split('+')) {
|
|
425
|
+
switch ($part.Trim()) {
|
|
426
|
+
'ctrl' { $mods = $mods -bor 2 }
|
|
427
|
+
'control' { $mods = $mods -bor 2 }
|
|
428
|
+
'alt' { $mods = $mods -bor 1 }
|
|
429
|
+
'shift' { $mods = $mods -bor 4 }
|
|
430
|
+
'win' { $mods = $mods -bor 8 }
|
|
431
|
+
default { if ($part.Trim().Length -eq 1) { $vk = [int][char]$part.Trim().ToUpperInvariant() } }
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if ($vk -eq 0 -or $mods -eq 0) {
|
|
435
|
+
[DshHotkey]::Stop()
|
|
436
|
+
Write-HotkeyState $Spec 'invalid'
|
|
437
|
+
return
|
|
438
|
+
}
|
|
439
|
+
[void][DshHotkey]::Start($mods, $vk)
|
|
440
|
+
# Registration happens on the pump thread a moment later; wait for a verdict.
|
|
441
|
+
for ($i = 0; $i -lt 20; $i++) {
|
|
442
|
+
if ([DshHotkey]::Last -ne 'pending') { break }
|
|
443
|
+
Start-Sleep -Milliseconds 25
|
|
444
|
+
}
|
|
445
|
+
Write-HotkeyState $Spec ([DshHotkey]::Last)
|
|
446
|
+
} catch {
|
|
447
|
+
Write-HotkeyState $Spec ('exception:' + $_.Exception.GetType().Name)
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
# Show or hide the floating ball. The outcome lands in a temp file for the same
|
|
452
|
+
# reason the hotkey does: a helper has no console anyone can read.
|
|
453
|
+
$script:FloatStateFile = Join-Path ([IO.Path]::GetTempPath()) 'dsh-screenshot-float-state.txt'
|
|
454
|
+
function Write-FloatState([string]$Mode, [string]$Detail = '') {
|
|
455
|
+
try { [IO.File]::WriteAllText($script:FloatStateFile, "float=$Mode $Detail`n") } catch { }
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function Set-Float {
|
|
459
|
+
param([string]$Mode = 'on')
|
|
460
|
+
try {
|
|
461
|
+
if ([string]::IsNullOrWhiteSpace($Mode) -or $Mode.Trim() -eq 'off') {
|
|
462
|
+
[DshFloat]::SetVisible($false)
|
|
463
|
+
Write-FloatState 'off'
|
|
464
|
+
return
|
|
465
|
+
}
|
|
466
|
+
[DshFloat]::Start($true)
|
|
467
|
+
[DshFloat]::SetVisible($true)
|
|
468
|
+
Write-FloatState 'on' ([DshFloat]::Last)
|
|
469
|
+
} catch {
|
|
470
|
+
Write-FloatState 'exception' ('msg=' + $_.Exception.Message)
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
# Window P/Invoke used to enumerate and filter top-level windows for hover
|
|
476
|
+
# selection. Hit testing runs against the cached z-order list; the overlay form
|
|
477
|
+
# is never moved or hidden at runtime.
|
|
478
|
+
try {
|
|
479
|
+
Add-Type -TypeDefinition @'
|
|
480
|
+
using System;
|
|
481
|
+
using System.Text;
|
|
482
|
+
using System.Runtime.InteropServices;
|
|
483
|
+
public struct DshRect { public int Left; public int Top; public int Right; public int Bottom; }
|
|
484
|
+
public struct DshPoint { public int X; public int Y; }
|
|
485
|
+
public delegate bool DshEnumProc(IntPtr hWnd, IntPtr lParam);
|
|
486
|
+
public static class DshWin {
|
|
487
|
+
[DllImport("user32.dll")] public static extern bool EnumWindows(DshEnumProc lpEnumFunc, IntPtr lParam);
|
|
488
|
+
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out DshRect lpRect);
|
|
489
|
+
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
|
|
490
|
+
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd);
|
|
491
|
+
[DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
|
492
|
+
[DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
|
493
|
+
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
|
494
|
+
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(DshPoint p);
|
|
495
|
+
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
|
|
496
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
497
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
|
|
498
|
+
[DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();
|
|
499
|
+
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
|
500
|
+
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
501
|
+
[DllImport("user32.dll")] public static extern bool BringWindowToTop(IntPtr hWnd);
|
|
502
|
+
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
503
|
+
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hWnd);
|
|
504
|
+
|
|
505
|
+
// Raise a window even though the foreground lock would normally refuse it -
|
|
506
|
+
// this runs while the overlay still owns the foreground, so borrowing the
|
|
507
|
+
// foreground thread's input queue for the call is enough.
|
|
508
|
+
public static bool Activate(IntPtr hwnd) {
|
|
509
|
+
if (hwnd == IntPtr.Zero) return false;
|
|
510
|
+
IntPtr foreground = GetForegroundWindow();
|
|
511
|
+
if (foreground == hwnd) return true;
|
|
512
|
+
uint foregroundThread = GetWindowThreadProcessId(foreground, IntPtr.Zero);
|
|
513
|
+
uint self = GetCurrentThreadId();
|
|
514
|
+
bool attached = false;
|
|
515
|
+
try {
|
|
516
|
+
if (foregroundThread != 0 && foregroundThread != self) {
|
|
517
|
+
attached = AttachThreadInput(self, foregroundThread, true);
|
|
518
|
+
}
|
|
519
|
+
if (IsIconic(hwnd)) {
|
|
520
|
+
ShowWindow(hwnd, 9);
|
|
521
|
+
} else if (!IsWindowVisible(hwnd)) {
|
|
522
|
+
// Hidden to the tray: a foreground call alone would do nothing.
|
|
523
|
+
ShowWindow(hwnd, 5);
|
|
524
|
+
ShowWindow(hwnd, 8);
|
|
525
|
+
}
|
|
526
|
+
BringWindowToTop(hwnd);
|
|
527
|
+
return SetForegroundWindow(hwnd);
|
|
528
|
+
} finally {
|
|
529
|
+
if (attached) { AttachThreadInput(self, foregroundThread, false); }
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
'@ -ErrorAction Stop
|
|
534
|
+
} catch { }
|
|
535
|
+
|
|
536
|
+
# Confirming a capture should leave the user looking at DSH, not at whatever app
|
|
537
|
+
# happened to be in front when the hotkey fired. The host window is located via
|
|
538
|
+
# the parent pid the plugin started us with, falling back to any visible DSH
|
|
539
|
+
# window when the parent turns out to be a windowless helper process.
|
|
540
|
+
function Find-HostWindow {
|
|
541
|
+
if ($ParentPid -gt 0) {
|
|
542
|
+
try {
|
|
543
|
+
$handle = [System.Diagnostics.Process]::GetProcessById($ParentPid).MainWindowHandle
|
|
544
|
+
if ($handle -ne [IntPtr]::Zero) { return $handle }
|
|
545
|
+
} catch { }
|
|
546
|
+
}
|
|
547
|
+
# Last resort: the host window carries the product name in its title, and
|
|
548
|
+
# enumerating top-level windows is not subject to the per-process access rules
|
|
549
|
+
# that make MainWindowHandle unreliable from a helper.
|
|
550
|
+
try {
|
|
551
|
+
$script:probeHwnd = [IntPtr]::Zero
|
|
552
|
+
$probe = {
|
|
553
|
+
param($hwnd, $unused)
|
|
554
|
+
$length = [DshWin]::GetWindowTextLength($hwnd)
|
|
555
|
+
if ($length -lt 6) { return $true }
|
|
556
|
+
$builder = New-Object System.Text.StringBuilder ($length + 2)
|
|
557
|
+
[void][DshWin]::GetWindowText($hwnd, $builder, $builder.Capacity)
|
|
558
|
+
if ($builder.ToString() -like '*DeepSeek Harness*') {
|
|
559
|
+
$script:probeHwnd = $hwnd
|
|
560
|
+
return $false
|
|
561
|
+
}
|
|
562
|
+
return $true
|
|
563
|
+
}
|
|
564
|
+
[void][DshWin]::EnumWindows([DshEnumProc]$probe, [IntPtr]::Zero)
|
|
565
|
+
if ($script:probeHwnd -ne [IntPtr]::Zero) { return $script:probeHwnd }
|
|
566
|
+
} catch { }
|
|
567
|
+
return [IntPtr]::Zero
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function Activate-HostWindow {
|
|
571
|
+
$report = 'none'
|
|
572
|
+
try {
|
|
573
|
+
$handle = Find-HostWindow
|
|
574
|
+
$ok = [DshWin]::Activate($handle)
|
|
575
|
+
$report = "hwnd=$handle ok=$ok parent=$ParentPid procs=$(@(Get-Process -Name 'DeepSeek Harness' -ErrorAction SilentlyContinue).Count)"
|
|
576
|
+
} catch {
|
|
577
|
+
$report = 'exception:' + $_.Exception.Message
|
|
578
|
+
}
|
|
579
|
+
# Kept on disk because the overlay is closing and nobody is watching stdout.
|
|
580
|
+
try {
|
|
581
|
+
[IO.File]::WriteAllText((Join-Path ([IO.Path]::GetTempPath()) 'dsh-screenshot-activate.txt'), $report + "`n")
|
|
582
|
+
} catch { }
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
# The window you capture from must not be part of the picture, so it steps aside
|
|
586
|
+
# for the grab and comes back when the overlay is done - the way capture tools
|
|
587
|
+
# behave. Hidden/minimised hosts are left alone: they are already out of frame.
|
|
588
|
+
function Hide-HostWindow {
|
|
589
|
+
# The ball floats above everything, so it would be inside the shot as well.
|
|
590
|
+
try { [DshFloat]::Suspend() } catch { }
|
|
591
|
+
try {
|
|
592
|
+
$handle = Find-HostWindow
|
|
593
|
+
if ($handle -eq [IntPtr]::Zero) { return }
|
|
594
|
+
$script:hostWindow = $handle
|
|
595
|
+
if (-not [DshWin]::IsWindowVisible($handle)) { return }
|
|
596
|
+
if ([DshWin]::IsIconic($handle)) { return }
|
|
597
|
+
[void][DshWin]::ShowWindow($handle, 0)
|
|
598
|
+
$script:hostHidden = $true
|
|
599
|
+
# Give the desktop a beat to repaint where the window used to be.
|
|
600
|
+
Start-Sleep -Milliseconds 220
|
|
601
|
+
} catch { }
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function Restore-HostWindow {
|
|
605
|
+
try { [DshFloat]::Resume() } catch { }
|
|
606
|
+
if (-not $script:hostHidden) { return }
|
|
607
|
+
$script:hostHidden = $false
|
|
608
|
+
try {
|
|
609
|
+
$handle = $script:hostWindow
|
|
610
|
+
if ($handle -eq [IntPtr]::Zero) { $handle = Find-HostWindow }
|
|
611
|
+
if ($handle -ne [IntPtr]::Zero -and -not [DshWin]::IsWindowVisible($handle)) {
|
|
612
|
+
[void][DshWin]::ShowWindow($handle, 5)
|
|
613
|
+
}
|
|
614
|
+
} catch { }
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
$script:hostWindow = [IntPtr]::Zero
|
|
618
|
+
$script:hostHidden = $false
|
|
619
|
+
|
|
620
|
+
# This file is saved as UTF-8 with BOM on purpose: Windows PowerShell 5.1 decodes
|
|
621
|
+
# BOM-less scripts as ANSI (GBK on zh-CN) and would mangle every literal below.
|
|
622
|
+
|
|
623
|
+
# Toolbar labels, the wording this capture-bar family uses.
|
|
624
|
+
$script:labelCopy = '复制'
|
|
625
|
+
$script:labelCopied = '已复制'
|
|
626
|
+
$script:labelCancel = '取消'
|
|
627
|
+
$script:labelInsert = '插入对话'
|
|
628
|
+
|
|
629
|
+
# --- Annotation ----------------------------------------------------------
|
|
630
|
+
# Drawing happens over the frozen frame before it becomes an attachment: one
|
|
631
|
+
# transparent, screen-sized layer holds every stroke in screen coordinates, so
|
|
632
|
+
# the region can still be dragged and resized underneath the marks. 'move' is
|
|
633
|
+
# the default tool - a fresh region stays draggable by its body, exactly as
|
|
634
|
+
# before; picking a drawing tool takes the body drag over until it is cleared.
|
|
635
|
+
$script:tool = 'move'
|
|
636
|
+
$script:penColors = @(
|
|
637
|
+
[System.Drawing.Color]::FromArgb(255, 224, 59, 59),
|
|
638
|
+
[System.Drawing.Color]::FromArgb(255, 245, 197, 66),
|
|
639
|
+
[System.Drawing.Color]::FromArgb(255, 59, 125, 216),
|
|
640
|
+
[System.Drawing.Color]::FromArgb(255, 31, 35, 41),
|
|
641
|
+
[System.Drawing.Color]::FromArgb(255, 255, 255, 255)
|
|
642
|
+
)
|
|
643
|
+
$script:penColorIndex = 0
|
|
644
|
+
# Stroke weight and mosaic coarseness cycle on right-click, so the bar keeps one
|
|
645
|
+
# button per tool instead of six extra pickers.
|
|
646
|
+
$script:penWidths = @(2, 3, 6)
|
|
647
|
+
$script:penWidthIndex = 1
|
|
648
|
+
$script:mosaicBlocks = @(5, 8, 14)
|
|
649
|
+
$script:mosaicIndex = 1
|
|
650
|
+
$script:widthLabels = @('细', '中', '粗')
|
|
651
|
+
# Step numbering: each click drops the next integer on the region.
|
|
652
|
+
$script:numberNext = 1
|
|
653
|
+
# Each stroke: @{ Kind; Color; Width; Points; Rect }. A mosaic keeps its rect
|
|
654
|
+
# and re-samples the frozen frame on every replay instead of storing pixels.
|
|
655
|
+
$script:strokes = New-Object System.Collections.ArrayList
|
|
656
|
+
$script:inkBmp = $null
|
|
657
|
+
$script:inkG = $null
|
|
658
|
+
$script:drawing = $false
|
|
659
|
+
$script:drawStart = New-Object System.Drawing.Point(0, 0)
|
|
660
|
+
$script:drawEnd = New-Object System.Drawing.Point(0, 0)
|
|
661
|
+
$script:drawPoints = New-Object System.Collections.ArrayList
|
|
662
|
+
|
|
663
|
+
function Get-PenColor {
|
|
664
|
+
return $script:penColors[$script:penColorIndex]
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
function Get-PenWidth {
|
|
668
|
+
return [single][Math]::Max(2, (S $script:penWidths[$script:penWidthIndex]))
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function Get-MosaicBlock {
|
|
672
|
+
return [int][Math]::Max(3, (S $script:mosaicBlocks[$script:mosaicIndex]))
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
# Hover text carries the live档位, which is how right-click stays discoverable.
|
|
676
|
+
function Get-ToolLabel {
|
|
677
|
+
param([string]$key)
|
|
678
|
+
if ($key -eq 'pen') { return ('画笔 · ' + $script:widthLabels[$script:penWidthIndex] + '(右键切)') }
|
|
679
|
+
if ($key -eq 'mosaic') { return ('马赛克 · ' + $script:widthLabels[$script:mosaicIndex] + '(右键切)') }
|
|
680
|
+
if ($script:toolLabels.ContainsKey($key)) { return $script:toolLabels[$key] }
|
|
681
|
+
return $key
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
# Rebuild the transparent layer: every stroke replays in screen coordinates,
|
|
685
|
+
# which is what undo needs after a stroke has already been painted into it.
|
|
686
|
+
function Reset-Annotation {
|
|
687
|
+
if ($null -ne $script:textBox) { Commit-TextEditor $false }
|
|
688
|
+
$script:strokes = New-Object System.Collections.ArrayList
|
|
689
|
+
$script:numberNext = 1
|
|
690
|
+
$script:drawing = $false
|
|
691
|
+
$script:drawStart = New-Object System.Drawing.Point(0, 0)
|
|
692
|
+
$script:drawEnd = New-Object System.Drawing.Point(0, 0)
|
|
693
|
+
$script:drawPoints = New-Object System.Collections.ArrayList
|
|
694
|
+
if ($null -ne $script:inkG) { try { $script:inkG.Dispose() } catch { } }
|
|
695
|
+
if ($null -ne $script:inkBmp) { try { $script:inkBmp.Dispose() } catch { } }
|
|
696
|
+
$script:inkBmp = New-Object System.Drawing.Bitmap($script:vs.Width, $script:vs.Height, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb)
|
|
697
|
+
$script:inkG = [System.Drawing.Graphics]::FromImage($script:inkBmp)
|
|
698
|
+
$script:inkG.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
|
|
699
|
+
$script:inkG.Clear([System.Drawing.Color]::Transparent)
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
# Painting resources live for the whole process: a resident helper pays their
|
|
703
|
+
# construction once instead of per click. Fonts are declared in pixels rather
|
|
704
|
+
# than points so the chrome keeps its measured size at any display scaling.
|
|
705
|
+
$script:chipFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 15, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
|
|
706
|
+
$script:labelFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 14, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
|
|
707
|
+
# Digits inside the step badges: semi-bold so a "6" still reads at 24px diameter.
|
|
708
|
+
$script:numFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 14, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Pixel)
|
|
709
|
+
# Free text labels: slightly larger than the chip text so a comment reads at a glance.
|
|
710
|
+
$script:textFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 16, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
|
|
711
|
+
$script:textBox = $null
|
|
712
|
+
$script:textPoint = New-Object System.Drawing.Point(0, 0)
|
|
713
|
+
|
|
714
|
+
# Chrome is authored in logical pixels and painted in physical ones: the desktop
|
|
715
|
+
# here is physical-pixel space, so every size below runs through S() using the
|
|
716
|
+
# display scale measured when the overlay opens (150% -> 1.5).
|
|
717
|
+
$script:scale = 1.0
|
|
718
|
+
function S {
|
|
719
|
+
param([double]$v)
|
|
720
|
+
return [int][Math]::Round($v * $script:scale)
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
# Palette: near-black ink on white cards, hairline #E5E6EB boundaries.
|
|
724
|
+
$script:inkColor = [System.Drawing.Color]::FromArgb(255, 31, 35, 41)
|
|
725
|
+
$script:inkBrush = New-Object System.Drawing.SolidBrush($script:inkColor)
|
|
726
|
+
$script:whiteBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
|
|
727
|
+
$script:shadowBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(30, 0, 0, 0))
|
|
728
|
+
$script:hoverBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 242, 243, 245))
|
|
729
|
+
# Selected tool: a pale tint of the brand blue, flat like the other items.
|
|
730
|
+
$script:toolActiveBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 235, 242, 252))
|
|
731
|
+
# The unselected desktop is knocked back by half, the usual capture-tool treatment.
|
|
732
|
+
$script:dimBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(128, 0, 0, 0))
|
|
733
|
+
$script:chipBorderPen = New-Object System.Drawing.Pen([System.Drawing.Color]::FromArgb(255, 229, 230, 235), 1)
|
|
734
|
+
$script:dividerPen = New-Object System.Drawing.Pen([System.Drawing.Color]::FromArgb(255, 221, 224, 230), 1)
|
|
735
|
+
$script:iconPen = New-Object System.Drawing.Pen($script:inkColor, 1.4)
|
|
736
|
+
|
|
737
|
+
# Selection outline: dark dashes around the region, the rest of the desktop
|
|
738
|
+
# dimmed underneath.
|
|
739
|
+
$script:accentPen = New-Object System.Drawing.Pen($script:inkColor, 1)
|
|
740
|
+
$script:accentPen.DashStyle = [System.Drawing.Drawing2D.DashStyle]::Dash
|
|
741
|
+
$script:accentPen.DashPattern = @([single]4, [single]3)
|
|
742
|
+
|
|
743
|
+
# Round handles: white fill, hairline outline.
|
|
744
|
+
$script:handleBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
|
|
745
|
+
$script:handlePen = New-Object System.Drawing.Pen([System.Drawing.Color]::FromArgb(255, 68, 72, 80), 1)
|
|
746
|
+
|
|
747
|
+
# Re-derive every scaled resource for the display the overlay landed on.
|
|
748
|
+
function Set-UiScale {
|
|
749
|
+
param([double]$factor)
|
|
750
|
+
if ($factor -lt 1) { $factor = 1 }
|
|
751
|
+
$script:scale = $factor
|
|
752
|
+
if ($null -ne $script:chipFont) { try { $script:chipFont.Dispose() } catch { } }
|
|
753
|
+
if ($null -ne $script:labelFont) { try { $script:labelFont.Dispose() } catch { } }
|
|
754
|
+
if ($null -ne $script:numFont) { try { $script:numFont.Dispose() } catch { } }
|
|
755
|
+
if ($null -ne $script:textFont) { try { $script:textFont.Dispose() } catch { } }
|
|
756
|
+
$script:chipFont = New-Object System.Drawing.Font('Microsoft YaHei UI', ([single](15 * $factor)), [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
|
|
757
|
+
$script:labelFont = New-Object System.Drawing.Font('Microsoft YaHei UI', ([single](14 * $factor)), [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
|
|
758
|
+
$script:numFont = New-Object System.Drawing.Font('Microsoft YaHei UI', ([single](14 * $factor)), [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Pixel)
|
|
759
|
+
$script:textFont = New-Object System.Drawing.Font('Microsoft YaHei UI', ([single](16 * $factor)), [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel)
|
|
760
|
+
$script:accentPen.Width = [single][Math]::Max(1, [Math]::Round(1.4 * $factor))
|
|
761
|
+
$script:accentPen.DashPattern = @([single]2.5, [single]2.5)
|
|
762
|
+
$script:iconPen.Width = [single](1.4 * $factor)
|
|
763
|
+
$script:handlePen.Width = [single][Math]::Max(1, [Math]::Round(0.7 * $factor))
|
|
764
|
+
$script:chipBorderPen.Width = 1
|
|
765
|
+
$script:dividerPen.Width = 1
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
$script:centerFormat = New-Object System.Drawing.StringFormat
|
|
769
|
+
$script:centerFormat.Alignment = [System.Drawing.StringAlignment]::Center
|
|
770
|
+
$script:centerFormat.LineAlignment = [System.Drawing.StringAlignment]::Center
|
|
771
|
+
$script:centerFormat.FormatFlags = [System.Drawing.StringFormatFlags]::NoWrap
|
|
772
|
+
|
|
773
|
+
# Classes that are shell / IME / widget backdrops, never a capture target.
|
|
774
|
+
$excludeClasses = @(
|
|
775
|
+
'Progman', 'WorkerW', 'Shell_TrayWnd', 'Shell_SecondaryTrayWnd',
|
|
776
|
+
'IPHlpAPI', 'MsgWnd', 'CEF-OSC-WIDGET', 'Windows.UI.Core.CoreWindow',
|
|
777
|
+
'Intermediate D3D Window', 'Chrome_RenderWidgetHostHWND', 'CiceroUIWndFrame'
|
|
778
|
+
)
|
|
779
|
+
|
|
780
|
+
# Per-capture state. Everything is $script: scoped because the form handlers are
|
|
781
|
+
# scriptblocks that run after this frame has left the caller's local scope.
|
|
782
|
+
$script:vs = $null
|
|
783
|
+
$script:bmp = $null
|
|
784
|
+
$script:measureG = $null
|
|
785
|
+
$script:copiedAt = 0
|
|
786
|
+
$script:form = $null
|
|
787
|
+
$script:overlayHwnd = [IntPtr]::Zero
|
|
788
|
+
$script:windows = New-Object System.Collections.ArrayList
|
|
789
|
+
$script:tick = 0
|
|
790
|
+
$script:timeout = 120
|
|
791
|
+
$script:parentPid = $ParentPid
|
|
792
|
+
|
|
793
|
+
function Reset-GestureState {
|
|
794
|
+
$script:sel = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
795
|
+
$script:origin = New-Object System.Drawing.Point(0, 0)
|
|
796
|
+
$script:pressed = $false
|
|
797
|
+
$script:dragged = $false
|
|
798
|
+
$script:downWindow = $null
|
|
799
|
+
$script:committed = $false
|
|
800
|
+
# idle | selecting | adjust
|
|
801
|
+
$script:phase = 'idle'
|
|
802
|
+
# none | window | drag
|
|
803
|
+
$script:mode = 'none'
|
|
804
|
+
$script:hoverTitle = ''
|
|
805
|
+
# drag inside the selection, or a handle
|
|
806
|
+
$script:adjustMode = ''
|
|
807
|
+
$script:resizeEdge = ''
|
|
808
|
+
$script:resizeStart = New-Object System.Drawing.Point(0, 0)
|
|
809
|
+
$script:resizeStartRect = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
810
|
+
$script:moveStart = New-Object System.Drawing.Point(0, 0)
|
|
811
|
+
$script:moveStartRect = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
812
|
+
$script:hoverButton = ''
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function Test-ParentAlive {
|
|
816
|
+
if ($script:parentPid -le 0) { return $true }
|
|
817
|
+
try {
|
|
818
|
+
$null = [System.Diagnostics.Process]::GetProcessById($script:parentPid)
|
|
819
|
+
return $true
|
|
820
|
+
} catch {
|
|
821
|
+
return $false
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
function Test-FilteredWindow {
|
|
826
|
+
param([IntPtr]$hWnd)
|
|
827
|
+
if ($hWnd -eq $script:overlayHwnd) { return $true }
|
|
828
|
+
if (-not [DshWin]::IsWindowVisible($hWnd)) { return $true }
|
|
829
|
+
|
|
830
|
+
$r = New-Object DshRect
|
|
831
|
+
if (-not [DshWin]::GetWindowRect($hWnd, [ref]$r)) { return $true }
|
|
832
|
+
$w = $r.Right - $r.Left; $h = $r.Bottom - $r.Top
|
|
833
|
+
if ($w -lt 40 -or $h -lt 30) { return $true }
|
|
834
|
+
|
|
835
|
+
$cls = New-Object System.Text.StringBuilder(256)
|
|
836
|
+
[void][DshWin]::GetClassName($hWnd, $cls, 256)
|
|
837
|
+
if ($excludeClasses -contains $cls.ToString()) { return $true }
|
|
838
|
+
|
|
839
|
+
# Tool windows (IME host, popups, floating widgets) and click-through
|
|
840
|
+
# transparent windows swallow the hover hit-test but are not real targets.
|
|
841
|
+
$exStyle = [DshWin]::GetWindowLong($hWnd, -20)
|
|
842
|
+
if (($exStyle -band 0x80) -ne 0) { return $true }
|
|
843
|
+
if (($exStyle -band 0x20) -ne 0) { return $true }
|
|
844
|
+
|
|
845
|
+
$len = [DshWin]::GetWindowTextLength($hWnd)
|
|
846
|
+
if ($len -le 0) { return $true }
|
|
847
|
+
return $false
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
function Get-WindowEntry {
|
|
851
|
+
param([IntPtr]$hWnd)
|
|
852
|
+
$r = New-Object DshRect
|
|
853
|
+
[void][DshWin]::GetWindowRect($hWnd, [ref]$r)
|
|
854
|
+
$len = [DshWin]::GetWindowTextLength($hWnd)
|
|
855
|
+
$title = New-Object System.Text.StringBuilder(($len + 1))
|
|
856
|
+
[void][DshWin]::GetWindowText($hWnd, $title, ($len + 1))
|
|
857
|
+
return [pscustomobject]@{ Hwnd = $hWnd; Rect = $r; Title = $title.ToString() }
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
$script:enumProc = [DshEnumProc]{
|
|
861
|
+
param($hWnd, $lParam)
|
|
862
|
+
if (Test-FilteredWindow $hWnd) { return $true }
|
|
863
|
+
[void]$script:windows.Add((Get-WindowEntry $hWnd))
|
|
864
|
+
return $true
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
# Hit test against the cached z-order list. We deliberately do NOT move or hide
|
|
868
|
+
# the overlay to call WindowFromPoint: toggling Bounds / visibility pumps a
|
|
869
|
+
# repaint on every mouse move and makes the overlay flash.
|
|
870
|
+
function Resolve-Window {
|
|
871
|
+
param([int]$px, [int]$py)
|
|
872
|
+
$vs = $script:vs
|
|
873
|
+
foreach ($w in $script:windows) {
|
|
874
|
+
$r = $w.Rect
|
|
875
|
+
$lx = $r.Left - $vs.X; $ly = $r.Top - $vs.Y
|
|
876
|
+
$rx = $r.Right - $vs.X; $ry = $r.Bottom - $vs.Y
|
|
877
|
+
if ($px -ge $lx -and $px -le $rx -and $py -ge $ly -and $py -le $ry) {
|
|
878
|
+
return $w
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return $null
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function Set-WindowSelection {
|
|
885
|
+
param($w)
|
|
886
|
+
$vs = $script:vs
|
|
887
|
+
$r = $w.Rect
|
|
888
|
+
$script:sel = New-Object System.Drawing.Rectangle(
|
|
889
|
+
($r.Left - $vs.X), ($r.Top - $vs.Y),
|
|
890
|
+
($r.Right - $r.Left), ($r.Bottom - $r.Top))
|
|
891
|
+
$script:mode = 'window'
|
|
892
|
+
$script:hoverTitle = $w.Title
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
function Get-HandlePoints {
|
|
896
|
+
param($r)
|
|
897
|
+
$mx = $r.X + [int]($r.Width / 2)
|
|
898
|
+
$my = $r.Y + [int]($r.Height / 2)
|
|
899
|
+
$rt = $r.X + $r.Width
|
|
900
|
+
$bt = $r.Y + $r.Height
|
|
901
|
+
return @{
|
|
902
|
+
nw = @($r.X, $r.Y); n = @($mx, $r.Y); ne = @($rt, $r.Y)
|
|
903
|
+
w = @($r.X, $my); e = @($rt, $my)
|
|
904
|
+
sw = @($r.X, $bt); s = @($mx, $bt); se = @($rt, $bt)
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
# Handles are hit-tested by geometry rather than stored rects, so they keep
|
|
909
|
+
# working while the selection is being resized. Written without allocations:
|
|
910
|
+
# this runs on every mouse move.
|
|
911
|
+
function Get-HandleAt {
|
|
912
|
+
param([int]$px, [int]$py)
|
|
913
|
+
$sel = $script:sel
|
|
914
|
+
if ($sel.Width -lt 6 -or $sel.Height -lt 6) { return '' }
|
|
915
|
+
$l = $sel.X; $t = $sel.Y
|
|
916
|
+
$r = $sel.X + $sel.Width; $b = $sel.Y + $sel.Height
|
|
917
|
+
$mx = $l + [int]($sel.Width / 2); $my = $t + [int]($sel.Height / 2)
|
|
918
|
+
$d = S 6
|
|
919
|
+
if ([Math]::Abs($px - $l) -le $d -and [Math]::Abs($py - $t) -le $d) { return 'nw' }
|
|
920
|
+
if ([Math]::Abs($px - $r) -le $d -and [Math]::Abs($py - $t) -le $d) { return 'ne' }
|
|
921
|
+
if ([Math]::Abs($px - $l) -le $d -and [Math]::Abs($py - $b) -le $d) { return 'sw' }
|
|
922
|
+
if ([Math]::Abs($px - $r) -le $d -and [Math]::Abs($py - $b) -le $d) { return 'se' }
|
|
923
|
+
if ([Math]::Abs($px - $mx) -le $d -and [Math]::Abs($py - $t) -le $d) { return 'n' }
|
|
924
|
+
if ([Math]::Abs($px - $mx) -le $d -and [Math]::Abs($py - $b) -le $d) { return 's' }
|
|
925
|
+
if ([Math]::Abs($px - $l) -le $d -and [Math]::Abs($py - $my) -le $d) { return 'w' }
|
|
926
|
+
if ([Math]::Abs($px - $r) -le $d -and [Math]::Abs($py - $my) -le $d) { return 'e' }
|
|
927
|
+
return ''
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
# Toolbar geometry: a white rounded strip under
|
|
931
|
+
# the selection - [pen][rect][arrow][number][text][mosaic][undo][color] | [复制][取消] | [插入对话].
|
|
932
|
+
function Get-ToolbarLayout {
|
|
933
|
+
param($r)
|
|
934
|
+
$barH = S 42
|
|
935
|
+
$padOuter = S 8
|
|
936
|
+
$padItem = S 12
|
|
937
|
+
$iconSize = S 14
|
|
938
|
+
$iconGap = S 6
|
|
939
|
+
$divPad = S 10
|
|
940
|
+
$btn = S 30
|
|
941
|
+
$btnGap = S 4
|
|
942
|
+
$toolNames = @('pen', 'rect', 'arrow', 'number', 'text', 'mosaic', 'undo', 'color')
|
|
943
|
+
|
|
944
|
+
$mg = $script:measureG
|
|
945
|
+
$twCopy = [int][Math]::Ceiling($mg.MeasureString($script:labelCopy, $script:labelFont).Width)
|
|
946
|
+
$twCancel = [int][Math]::Ceiling($mg.MeasureString($script:labelCancel, $script:labelFont).Width)
|
|
947
|
+
$twInsert = [int][Math]::Ceiling($mg.MeasureString($script:labelInsert, $script:labelFont).Width)
|
|
948
|
+
|
|
949
|
+
$wCopy = $padItem * 2 + $iconSize + $iconGap + $twCopy
|
|
950
|
+
$wCancel = $padItem * 2 + $iconSize + $iconGap + $twCancel
|
|
951
|
+
$wInsert = $padItem * 2 + $iconSize + $iconGap + $twInsert
|
|
952
|
+
$wDiv = 1 + $divPad * 2
|
|
953
|
+
$wTools = $btn * $toolNames.Count + $btnGap * ($toolNames.Count - 1)
|
|
954
|
+
|
|
955
|
+
$barW = $padOuter * 2 + $wTools + $wDiv + $wCopy + $wCancel + $wDiv + $wInsert
|
|
956
|
+
$x = $r.X
|
|
957
|
+
$gap = S 11
|
|
958
|
+
$margin = S 4
|
|
959
|
+
if (($x + $barW) -gt ($script:vs.Width - $margin)) { $x = $script:vs.Width - $margin - $barW }
|
|
960
|
+
if ($x -lt $margin) { $x = $margin }
|
|
961
|
+
$y = $r.Y + $r.Height + $gap
|
|
962
|
+
if (($y + $barH) -gt ($script:vs.Height - $margin)) { $y = $r.Y - $barH - $gap }
|
|
963
|
+
if ($y -lt $margin) { $y = $margin }
|
|
964
|
+
|
|
965
|
+
$cx = $x + $padOuter
|
|
966
|
+
$ty = $y + [int](($barH - $btn) / 2)
|
|
967
|
+
$tools = @{}
|
|
968
|
+
foreach ($name in $toolNames) {
|
|
969
|
+
$tools[$name] = New-Object System.Drawing.Rectangle($cx, $ty, $btn, $btn)
|
|
970
|
+
$cx += $btn + $btnGap
|
|
971
|
+
}
|
|
972
|
+
$cx -= $btnGap
|
|
973
|
+
$cx += $divPad
|
|
974
|
+
$divider = New-Object System.Drawing.Rectangle($cx, ($y + (S 12)), 1, ($barH - (S 24)))
|
|
975
|
+
$cx += 1 + $divPad
|
|
976
|
+
$copy = New-Object System.Drawing.Rectangle($cx, $y, $wCopy, $barH)
|
|
977
|
+
$cx += $wCopy
|
|
978
|
+
$cancel = New-Object System.Drawing.Rectangle($cx, $y, $wCancel, $barH)
|
|
979
|
+
$cx += $wCancel + $divPad
|
|
980
|
+
$divider2 = New-Object System.Drawing.Rectangle($cx, ($y + (S 12)), 1, ($barH - (S 24)))
|
|
981
|
+
$cx += 1 + $divPad
|
|
982
|
+
$insert = New-Object System.Drawing.Rectangle($cx, $y, $wInsert, $barH)
|
|
983
|
+
|
|
984
|
+
return @{
|
|
985
|
+
bar = (New-Object System.Drawing.Rectangle($x, $y, $barW, $barH))
|
|
986
|
+
tools = $tools
|
|
987
|
+
copy = $copy
|
|
988
|
+
cancel = $cancel
|
|
989
|
+
divider = $divider
|
|
990
|
+
divider2 = $divider2
|
|
991
|
+
insert = $insert
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
$script:toolNames = @('pen', 'rect', 'arrow', 'number', 'text', 'mosaic', 'undo', 'color')
|
|
996
|
+
$script:toolLabels = @{ pen = '画笔'; rect = '方框'; arrow = '箭头'; number = '序号'; text = '文字'; mosaic = '马赛克'; undo = '撤销'; color = '换色' }
|
|
997
|
+
|
|
998
|
+
# Hover hint above the tool: a white card with the tool's name, because the
|
|
999
|
+
# glyphs at 16px cannot carry their meaning on their own.
|
|
1000
|
+
function Draw-ToolTip {
|
|
1001
|
+
param($gg, $rect, [string]$text)
|
|
1002
|
+
$ts = $gg.MeasureString($text, $script:labelFont)
|
|
1003
|
+
$padX = S 8; $padY = S 5
|
|
1004
|
+
$w = [int][Math]::Ceiling($ts.Width + $padX * 2)
|
|
1005
|
+
$h = [int][Math]::Ceiling($ts.Height + $padY * 2)
|
|
1006
|
+
$x = [int]($rect.X + ($rect.Width / 2)) - [int]($w / 2)
|
|
1007
|
+
$y = $rect.Y - $h - (S 6)
|
|
1008
|
+
$margin = S 4
|
|
1009
|
+
if ($y -lt $margin) { $y = $rect.Y + $rect.Height + (S 6) }
|
|
1010
|
+
if ($x -lt $margin) { $x = $margin }
|
|
1011
|
+
if (($x + $w) -gt ($script:vs.Width - $margin)) { $x = $script:vs.Width - $margin - $w }
|
|
1012
|
+
$box = New-Object System.Drawing.Rectangle($x, $y, $w, $h)
|
|
1013
|
+
Draw-Card $gg $box (S 6)
|
|
1014
|
+
$tf = New-Object System.Drawing.RectangleF(($x + $padX), ($y + $padY), ($w - $padX * 2), ($h - $padY * 2))
|
|
1015
|
+
$gg.DrawString($text, $script:labelFont, $script:inkBrush, $tf, $script:centerFormat)
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
# --- Text annotation ----------------------------------------------------
|
|
1019
|
+
# Clicking with the text tool drops a real TextBox onto the overlay instead of a
|
|
1020
|
+
# modal dialog: the overlay is borderless and topmost, so a second window would
|
|
1021
|
+
# steal focus and hide the selection while you type.
|
|
1022
|
+
function Show-TextEditor {
|
|
1023
|
+
param($owner, [int]$x, [int]$y)
|
|
1024
|
+
if ($null -ne $script:textBox) { return }
|
|
1025
|
+
$tb = New-Object System.Windows.Forms.TextBox
|
|
1026
|
+
$tb.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
|
|
1027
|
+
$tb.Font = $script:textFont
|
|
1028
|
+
$tb.Width = (S 230)
|
|
1029
|
+
$tb.BackColor = [System.Drawing.Color]::White
|
|
1030
|
+
$tb.ForeColor = $script:inkColor
|
|
1031
|
+
$h = [int]$tb.PreferredHeight
|
|
1032
|
+
$top = $y - [int]($h / 2)
|
|
1033
|
+
$script:textPoint = New-Object System.Drawing.Point($x, $top)
|
|
1034
|
+
$tb.Location = New-Object System.Drawing.Point($x, $top)
|
|
1035
|
+
$script:textBox = $tb
|
|
1036
|
+
$owner.Controls.Add($tb)
|
|
1037
|
+
$tb.Focus()
|
|
1038
|
+
$tb.Add_KeyDown({
|
|
1039
|
+
param($sender, $ev)
|
|
1040
|
+
if ($ev.KeyCode -eq [System.Windows.Forms.Keys]::Enter) {
|
|
1041
|
+
Commit-TextEditor $true
|
|
1042
|
+
$ev.SuppressKeyPress = $true
|
|
1043
|
+
} elseif ($ev.KeyCode -eq [System.Windows.Forms.Keys]::Escape) {
|
|
1044
|
+
Commit-TextEditor $false
|
|
1045
|
+
$ev.SuppressKeyPress = $true
|
|
1046
|
+
}
|
|
1047
|
+
})
|
|
1048
|
+
# Clicking elsewhere drops the box; keep whatever was typed.
|
|
1049
|
+
$tb.Add_LostFocus({ param($sender, $ev) if ($null -ne $script:textBox) { Commit-TextEditor $true } })
|
|
1050
|
+
$owner.Invalidate()
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
function Commit-TextEditor {
|
|
1054
|
+
param([bool]$keep)
|
|
1055
|
+
$tb = $script:textBox
|
|
1056
|
+
if ($null -eq $tb) { return }
|
|
1057
|
+
$script:textBox = $null
|
|
1058
|
+
$text = ''
|
|
1059
|
+
if ($keep) {
|
|
1060
|
+
$raw = $tb.Text
|
|
1061
|
+
if ($null -ne $raw) { $text = $raw.Trim() }
|
|
1062
|
+
}
|
|
1063
|
+
$pt = $script:textPoint
|
|
1064
|
+
if ($null -ne $script:form) {
|
|
1065
|
+
try { $script:form.Controls.Remove($tb) } catch { }
|
|
1066
|
+
}
|
|
1067
|
+
try { $tb.Dispose() } catch { }
|
|
1068
|
+
if ($text.Length -gt 0 -and $null -ne $script:inkG) {
|
|
1069
|
+
$stroke = @{ Kind = 'text'; Color = (Get-PenColor); Width = (Get-PenWidth); Text = $text; Point = $pt }
|
|
1070
|
+
[void]$script:strokes.Add($stroke)
|
|
1071
|
+
Draw-Stroke $script:inkG $stroke 0 0
|
|
1072
|
+
}
|
|
1073
|
+
if ($null -ne $script:form) { try { $script:form.Activate() } catch { }; $script:form.Invalidate() }
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
function Get-ButtonAt {
|
|
1077
|
+
param([int]$px, [int]$py)
|
|
1078
|
+
if ($script:phase -ne 'adjust') { return '' }
|
|
1079
|
+
if ($null -eq $script:measureG) { return '' }
|
|
1080
|
+
$layout = Get-ToolbarLayout $script:sel
|
|
1081
|
+
foreach ($name in $script:toolNames) {
|
|
1082
|
+
if ($layout['tools'][$name].Contains($px, $py)) { return 'tool:' + $name }
|
|
1083
|
+
}
|
|
1084
|
+
if ($layout['copy'].Contains($px, $py)) { return 'copy' }
|
|
1085
|
+
if ($layout['cancel'].Contains($px, $py)) { return 'cancel' }
|
|
1086
|
+
if ($layout['insert'].Contains($px, $py)) { return 'insert' }
|
|
1087
|
+
return ''
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
function Set-SelectionFromEdges {
|
|
1091
|
+
param([int]$left, [int]$top, [int]$right, [int]$bottom)
|
|
1092
|
+
if ($left -gt $right) { $t = $left; $left = $right; $right = $t }
|
|
1093
|
+
if ($top -gt $bottom) { $t = $top; $top = $bottom; $bottom = $t }
|
|
1094
|
+
if ($left -lt 0) { $left = 0 }
|
|
1095
|
+
if ($top -lt 0) { $top = 0 }
|
|
1096
|
+
if ($right -gt $script:vs.Width) { $right = $script:vs.Width }
|
|
1097
|
+
if ($bottom -gt $script:vs.Height) { $bottom = $script:vs.Height }
|
|
1098
|
+
$script:sel = New-Object System.Drawing.Rectangle($left, $top, ($right - $left), ($bottom - $top))
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
# --- Drawing primitives --------------------------------------------------
|
|
1102
|
+
function Get-RoundRectPath {
|
|
1103
|
+
param($rect, [single]$radius)
|
|
1104
|
+
$path = New-Object System.Drawing.Drawing2D.GraphicsPath
|
|
1105
|
+
$d = [single]($radius * 2)
|
|
1106
|
+
if ($d -gt $rect.Width) { $d = [single]$rect.Width }
|
|
1107
|
+
if ($d -gt $rect.Height) { $d = [single]$rect.Height }
|
|
1108
|
+
$x = [single]$rect.X; $y = [single]$rect.Y
|
|
1109
|
+
$w = [single]$rect.Width; $h = [single]$rect.Height
|
|
1110
|
+
if ($d -le 2) {
|
|
1111
|
+
$path.AddRectangle((New-Object System.Drawing.RectangleF($x, $y, $w, $h)))
|
|
1112
|
+
return $path
|
|
1113
|
+
}
|
|
1114
|
+
$path.AddArc($x, $y, $d, $d, 180, 90)
|
|
1115
|
+
$path.AddArc(($x + $w - $d), $y, $d, $d, 270, 90)
|
|
1116
|
+
$path.AddArc(($x + $w - $d), ($y + $h - $d), $d, $d, 0, 90)
|
|
1117
|
+
$path.AddArc($x, ($y + $h - $d), $d, $d, 90, 90)
|
|
1118
|
+
$path.CloseFigure()
|
|
1119
|
+
return $path
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
function Fill-RoundedRect {
|
|
1123
|
+
param($gg, $rect, [single]$radius, $brush)
|
|
1124
|
+
$path = Get-RoundRectPath $rect $radius
|
|
1125
|
+
try { $gg.FillPath($brush, $path) } finally { $path.Dispose() }
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
# White card, hairline border, faint drop shadow. Used by the size chip and the
|
|
1129
|
+
# toolbar so both read as the same surface family.
|
|
1130
|
+
function Draw-Card {
|
|
1131
|
+
param($gg, $rect, [single]$radius)
|
|
1132
|
+
$sh = New-Object System.Drawing.Rectangle(($rect.X - (S 2)), ($rect.Y + (S 1)), ($rect.Width + (S 4)), ($rect.Height + (S 5)))
|
|
1133
|
+
Fill-RoundedRect $gg $sh ($radius + (S 2)) $script:shadowBrush
|
|
1134
|
+
Fill-RoundedRect $gg $rect $radius $script:whiteBrush
|
|
1135
|
+
$path = Get-RoundRectPath $rect $radius
|
|
1136
|
+
try { $gg.DrawPath($script:chipBorderPen, $path) } finally { $path.Dispose() }
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
function Draw-IconCopy {
|
|
1140
|
+
param($gg, [single]$x, [single]$y, [single]$s, $pen)
|
|
1141
|
+
$half = [single]($s * 0.6)
|
|
1142
|
+
$off = [single]($s * 0.4)
|
|
1143
|
+
# Two rounded sheets, the front one offset down-left.
|
|
1144
|
+
$back = New-Object System.Drawing.Rectangle([int]($x + $off), [int]$y, [int]$half, [int]$half)
|
|
1145
|
+
$front = New-Object System.Drawing.Rectangle([int]$x, [int]($y + $off), [int]$half, [int]$half)
|
|
1146
|
+
foreach ($r in @($back, $front)) {
|
|
1147
|
+
$path = Get-RoundRectPath $r 1.5
|
|
1148
|
+
try { $gg.DrawPath($pen, $path) } finally { $path.Dispose() }
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
function Draw-IconCheck {
|
|
1153
|
+
param($gg, [single]$x, [single]$y, [single]$s, $pen)
|
|
1154
|
+
$pts = New-Object 'System.Drawing.PointF[]' 3
|
|
1155
|
+
$pts[0] = New-Object System.Drawing.PointF(($x + $s * 0.14), ($y + $s * 0.54))
|
|
1156
|
+
$pts[1] = New-Object System.Drawing.PointF(($x + $s * 0.4), ($y + $s * 0.8))
|
|
1157
|
+
$pts[2] = New-Object System.Drawing.PointF(($x + $s * 0.86), ($y + $s * 0.2))
|
|
1158
|
+
$gg.DrawLines($pen, $pts)
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
function Draw-IconX {
|
|
1162
|
+
param($gg, [single]$x, [single]$y, [single]$s, $pen)
|
|
1163
|
+
$gg.DrawLine($pen, ($x + $s * 0.2), ($y + $s * 0.2), ($x + $s * 0.8), ($y + $s * 0.8))
|
|
1164
|
+
$gg.DrawLine($pen, ($x + $s * 0.8), ($y + $s * 0.2), ($x + $s * 0.2), ($y + $s * 0.8))
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
# Icon plus label, centred as one group inside the item rect.
|
|
1168
|
+
function Draw-ItemContent {
|
|
1169
|
+
param($gg, $rect, [string]$label, [string]$icon, $pen, $brush, $font)
|
|
1170
|
+
$iconSize = S 14
|
|
1171
|
+
$iconGap = S 6
|
|
1172
|
+
$ts = $gg.MeasureString($label, $font)
|
|
1173
|
+
$total = $iconSize + $iconGap + $ts.Width
|
|
1174
|
+
$ix = [single]($rect.X + ($rect.Width - $total) / 2)
|
|
1175
|
+
$iy = [single]($rect.Y + ($rect.Height - $iconSize) / 2)
|
|
1176
|
+
if ($icon -eq 'copy') { Draw-IconCopy $gg $ix $iy $iconSize $pen }
|
|
1177
|
+
elseif ($icon -eq 'check') { Draw-IconCheck $gg $ix $iy $iconSize $pen }
|
|
1178
|
+
elseif ($icon -eq 'x') { Draw-IconX $gg $ix $iy $iconSize $pen }
|
|
1179
|
+
$ty = [single]($rect.Y + ($rect.Height - $ts.Height) / 2)
|
|
1180
|
+
$gg.DrawString($label, $font, $brush, (New-Object System.Drawing.PointF(($ix + $iconSize + $iconGap), $ty)))
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
# --- Stroke replay -------------------------------------------------------
|
|
1184
|
+
# Every stroke lives in screen coordinates; ox/oy shift it into the target
|
|
1185
|
+
# surface (0,0 when painting the screen-sized layer, -sel.X,-sel.Y when
|
|
1186
|
+
# compositing a crop).
|
|
1187
|
+
function Draw-MosaicBlock {
|
|
1188
|
+
param($gg, $rect, [int]$ox, [int]$oy, [int]$block)
|
|
1189
|
+
if ($rect.Width -lt 2 -or $rect.Height -lt 2) { return }
|
|
1190
|
+
$cols = [Math]::Max(1, [int][Math]::Ceiling($rect.Width / $block))
|
|
1191
|
+
$rows = [Math]::Max(1, [int][Math]::Ceiling($rect.Height / $block))
|
|
1192
|
+
if ($cols -gt 400 -or $rows -gt 400) { return }
|
|
1193
|
+
$small = New-Object System.Drawing.Bitmap($cols, $rows)
|
|
1194
|
+
$sg = [System.Drawing.Graphics]::FromImage($small)
|
|
1195
|
+
try {
|
|
1196
|
+
$sg.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBilinear
|
|
1197
|
+
$dest = New-Object System.Drawing.Rectangle(0, 0, $cols, $rows)
|
|
1198
|
+
$src = New-Object System.Drawing.Rectangle($rect.X, $rect.Y, $rect.Width, $rect.Height)
|
|
1199
|
+
$sg.DrawImage($script:bmp, $dest, $src, [System.Drawing.GraphicsUnit]::Pixel)
|
|
1200
|
+
} finally { $sg.Dispose() }
|
|
1201
|
+
try {
|
|
1202
|
+
$prev = $gg.InterpolationMode
|
|
1203
|
+
$gg.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::NearestNeighbor
|
|
1204
|
+
$outRect = New-Object System.Drawing.Rectangle(($rect.X - $ox), ($rect.Y - $oy), $rect.Width, $rect.Height)
|
|
1205
|
+
$gg.DrawImage($small, $outRect)
|
|
1206
|
+
$gg.InterpolationMode = $prev
|
|
1207
|
+
} finally { $small.Dispose() }
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function Draw-Stroke {
|
|
1211
|
+
param($gg, $stroke, [int]$ox = 0, [int]$oy = 0)
|
|
1212
|
+
if ($null -eq $stroke) { return }
|
|
1213
|
+
if ($stroke.Kind -eq 'mosaic') {
|
|
1214
|
+
$block = (S 8)
|
|
1215
|
+
if ($stroke.ContainsKey('Block')) { $block = $stroke.Block }
|
|
1216
|
+
Draw-MosaicBlock $gg $stroke.Rect $ox $oy $block
|
|
1217
|
+
return
|
|
1218
|
+
}
|
|
1219
|
+
if ($stroke.Kind -eq 'number') {
|
|
1220
|
+
# A filled disc with the step number knocked out of it: solid enough to read
|
|
1221
|
+
# over any screenshot, and the digit flips to dark on a light fill.
|
|
1222
|
+
$p = $stroke.Point
|
|
1223
|
+
$rad = [single](S 12)
|
|
1224
|
+
$cx = [single]($p.X - $ox); $cy = [single]($p.Y - $oy)
|
|
1225
|
+
$brush = New-Object System.Drawing.SolidBrush($stroke.Color)
|
|
1226
|
+
try { $gg.FillEllipse($brush, ($cx - $rad), ($cy - $rad), ($rad * 2), ($rad * 2)) } finally { $brush.Dispose() }
|
|
1227
|
+
$lum = ($stroke.Color.R * 0.299) + ($stroke.Color.G * 0.587) + ($stroke.Color.B * 0.114)
|
|
1228
|
+
$tb = $script:whiteBrush
|
|
1229
|
+
if ($lum -ge 150) { $tb = $script:inkBrush }
|
|
1230
|
+
$box = New-Object System.Drawing.RectangleF(($cx - $rad), ($cy - $rad), ($rad * 2), ($rad * 2))
|
|
1231
|
+
$gg.DrawString([string]$stroke.Num, $script:numFont, $tb, $box, $script:centerFormat)
|
|
1232
|
+
return
|
|
1233
|
+
}
|
|
1234
|
+
if ($stroke.Kind -eq 'text') {
|
|
1235
|
+
# Outlined text: the glyph outline is widened into a separate white path and
|
|
1236
|
+
# filled first, then the ink on top. Four offset DrawString passes left the
|
|
1237
|
+
# strokes muddy at 16px, especially on CJK glyphs.
|
|
1238
|
+
if ([string]::IsNullOrWhiteSpace($stroke.Text)) { return }
|
|
1239
|
+
$p = $stroke.Point
|
|
1240
|
+
$x = [single]($p.X - $ox); $y = [single]($p.Y - $oy)
|
|
1241
|
+
$fam = New-Object System.Drawing.FontFamily('Microsoft YaHei UI')
|
|
1242
|
+
$fmt = New-Object System.Drawing.StringFormat([System.Drawing.StringFormat]::GenericTypographic)
|
|
1243
|
+
$fp = New-Object System.Drawing.Drawing2D.GraphicsPath
|
|
1244
|
+
$wide = $null
|
|
1245
|
+
$haloPen = $null
|
|
1246
|
+
try {
|
|
1247
|
+
# AddString sizes in points; the font is declared in pixels, so scale by 0.75.
|
|
1248
|
+
$fp.AddString($stroke.Text, $fam, [int][System.Drawing.FontStyle]::Regular, [single]($script:textFont.Size * 0.75), (New-Object System.Drawing.PointF($x, $y)), $fmt)
|
|
1249
|
+
$wide = $fp.Clone()
|
|
1250
|
+
$haloPen = New-Object System.Drawing.Pen([System.Drawing.Color]::White, [single][Math]::Max(1.5, (S 2)))
|
|
1251
|
+
$wide.Widen($haloPen)
|
|
1252
|
+
$gg.FillPath($script:whiteBrush, $wide)
|
|
1253
|
+
$brush = New-Object System.Drawing.SolidBrush($stroke.Color)
|
|
1254
|
+
try { $gg.FillPath($brush, $fp) } finally { $brush.Dispose() }
|
|
1255
|
+
} finally {
|
|
1256
|
+
if ($null -ne $haloPen) { $haloPen.Dispose() }
|
|
1257
|
+
if ($null -ne $wide) { $wide.Dispose() }
|
|
1258
|
+
$fp.Dispose(); $fam.Dispose(); $fmt.Dispose()
|
|
1259
|
+
}
|
|
1260
|
+
return
|
|
1261
|
+
}
|
|
1262
|
+
$pen = New-Object System.Drawing.Pen($stroke.Color, [single]$stroke.Width)
|
|
1263
|
+
try {
|
|
1264
|
+
$pen.LineJoin = [System.Drawing.Drawing2D.LineJoin]::Round
|
|
1265
|
+
$pen.StartCap = [System.Drawing.Drawing2D.LineCap]::Round
|
|
1266
|
+
$pen.EndCap = [System.Drawing.Drawing2D.LineCap]::Round
|
|
1267
|
+
if ($stroke.Kind -eq 'pen') {
|
|
1268
|
+
$pts = $stroke.Points
|
|
1269
|
+
if ($pts.Count -eq 1) {
|
|
1270
|
+
$brush = New-Object System.Drawing.SolidBrush($stroke.Color)
|
|
1271
|
+
try {
|
|
1272
|
+
$rad = [single]($stroke.Width / 2)
|
|
1273
|
+
$gg.FillEllipse($brush, [single]($pts[0].X - $ox - $rad), [single]($pts[0].Y - $oy - $rad), [single]$stroke.Width, [single]$stroke.Width)
|
|
1274
|
+
} finally { $brush.Dispose() }
|
|
1275
|
+
return
|
|
1276
|
+
}
|
|
1277
|
+
$arr = New-Object 'System.Drawing.PointF[]' $pts.Count
|
|
1278
|
+
for ($i = 0; $i -lt $pts.Count; $i++) {
|
|
1279
|
+
$arr[$i] = New-Object System.Drawing.PointF([single]($pts[$i].X - $ox), [single]($pts[$i].Y - $oy))
|
|
1280
|
+
}
|
|
1281
|
+
$gg.DrawLines($pen, $arr)
|
|
1282
|
+
return
|
|
1283
|
+
}
|
|
1284
|
+
if ($stroke.Kind -eq 'rect') {
|
|
1285
|
+
$r = $stroke.Rect
|
|
1286
|
+
$gg.DrawRectangle($pen, ($r.X - $ox), ($r.Y - $oy), $r.Width, $r.Height)
|
|
1287
|
+
return
|
|
1288
|
+
}
|
|
1289
|
+
if ($stroke.Kind -eq 'arrow') {
|
|
1290
|
+
$a = $stroke.Points[0]; $b = $stroke.Points[1]
|
|
1291
|
+
$x1 = [single]($a.X - $ox); $y1 = [single]($a.Y - $oy)
|
|
1292
|
+
$x2 = [single]($b.X - $ox); $y2 = [single]($b.Y - $oy)
|
|
1293
|
+
$gg.DrawLine($pen, $x1, $y1, $x2, $y2)
|
|
1294
|
+
$dx = $x2 - $x1; $dy = $y2 - $y1
|
|
1295
|
+
$len = [Math]::Sqrt(($dx * $dx) + ($dy * $dy))
|
|
1296
|
+
if ($len -gt 4) {
|
|
1297
|
+
$ux = $dx / $len; $uy = $dy / $len
|
|
1298
|
+
$head = [Math]::Min([single](S 16), [single]($len * 0.5))
|
|
1299
|
+
$ca = [Math]::Cos(0.45); $sa = [Math]::Sin(0.45)
|
|
1300
|
+
$h1x = $x2 - (($ux * $ca) - ($uy * $sa)) * $head
|
|
1301
|
+
$h1y = $y2 - (($uy * $ca) + ($ux * $sa)) * $head
|
|
1302
|
+
$h2x = $x2 - (($ux * $ca) + ($uy * $sa)) * $head
|
|
1303
|
+
$h2y = $y2 - (($uy * $ca) - ($ux * $sa)) * $head
|
|
1304
|
+
$gg.DrawLine($pen, $x2, $y2, $h1x, $h1y)
|
|
1305
|
+
$gg.DrawLine($pen, $x2, $y2, $h2x, $h2y)
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
} finally { $pen.Dispose() }
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
# Icon glyphs for the tool half of the bar, drawn in the same 1.4px ink stroke
|
|
1312
|
+
# as the copy / cancel / insert icons so the strip reads as one family.
|
|
1313
|
+
function Draw-ToolIcon {
|
|
1314
|
+
param($gg, $rect, [string]$kind)
|
|
1315
|
+
$s = [single](S 16)
|
|
1316
|
+
$x = [single]($rect.X + (($rect.Width - $s) / 2))
|
|
1317
|
+
$y = [single]($rect.Y + (($rect.Height - $s) / 2))
|
|
1318
|
+
$pen = $script:iconPen
|
|
1319
|
+
|
|
1320
|
+
if ($kind -eq 'pen') {
|
|
1321
|
+
# Pencil: a slim barrel outline and a pointed tip, drawn as two polygons
|
|
1322
|
+
# that share the tip edge so the two halves read as one object.
|
|
1323
|
+
$body = New-Object 'System.Drawing.PointF[]' 4
|
|
1324
|
+
$body[0] = New-Object System.Drawing.PointF(($x + $s * 0.74), ($y + $s * 0.1))
|
|
1325
|
+
$body[1] = New-Object System.Drawing.PointF(($x + $s * 0.9), ($y + $s * 0.26))
|
|
1326
|
+
$body[2] = New-Object System.Drawing.PointF(($x + $s * 0.4), ($y + $s * 0.76))
|
|
1327
|
+
$body[3] = New-Object System.Drawing.PointF(($x + $s * 0.24), ($y + $s * 0.6))
|
|
1328
|
+
$gg.DrawPolygon($pen, $body)
|
|
1329
|
+
$tip = New-Object 'System.Drawing.PointF[]' 3
|
|
1330
|
+
$tip[0] = New-Object System.Drawing.PointF(($x + $s * 0.24), ($y + $s * 0.6))
|
|
1331
|
+
$tip[1] = New-Object System.Drawing.PointF(($x + $s * 0.4), ($y + $s * 0.76))
|
|
1332
|
+
$tip[2] = New-Object System.Drawing.PointF(($x + $s * 0.12), ($y + $s * 0.88))
|
|
1333
|
+
$gg.DrawPolygon($pen, $tip)
|
|
1334
|
+
return
|
|
1335
|
+
}
|
|
1336
|
+
if ($kind -eq 'rect') {
|
|
1337
|
+
$gg.DrawRectangle($pen, [single]($x + $s * 0.14), [single]($y + $s * 0.24), [single]($s * 0.72), [single]($s * 0.52))
|
|
1338
|
+
return
|
|
1339
|
+
}
|
|
1340
|
+
if ($kind -eq 'arrow') {
|
|
1341
|
+
$x1 = [single]($x + $s * 0.14); $y1 = [single]($y + $s * 0.8)
|
|
1342
|
+
$x2 = [single]($x + $s * 0.84); $y2 = [single]($y + $s * 0.2)
|
|
1343
|
+
$gg.DrawLine($pen, $x1, $y1, $x2, $y2)
|
|
1344
|
+
$gg.DrawLine($pen, $x2, $y2, [single]($x2 - $s * 0.3), [single]($y2 + $s * 0.12))
|
|
1345
|
+
$gg.DrawLine($pen, $x2, $y2, [single]($x2 - $s * 0.06), [single]($y2 + $s * 0.3))
|
|
1346
|
+
return
|
|
1347
|
+
}
|
|
1348
|
+
if ($kind -eq 'number') {
|
|
1349
|
+
# A ring with a line-drawn "1" inside: reads as "step badge" at 16px without
|
|
1350
|
+
# needing a filled glyph that would fight the 1.4px stroke family.
|
|
1351
|
+
$gg.DrawEllipse($pen, [single]($x + $s * 0.15), [single]($y + $s * 0.15), [single]($s * 0.7), [single]($s * 0.7))
|
|
1352
|
+
$mid = [single]($x + $s * 0.5)
|
|
1353
|
+
$gg.DrawLine($pen, $mid, [single]($y + $s * 0.3), $mid, [single]($y + $s * 0.72))
|
|
1354
|
+
$gg.DrawLine($pen, [single]($mid - $s * 0.09), [single]($y + $s * 0.37), $mid, [single]($y + $s * 0.3))
|
|
1355
|
+
$gg.DrawLine($pen, [single]($mid - $s * 0.07), [single]($y + $s * 0.72), [single]($mid + $s * 0.07), [single]($y + $s * 0.72))
|
|
1356
|
+
return
|
|
1357
|
+
}
|
|
1358
|
+
if ($kind -eq 'text') {
|
|
1359
|
+
# A capital T: crossbar plus stem, in the same stroke weight as the others.
|
|
1360
|
+
$gg.DrawLine($pen, [single]($x + $s * 0.16), [single]($y + $s * 0.22), [single]($x + $s * 0.84), [single]($y + $s * 0.22))
|
|
1361
|
+
$gg.DrawLine($pen, [single]($x + $s * 0.5), [single]($y + $s * 0.22), [single]($x + $s * 0.5), [single]($y + $s * 0.8))
|
|
1362
|
+
return
|
|
1363
|
+
}
|
|
1364
|
+
if ($kind -eq 'mosaic') {
|
|
1365
|
+
# Two-by-two blocks packed tight with a hairline gutter, so the glyph reads
|
|
1366
|
+
# as one patch broken into pixels rather than four loose squares.
|
|
1367
|
+
$d = [single]($s * 0.34)
|
|
1368
|
+
$g = [single]($s * 0.06)
|
|
1369
|
+
$span = ($d * 2) + $g
|
|
1370
|
+
$x0 = [single]($x + (($s - $span) / 2))
|
|
1371
|
+
$y0 = [single]($y + (($s - $span) / 2))
|
|
1372
|
+
$brush = New-Object System.Drawing.SolidBrush($script:inkColor)
|
|
1373
|
+
try {
|
|
1374
|
+
$gg.FillRectangle($brush, $x0, $y0, $d, $d)
|
|
1375
|
+
$gg.FillRectangle($brush, [single]($x0 + $d + $g), $y0, $d, $d)
|
|
1376
|
+
$gg.FillRectangle($brush, $x0, [single]($y0 + $d + $g), $d, $d)
|
|
1377
|
+
$gg.FillRectangle($brush, [single]($x0 + $d + $g), [single]($y0 + $d + $g), $d, $d)
|
|
1378
|
+
} finally { $brush.Dispose() }
|
|
1379
|
+
return
|
|
1380
|
+
}
|
|
1381
|
+
if ($kind -eq 'undo') {
|
|
1382
|
+
# An open arc sweeping up from the left, with a small head at its tail.
|
|
1383
|
+
$arcR = [single]($s * 0.3)
|
|
1384
|
+
$arcL = [single]($x + $s * 0.2)
|
|
1385
|
+
$arcT = [single]($y + $s * 0.24)
|
|
1386
|
+
$gg.DrawArc($pen, $arcL, $arcT, [single]($arcR * 2), [single]($arcR * 2), 180, 200)
|
|
1387
|
+
$tailX = $arcL
|
|
1388
|
+
$tailY = [single]($arcT + $arcR)
|
|
1389
|
+
$gg.DrawLine($pen, $tailX, $tailY, [single]($tailX + $s * 0.14), [single]($tailY - $s * 0.1))
|
|
1390
|
+
$gg.DrawLine($pen, $tailX, $tailY, [single]($tailX + $s * 0.16), [single]($tailY + $s * 0.04))
|
|
1391
|
+
return
|
|
1392
|
+
}
|
|
1393
|
+
if ($kind -eq 'color') {
|
|
1394
|
+
$c = Get-PenColor
|
|
1395
|
+
$brush = New-Object System.Drawing.SolidBrush($c)
|
|
1396
|
+
try {
|
|
1397
|
+
$gg.FillEllipse($brush, [single]($x + $s * 0.1), [single]($y + $s * 0.1), [single]($s * 0.8), [single]($s * 0.8))
|
|
1398
|
+
} finally { $brush.Dispose() }
|
|
1399
|
+
$gg.DrawEllipse($script:handlePen, [single]($x + $s * 0.1), [single]($y + $s * 0.1), [single]($s * 0.8), [single]($s * 0.8))
|
|
1400
|
+
return
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
# Live stroke while the pointer is down: same painter, drawn straight onto the
|
|
1405
|
+
# overlay so the layer never has to be repainted mid-drag.
|
|
1406
|
+
function Draw-LiveStroke {
|
|
1407
|
+
param($gg)
|
|
1408
|
+
if (-not $script:drawing) { return }
|
|
1409
|
+
$kind = $script:tool
|
|
1410
|
+
$stroke = $null
|
|
1411
|
+
if ($kind -eq 'pen') {
|
|
1412
|
+
if ($script:drawPoints.Count -eq 0) { return }
|
|
1413
|
+
$stroke = @{ Kind = 'pen'; Color = (Get-PenColor); Width = (Get-PenWidth); Points = $script:drawPoints }
|
|
1414
|
+
} elseif ($kind -eq 'arrow') {
|
|
1415
|
+
$pts = New-Object System.Collections.ArrayList
|
|
1416
|
+
[void]$pts.Add($script:drawStart)
|
|
1417
|
+
[void]$pts.Add($script:drawEnd)
|
|
1418
|
+
$stroke = @{ Kind = 'arrow'; Color = (Get-PenColor); Width = (Get-PenWidth); Points = $pts }
|
|
1419
|
+
} elseif ($kind -eq 'mosaic') {
|
|
1420
|
+
$r = Get-DragRect $script:drawStart $script:drawEnd
|
|
1421
|
+
if ($r.Width -lt 2 -or $r.Height -lt 2) { return }
|
|
1422
|
+
# Preview only: the pixels get sampled once, on release.
|
|
1423
|
+
$pen = New-Object System.Drawing.Pen($script:inkColor, 1)
|
|
1424
|
+
try {
|
|
1425
|
+
$pen.DashStyle = [System.Drawing.Drawing2D.DashStyle]::Dash
|
|
1426
|
+
$gg.DrawRectangle($pen, $r.X, $r.Y, $r.Width, $r.Height)
|
|
1427
|
+
} finally { $pen.Dispose() }
|
|
1428
|
+
return
|
|
1429
|
+
} else {
|
|
1430
|
+
$r = Get-DragRect $script:drawStart $script:drawEnd
|
|
1431
|
+
if ($r.Width -lt 2 -or $r.Height -lt 2) { return }
|
|
1432
|
+
$stroke = @{ Kind = 'rect'; Color = (Get-PenColor); Width = (Get-PenWidth); Rect = $r }
|
|
1433
|
+
}
|
|
1434
|
+
Draw-Stroke $gg $stroke 0 0
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
function Get-DragRect {
|
|
1438
|
+
param($a, $b)
|
|
1439
|
+
$l = [Math]::Min($a.X, $b.X)
|
|
1440
|
+
$t = [Math]::Min($a.Y, $b.Y)
|
|
1441
|
+
$r = [Math]::Max($a.X, $b.X)
|
|
1442
|
+
$bo = [Math]::Max($a.Y, $b.Y)
|
|
1443
|
+
return (New-Object System.Drawing.Rectangle($l, $t, ($r - $l), ($bo - $t)))
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
# Commit the live stroke: append it to the replay list and ink it once, so the
|
|
1447
|
+
# per-frame paint keeps blitting a finished layer instead of re-walking paths.
|
|
1448
|
+
function Finish-Stroke {
|
|
1449
|
+
param([int]$x, [int]$y)
|
|
1450
|
+
$color = Get-PenColor
|
|
1451
|
+
$width = Get-PenWidth
|
|
1452
|
+
$stroke = $null
|
|
1453
|
+
if ($script:tool -eq 'pen') {
|
|
1454
|
+
if ($script:drawPoints.Count -eq 0) { return }
|
|
1455
|
+
$stroke = @{ Kind = 'pen'; Color = $color; Width = $width; Points = $script:drawPoints }
|
|
1456
|
+
# New list, not Clear: the stroke keeps the points it was built with.
|
|
1457
|
+
$script:drawPoints = New-Object System.Collections.ArrayList
|
|
1458
|
+
} elseif ($script:tool -eq 'arrow') {
|
|
1459
|
+
$end = New-Object System.Drawing.Point($x, $y)
|
|
1460
|
+
if ([Math]::Abs($end.X - $script:drawStart.X) -lt 3 -and [Math]::Abs($end.Y - $script:drawStart.Y) -lt 3) { return }
|
|
1461
|
+
$pts = New-Object System.Collections.ArrayList
|
|
1462
|
+
[void]$pts.Add($script:drawStart)
|
|
1463
|
+
[void]$pts.Add($end)
|
|
1464
|
+
$stroke = @{ Kind = 'arrow'; Color = $color; Width = $width; Points = $pts }
|
|
1465
|
+
} elseif ($script:tool -eq 'mosaic') {
|
|
1466
|
+
$r = Get-DragRect $script:drawStart (New-Object System.Drawing.Point($x, $y))
|
|
1467
|
+
if ($r.Width -lt 6 -or $r.Height -lt 6) { return }
|
|
1468
|
+
$stroke = @{ Kind = 'mosaic'; Color = $color; Width = $width; Rect = $r; Block = (Get-MosaicBlock) }
|
|
1469
|
+
} else {
|
|
1470
|
+
$r = Get-DragRect $script:drawStart (New-Object System.Drawing.Point($x, $y))
|
|
1471
|
+
if ($r.Width -lt 3 -or $r.Height -lt 3) { return }
|
|
1472
|
+
$stroke = @{ Kind = 'rect'; Color = $color; Width = $width; Rect = $r }
|
|
1473
|
+
}
|
|
1474
|
+
if ($null -eq $stroke -or $null -eq $script:inkG) { return }
|
|
1475
|
+
[void]$script:strokes.Add($stroke)
|
|
1476
|
+
Draw-Stroke $script:inkG $stroke 0 0
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
function Rebuild-Ink {
|
|
1480
|
+
if ($null -eq $script:inkG) { return }
|
|
1481
|
+
$script:inkG.Clear([System.Drawing.Color]::Transparent)
|
|
1482
|
+
foreach ($stroke in $script:strokes) { Draw-Stroke $script:inkG $stroke 0 0 }
|
|
1483
|
+
if ($null -ne $script:form) { $script:form.Invalidate() }
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
# White rounded size chip ("640 * 480") just outside the top-left corner.
|
|
1487
|
+
function Draw-SizeChip {
|
|
1488
|
+
param($gg, $r)
|
|
1489
|
+
$label = ('{0} * {1}' -f $r.Width, $r.Height)
|
|
1490
|
+
$ts = $gg.MeasureString($label, $script:chipFont)
|
|
1491
|
+
$padX = S 10; $padY = S 6
|
|
1492
|
+
$w = [int][Math]::Ceiling($ts.Width + $padX * 2)
|
|
1493
|
+
$h = [int][Math]::Ceiling($ts.Height + $padY * 2)
|
|
1494
|
+
$x = $r.X
|
|
1495
|
+
$offset = S 5
|
|
1496
|
+
$margin = S 4
|
|
1497
|
+
$y = $r.Y - $h - $offset
|
|
1498
|
+
# Flip inside the selection when the region is flush with the top edge, then
|
|
1499
|
+
# clamp again: a maximised window reports a small negative Y.
|
|
1500
|
+
if ($y -lt $margin) { $y = $r.Y + $offset }
|
|
1501
|
+
if ($y -lt $margin) { $y = $margin }
|
|
1502
|
+
if (($x + $w) -gt ($script:vs.Width - $margin)) { $x = $script:vs.Width - $margin - $w }
|
|
1503
|
+
if ($x -lt $margin) { $x = $margin }
|
|
1504
|
+
$rect = New-Object System.Drawing.Rectangle($x, $y, $w, $h)
|
|
1505
|
+
Draw-Card $gg $rect (S 8)
|
|
1506
|
+
$tf = New-Object System.Drawing.RectangleF(($x + $padX), ($y + $padY), ($w - $padX * 2), ($h - $padY * 2))
|
|
1507
|
+
$gg.DrawString($label, $script:chipFont, $script:inkBrush, $tf, $script:centerFormat)
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
# Eight white round handles on the corners and edge midpoints.
|
|
1511
|
+
function Draw-Handles {
|
|
1512
|
+
param($gg, $r)
|
|
1513
|
+
$points = Get-HandlePoints $r
|
|
1514
|
+
$d = S 8
|
|
1515
|
+
$rad = $d / 2
|
|
1516
|
+
foreach ($key in $points.Keys) {
|
|
1517
|
+
$p = $points[$key]
|
|
1518
|
+
$gg.FillEllipse($script:handleBrush, ($p[0] - $rad), ($p[1] - $rad), $d, $d)
|
|
1519
|
+
$gg.DrawEllipse($script:handlePen, ($p[0] - $rad), ($p[1] - $rad), $d, $d)
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
function Draw-Toolbar {
|
|
1524
|
+
param($gg, $r)
|
|
1525
|
+
$layout = Get-ToolbarLayout $r
|
|
1526
|
+
Draw-Card $gg $layout['bar'] (S 10)
|
|
1527
|
+
|
|
1528
|
+
$tools = $layout['tools']
|
|
1529
|
+
foreach ($name in $script:toolNames) {
|
|
1530
|
+
$it = $tools[$name]
|
|
1531
|
+
$inset = New-Object System.Drawing.Rectangle(($it.X + (S 2)), ($it.Y + (S 2)), ($it.Width - (S 4)), ($it.Height - (S 4)))
|
|
1532
|
+
if ($name -eq $script:tool) {
|
|
1533
|
+
Fill-RoundedRect $gg $inset (S 6) $script:toolActiveBrush
|
|
1534
|
+
} elseif ($script:hoverButton -eq ('tool:' + $name)) {
|
|
1535
|
+
Fill-RoundedRect $gg $inset (S 6) $script:hoverBrush
|
|
1536
|
+
}
|
|
1537
|
+
Draw-ToolIcon $gg $it $name
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
foreach ($key in @('copy', 'cancel', 'insert')) {
|
|
1541
|
+
if ($script:hoverButton -eq $key) {
|
|
1542
|
+
$it = $layout[$key]
|
|
1543
|
+
$hv = New-Object System.Drawing.Rectangle(($it.X + (S 2)), ($it.Y + (S 5)), ($it.Width - (S 4)), ($it.Height - (S 10)))
|
|
1544
|
+
Fill-RoundedRect $gg $hv (S 6) $script:hoverBrush
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
$copyLabel = $script:labelCopy
|
|
1549
|
+
if ($script:copiedAt -gt 0) {
|
|
1550
|
+
$age = [Environment]::TickCount - $script:copiedAt
|
|
1551
|
+
if ($age -ge 0 -and $age -lt 1500) { $copyLabel = $script:labelCopied }
|
|
1552
|
+
}
|
|
1553
|
+
Draw-ItemContent $gg $layout['copy'] $copyLabel 'copy' $script:iconPen $script:inkBrush $script:labelFont
|
|
1554
|
+
Draw-ItemContent $gg $layout['cancel'] $script:labelCancel 'x' $script:iconPen $script:inkBrush $script:labelFont
|
|
1555
|
+
|
|
1556
|
+
foreach ($dvKey in @('divider', 'divider2')) {
|
|
1557
|
+
$dv = $layout[$dvKey]
|
|
1558
|
+
$gg.DrawLine($script:dividerPen, ($dv.X + 0.5), $dv.Y, ($dv.X + 0.5), ($dv.Y + $dv.Height))
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
Draw-ItemContent $gg $layout['insert'] $script:labelInsert 'check' $script:iconPen $script:inkBrush $script:labelFont
|
|
1562
|
+
|
|
1563
|
+
if ($script:hoverButton.StartsWith('tool:')) {
|
|
1564
|
+
$hoverKey = $script:hoverButton.Substring(5)
|
|
1565
|
+
if ($script:toolLabels.ContainsKey($hoverKey)) {
|
|
1566
|
+
Draw-ToolTip $gg $tools[$hoverKey] (Get-ToolLabel $hoverKey)
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
# Crop the frozen frame and lay every stroke on top in one blit: the layer is
|
|
1572
|
+
# already screen-aligned, so the marks land exactly where they were painted.
|
|
1573
|
+
function Compose-Crop {
|
|
1574
|
+
param($rect)
|
|
1575
|
+
$crop = $script:bmp.Clone($rect, $script:bmp.PixelFormat)
|
|
1576
|
+
if ($null -eq $script:inkBmp -or $script:strokes.Count -eq 0) { return $crop }
|
|
1577
|
+
try {
|
|
1578
|
+
$cg = [System.Drawing.Graphics]::FromImage($crop)
|
|
1579
|
+
try {
|
|
1580
|
+
$cg.CompositingMode = [System.Drawing.Drawing2D.CompositingMode]::SourceOver
|
|
1581
|
+
$dest = New-Object System.Drawing.Rectangle(0, 0, $crop.Width, $crop.Height)
|
|
1582
|
+
$cg.DrawImage($script:inkBmp, $dest, $rect.X, $rect.Y, $rect.Width, $rect.Height, [System.Drawing.GraphicsUnit]::Pixel)
|
|
1583
|
+
} finally { $cg.Dispose() }
|
|
1584
|
+
return $crop
|
|
1585
|
+
} catch {
|
|
1586
|
+
return $crop
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
function Copy-SelectionToClipboard {
|
|
1591
|
+
$sel = $script:sel
|
|
1592
|
+
if ($sel.Width -lt 1 -or $sel.Height -lt 1) { return }
|
|
1593
|
+
$crop = $null
|
|
1594
|
+
try {
|
|
1595
|
+
$crop = Compose-Crop $sel
|
|
1596
|
+
[System.Windows.Forms.Clipboard]::SetImage($crop)
|
|
1597
|
+
} catch {
|
|
1598
|
+
} finally {
|
|
1599
|
+
if ($null -ne $crop) { $crop.Dispose() }
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
function Save-Crop {
|
|
1604
|
+
param($rect, [string]$OutDir)
|
|
1605
|
+
$crop = $null
|
|
1606
|
+
try {
|
|
1607
|
+
$crop = Compose-Crop $rect
|
|
1608
|
+
# Milliseconds keep back-to-back captures from overwriting each other.
|
|
1609
|
+
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss-fff'
|
|
1610
|
+
$file = Join-Path $OutDir ('shot-' + $stamp + '.png')
|
|
1611
|
+
$crop.Save($file, [System.Drawing.Imaging.ImageFormat]::Png)
|
|
1612
|
+
try { [System.Windows.Forms.Clipboard]::SetImage($crop) } catch { }
|
|
1613
|
+
return $file
|
|
1614
|
+
} finally {
|
|
1615
|
+
if ($null -ne $crop) { $crop.Dispose() }
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
# One full gesture: grab the frame, run the overlay, return what happened.
|
|
1620
|
+
# Called once per click in -Serve mode and once per process otherwise.
|
|
1621
|
+
function Invoke-OneCapture {
|
|
1622
|
+
param(
|
|
1623
|
+
[string]$OutDir = '',
|
|
1624
|
+
[int]$TimeoutSeconds = 120,
|
|
1625
|
+
[bool]$FullScreen = $false,
|
|
1626
|
+
[int]$DelaySeconds = 0
|
|
1627
|
+
)
|
|
1628
|
+
|
|
1629
|
+
# Menus, tooltips and hover panels close as soon as the button is pressed
|
|
1630
|
+
# outside them, so a delayed grab is the only way to catch them.
|
|
1631
|
+
if ($DelaySeconds -gt 0) { Start-Sleep -Seconds $DelaySeconds }
|
|
1632
|
+
|
|
1633
|
+
if ([string]::IsNullOrWhiteSpace($OutDir)) {
|
|
1634
|
+
$OutDir = Join-Path $env:TEMP 'dsh-screenshots'
|
|
1635
|
+
}
|
|
1636
|
+
if (-not (Test-Path -LiteralPath $OutDir)) {
|
|
1637
|
+
try {
|
|
1638
|
+
New-Item -ItemType Directory -Path $OutDir -Force -ErrorAction Stop | Out-Null
|
|
1639
|
+
} catch {
|
|
1640
|
+
$OutDir = Join-Path $env:TEMP ('dsh-shot-' + [guid]::NewGuid().ToString('N').Substring(0, 8))
|
|
1641
|
+
New-Item -ItemType Directory -Path $OutDir -Force | Out-Null
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
# Release the previous frame first: at 4480x1600 each full-desktop bitmap is
|
|
1646
|
+
# ~28MB, and holding two of them between clicks costs more than rebuilding.
|
|
1647
|
+
if ($null -ne $script:measureG) { try { $script:measureG.Dispose() } catch { } }
|
|
1648
|
+
if ($null -ne $script:bmp) { try { $script:bmp.Dispose() } catch { } }
|
|
1649
|
+
$script:measureG = $null
|
|
1650
|
+
$script:bmp = $null
|
|
1651
|
+
|
|
1652
|
+
# Out of the frame first, then grab.
|
|
1653
|
+
Hide-HostWindow
|
|
1654
|
+
|
|
1655
|
+
$script:vs = [System.Windows.Forms.SystemInformation]::VirtualScreen
|
|
1656
|
+
$script:bmp = New-Object System.Drawing.Bitmap($script:vs.Width, $script:vs.Height)
|
|
1657
|
+
$g = [System.Drawing.Graphics]::FromImage($script:bmp)
|
|
1658
|
+
$g.CopyFromScreen($script:vs.X, $script:vs.Y, 0, 0, (New-Object System.Drawing.Size($script:vs.Width, $script:vs.Height)))
|
|
1659
|
+
$g.Dispose()
|
|
1660
|
+
|
|
1661
|
+
# One measuring context shared by layout and painting, so the toolbar's hit
|
|
1662
|
+
# rects are exactly the rects that were drawn.
|
|
1663
|
+
$script:measureG = [System.Drawing.Graphics]::FromImage($script:bmp)
|
|
1664
|
+
|
|
1665
|
+
if ($FullScreen) {
|
|
1666
|
+
$b = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
|
|
1667
|
+
$sel = New-Object System.Drawing.Rectangle(($b.X - $script:vs.X), ($b.Y - $script:vs.Y), $b.Width, $b.Height)
|
|
1668
|
+
$file = Save-Crop $sel $OutDir
|
|
1669
|
+
Restore-HostWindow
|
|
1670
|
+
return @{ Status = 'OK'; Path = $file }
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
Reset-GestureState
|
|
1674
|
+
Reset-Annotation
|
|
1675
|
+
$script:tick = 0
|
|
1676
|
+
$script:copiedAt = 0
|
|
1677
|
+
$script:timeout = $TimeoutSeconds
|
|
1678
|
+
|
|
1679
|
+
$form = New-Object System.Windows.Forms.Form
|
|
1680
|
+
$script:form = $form
|
|
1681
|
+
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
|
|
1682
|
+
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
|
|
1683
|
+
$form.Bounds = New-Object System.Drawing.Rectangle($script:vs.X, $script:vs.Y, $script:vs.Width, $script:vs.Height)
|
|
1684
|
+
# Under per-monitor DPI awareness WinForms would re-scale the form when it spans
|
|
1685
|
+
# displays with different factors; every coordinate here is already a physical
|
|
1686
|
+
# pixel, so layout scaling must stay out of the way.
|
|
1687
|
+
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::None
|
|
1688
|
+
$form.TopMost = $true
|
|
1689
|
+
$form.ShowInTaskbar = $false
|
|
1690
|
+
$form.Cursor = [System.Windows.Forms.Cursors]::Cross
|
|
1691
|
+
$form.KeyPreview = $true
|
|
1692
|
+
$form.BackColor = [System.Drawing.Color]::Black
|
|
1693
|
+
$form.Text = 'DSH Screen Capture'
|
|
1694
|
+
|
|
1695
|
+
# DoubleBuffered and SetStyle are protected members, so plain property assignment
|
|
1696
|
+
# silently fails and the overlay flickers. Reach them through reflection instead.
|
|
1697
|
+
try {
|
|
1698
|
+
$flags = [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic
|
|
1699
|
+
[System.Windows.Forms.Form].GetProperty('DoubleBuffered', $flags).SetValue($form, $true, $null)
|
|
1700
|
+
$style = [System.Windows.Forms.ControlStyles]::AllPaintingInWmPaint -bor `
|
|
1701
|
+
[System.Windows.Forms.ControlStyles]::OptimizedDoubleBuffer -bor `
|
|
1702
|
+
[System.Windows.Forms.ControlStyles]::UserPaint
|
|
1703
|
+
[System.Windows.Forms.Control].GetMethod('SetStyle', $flags).Invoke($form, @($style, $true)) | Out-Null
|
|
1704
|
+
} catch { }
|
|
1705
|
+
|
|
1706
|
+
# Force the handle into existence so the probe can exclude it.
|
|
1707
|
+
$script:overlayHwnd = $form.Handle
|
|
1708
|
+
|
|
1709
|
+
# The chrome is authored in logical pixels; read the physical factor off the
|
|
1710
|
+
# overlay's own device context (WinForms' DeviceDpi reports 96 here under
|
|
1711
|
+
# per-monitor v2, Graphics.DpiX is the real 144).
|
|
1712
|
+
try {
|
|
1713
|
+
$probe = [System.Drawing.Graphics]::FromHwnd($script:overlayHwnd)
|
|
1714
|
+
$factor = [double]$probe.DpiX / 96.0
|
|
1715
|
+
$probe.Dispose()
|
|
1716
|
+
Set-UiScale $factor
|
|
1717
|
+
} catch {
|
|
1718
|
+
Set-UiScale 1
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
# Rebuild the target list per capture: windows open and close between clicks.
|
|
1722
|
+
# EnumWindows returns them topmost-first (z-order); after filtering shell / tool
|
|
1723
|
+
# / transparent / title-less windows, the first entry containing the cursor wins.
|
|
1724
|
+
$script:windows = New-Object System.Collections.ArrayList
|
|
1725
|
+
[void][DshWin]::EnumWindows($script:enumProc, [IntPtr]::Zero)
|
|
1726
|
+
|
|
1727
|
+
$form.Add_Paint({
|
|
1728
|
+
param($s, $e)
|
|
1729
|
+
$gg = $e.Graphics
|
|
1730
|
+
$gg.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
|
|
1731
|
+
$gg.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::ClearTypeGridFit
|
|
1732
|
+
|
|
1733
|
+
# Blit the frozen desktop, knock the whole surface back, then restore the
|
|
1734
|
+
# selected region from the same bitmap. One fill plus a clipped blit: laying
|
|
1735
|
+
# four rectangles around the hole instead leaves a faint seam row on the
|
|
1736
|
+
# shared edges, which reads as two white lines across the screen.
|
|
1737
|
+
$gg.DrawImageUnscaled($script:bmp, 0, 0)
|
|
1738
|
+
|
|
1739
|
+
$sel = $script:sel
|
|
1740
|
+
$vw = $script:vs.Width
|
|
1741
|
+
$vh = $script:vs.Height
|
|
1742
|
+
|
|
1743
|
+
$gg.FillRectangle($script:dimBrush, 0, 0, $vw, $vh)
|
|
1744
|
+
|
|
1745
|
+
if ($sel.Width -gt 1 -and $sel.Height -gt 1) {
|
|
1746
|
+
$gg.SetClip($sel)
|
|
1747
|
+
$gg.DrawImageUnscaled($script:bmp, 0, 0)
|
|
1748
|
+
# Marks ride on their own layer, clipped to the region exactly like the
|
|
1749
|
+
# region itself: whatever sticks out of it is what the crop will drop.
|
|
1750
|
+
if ($null -ne $script:inkBmp) { $gg.DrawImageUnscaled($script:inkBmp, 0, 0) }
|
|
1751
|
+
Draw-LiveStroke $gg
|
|
1752
|
+
$gg.ResetClip()
|
|
1753
|
+
|
|
1754
|
+
# The half-pixel offset keeps a 1px dash crisp instead of blurred across
|
|
1755
|
+
# two rows of pixels.
|
|
1756
|
+
$gg.DrawRectangle($script:accentPen, ($sel.X - 0.5), ($sel.Y - 0.5), ($sel.Width + 1), ($sel.Height + 1))
|
|
1757
|
+
Draw-SizeChip $gg $sel
|
|
1758
|
+
# Adjust mode: eight handles plus the action bar, so the region can be
|
|
1759
|
+
# nudged and resized before it becomes an attachment.
|
|
1760
|
+
if ($script:phase -eq 'adjust') {
|
|
1761
|
+
Draw-Handles $gg $sel
|
|
1762
|
+
Draw-Toolbar $gg $sel
|
|
1763
|
+
}
|
|
1764
|
+
return
|
|
1765
|
+
}
|
|
1766
|
+
})
|
|
1767
|
+
|
|
1768
|
+
$form.Add_MouseDown({
|
|
1769
|
+
param($s, $e)
|
|
1770
|
+
if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Right) {
|
|
1771
|
+
# Right-click on a tool cycles its setting instead of leaving the capture:
|
|
1772
|
+
# one button per tool still covers fine / medium / coarse.
|
|
1773
|
+
if ($script:phase -eq 'adjust') {
|
|
1774
|
+
$rButton = Get-ButtonAt $e.X $e.Y
|
|
1775
|
+
if ($rButton -eq 'tool:pen') {
|
|
1776
|
+
$script:penWidthIndex = ($script:penWidthIndex + 1) % $script:penWidths.Count
|
|
1777
|
+
$script:hoverButton = $rButton
|
|
1778
|
+
$s.Invalidate()
|
|
1779
|
+
return
|
|
1780
|
+
}
|
|
1781
|
+
if ($rButton -eq 'tool:mosaic') {
|
|
1782
|
+
$script:mosaicIndex = ($script:mosaicIndex + 1) % $script:mosaicBlocks.Count
|
|
1783
|
+
$script:hoverButton = $rButton
|
|
1784
|
+
$s.Invalidate()
|
|
1785
|
+
return
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
$script:committed = $false
|
|
1789
|
+
$s.Close()
|
|
1790
|
+
return
|
|
1791
|
+
}
|
|
1792
|
+
if ($e.Button -ne [System.Windows.Forms.MouseButtons]::Left) { return }
|
|
1793
|
+
|
|
1794
|
+
# Adjust mode owns every press: handles and body resize/move the same
|
|
1795
|
+
# selection instead of starting a new one, and the confirm bar acts on press.
|
|
1796
|
+
if ($script:phase -eq 'adjust') {
|
|
1797
|
+
$button = Get-ButtonAt $e.X $e.Y
|
|
1798
|
+
if ($button -eq 'insert') { $script:committed = $true; Activate-HostWindow; $s.Close(); return }
|
|
1799
|
+
if ($button -eq 'cancel') { $script:committed = $false; $s.Close(); return }
|
|
1800
|
+
if ($button -eq 'copy') {
|
|
1801
|
+
# Copy without closing: the region stays editable, and confirming still
|
|
1802
|
+
# sends it to the conversation.
|
|
1803
|
+
Copy-SelectionToClipboard
|
|
1804
|
+
$script:copiedAt = [Environment]::TickCount
|
|
1805
|
+
$s.Invalidate()
|
|
1806
|
+
return
|
|
1807
|
+
}
|
|
1808
|
+
if ($button.StartsWith('tool:')) {
|
|
1809
|
+
$toolKey = $button.Substring(5)
|
|
1810
|
+
# Switching tools commits an open text box instead of stranding it.
|
|
1811
|
+
if ($null -ne $script:textBox) { Commit-TextEditor $true }
|
|
1812
|
+
if ($toolKey -eq 'undo') {
|
|
1813
|
+
if ($script:strokes.Count -gt 0) {
|
|
1814
|
+
$script:strokes.RemoveAt($script:strokes.Count - 1)
|
|
1815
|
+
Rebuild-Ink
|
|
1816
|
+
}
|
|
1817
|
+
return
|
|
1818
|
+
}
|
|
1819
|
+
if ($toolKey -eq 'color') {
|
|
1820
|
+
$script:penColorIndex = ($script:penColorIndex + 1) % $script:penColors.Count
|
|
1821
|
+
$s.Invalidate()
|
|
1822
|
+
return
|
|
1823
|
+
}
|
|
1824
|
+
# Picking the active tool again hands the body drag back to the region.
|
|
1825
|
+
if ($script:tool -eq $toolKey) { $script:tool = 'move' } else { $script:tool = $toolKey }
|
|
1826
|
+
$s.Invalidate()
|
|
1827
|
+
return
|
|
1828
|
+
}
|
|
1829
|
+
# A step badge drops on the click point: numbering takes no drag, so it
|
|
1830
|
+
# commits immediately and the counter moves on.
|
|
1831
|
+
if ($script:tool -eq 'number' -and $script:sel.Contains($e.X, $e.Y)) {
|
|
1832
|
+
$stroke = @{ Kind = 'number'; Color = (Get-PenColor); Width = (Get-PenWidth); Point = (New-Object System.Drawing.Point($e.X, $e.Y)); Num = $script:numberNext }
|
|
1833
|
+
$script:numberNext++
|
|
1834
|
+
if ($null -ne $script:inkG) {
|
|
1835
|
+
[void]$script:strokes.Add($stroke)
|
|
1836
|
+
Draw-Stroke $script:inkG $stroke 0 0
|
|
1837
|
+
}
|
|
1838
|
+
$s.Invalidate()
|
|
1839
|
+
return
|
|
1840
|
+
}
|
|
1841
|
+
if ($script:tool -eq 'text' -and $script:sel.Contains($e.X, $e.Y)) {
|
|
1842
|
+
Show-TextEditor $s $e.X $e.Y
|
|
1843
|
+
return
|
|
1844
|
+
}
|
|
1845
|
+
# A drawing tool owns the body: press starts a stroke instead of a move.
|
|
1846
|
+
if ($script:tool -ne 'move' -and $script:sel.Contains($e.X, $e.Y)) {
|
|
1847
|
+
$script:drawing = $true
|
|
1848
|
+
$script:drawStart = New-Object System.Drawing.Point($e.X, $e.Y)
|
|
1849
|
+
$script:drawEnd = New-Object System.Drawing.Point($e.X, $e.Y)
|
|
1850
|
+
$script:drawPoints = New-Object System.Collections.ArrayList
|
|
1851
|
+
if ($script:tool -eq 'pen') { [void]$script:drawPoints.Add($script:drawStart) }
|
|
1852
|
+
$s.Invalidate()
|
|
1853
|
+
return
|
|
1854
|
+
}
|
|
1855
|
+
# Once marks exist the region freezes: strokes stay in screen coordinates,
|
|
1856
|
+
# so moving or resizing the crop underneath them would decouple the marks
|
|
1857
|
+
# from what gets saved. Undo them away to edit the region again.
|
|
1858
|
+
if ($script:strokes.Count -gt 0) { return }
|
|
1859
|
+
$handle = Get-HandleAt $e.X $e.Y
|
|
1860
|
+
if ($handle -ne '') {
|
|
1861
|
+
$script:adjustMode = 'resize'
|
|
1862
|
+
$script:resizeEdge = $handle
|
|
1863
|
+
$script:resizeStart = New-Object System.Drawing.Point($e.X, $e.Y)
|
|
1864
|
+
$script:resizeStartRect = $script:sel
|
|
1865
|
+
return
|
|
1866
|
+
}
|
|
1867
|
+
if ($script:sel.Contains($e.X, $e.Y)) {
|
|
1868
|
+
$script:adjustMode = 'move'
|
|
1869
|
+
$script:moveStart = New-Object System.Drawing.Point($e.X, $e.Y)
|
|
1870
|
+
$script:moveStartRect = $script:sel
|
|
1871
|
+
return
|
|
1872
|
+
}
|
|
1873
|
+
# A press outside the selection starts over.
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
# A press only records intent. Committing here would steal every drag that
|
|
1877
|
+
# starts inside a window - which is most of the screen - so a manual region
|
|
1878
|
+
# could never be drawn over an application. Release decides instead.
|
|
1879
|
+
$script:phase = 'selecting'
|
|
1880
|
+
$script:origin = New-Object System.Drawing.Point($e.X, $e.Y)
|
|
1881
|
+
$script:downWindow = Resolve-Window $e.X $e.Y
|
|
1882
|
+
$script:dragged = $false
|
|
1883
|
+
$script:pressed = $true
|
|
1884
|
+
$script:hoverButton = ''
|
|
1885
|
+
if ($null -ne $script:downWindow) {
|
|
1886
|
+
Set-WindowSelection $script:downWindow
|
|
1887
|
+
} else {
|
|
1888
|
+
$script:sel = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
1889
|
+
$script:mode = 'none'
|
|
1890
|
+
$script:hoverTitle = ''
|
|
1891
|
+
}
|
|
1892
|
+
$s.Invalidate()
|
|
1893
|
+
})
|
|
1894
|
+
|
|
1895
|
+
$form.Add_MouseMove({
|
|
1896
|
+
param($s, $e)
|
|
1897
|
+
if ($script:drawing) {
|
|
1898
|
+
$p = New-Object System.Drawing.Point($e.X, $e.Y)
|
|
1899
|
+
if ($script:tool -eq 'pen') {
|
|
1900
|
+
if ($script:drawPoints.Count -eq 0) {
|
|
1901
|
+
[void]$script:drawPoints.Add($p)
|
|
1902
|
+
} else {
|
|
1903
|
+
$last = $script:drawPoints[$script:drawPoints.Count - 1]
|
|
1904
|
+
# Thinning by 2px: the paint pass replays the whole path every frame,
|
|
1905
|
+
# so a redundant point costs a segment for no visible gain.
|
|
1906
|
+
if ([Math]::Abs($p.X - $last.X) -ge 2 -or [Math]::Abs($p.Y - $last.Y) -ge 2) {
|
|
1907
|
+
[void]$script:drawPoints.Add($p)
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
} else {
|
|
1911
|
+
$script:drawEnd = $p
|
|
1912
|
+
}
|
|
1913
|
+
$s.Invalidate()
|
|
1914
|
+
return
|
|
1915
|
+
}
|
|
1916
|
+
if ($script:adjustMode -eq 'resize') {
|
|
1917
|
+
$r = $script:resizeStartRect
|
|
1918
|
+
$dx = $e.X - $script:resizeStart.X
|
|
1919
|
+
$dy = $e.Y - $script:resizeStart.Y
|
|
1920
|
+
$l = $r.X; $t = $r.Y; $rt = $r.Right; $bt = $r.Bottom
|
|
1921
|
+
if ($script:resizeEdge -match 'w') { $l += $dx }
|
|
1922
|
+
if ($script:resizeEdge -match 'e') { $rt += $dx }
|
|
1923
|
+
if ($script:resizeEdge -match 'n') { $t += $dy }
|
|
1924
|
+
if ($script:resizeEdge -match 's') { $bt += $dy }
|
|
1925
|
+
Set-SelectionFromEdges $l $t $rt $bt
|
|
1926
|
+
$s.Invalidate()
|
|
1927
|
+
return
|
|
1928
|
+
}
|
|
1929
|
+
if ($script:adjustMode -eq 'move') {
|
|
1930
|
+
$r = $script:moveStartRect
|
|
1931
|
+
$dx = $e.X - $script:moveStart.X
|
|
1932
|
+
$dy = $e.Y - $script:moveStart.Y
|
|
1933
|
+
$l = $r.X + $dx; $t = $r.Y + $dy
|
|
1934
|
+
$rt = $l + $r.Width; $bt = $t + $r.Height
|
|
1935
|
+
# Clamp by shifting the whole rect so the size survives hitting an edge.
|
|
1936
|
+
if ($l -lt 0) { $l = 0; $rt = $r.Width }
|
|
1937
|
+
if ($t -lt 0) { $t = 0; $bt = $r.Height }
|
|
1938
|
+
if ($rt -gt $script:vs.Width) { $rt = $script:vs.Width; $l = $rt - $r.Width }
|
|
1939
|
+
if ($bt -gt $script:vs.Height) { $bt = $script:vs.Height; $t = $bt - $r.Height }
|
|
1940
|
+
$script:sel = New-Object System.Drawing.Rectangle($l, $t, $r.Width, $r.Height)
|
|
1941
|
+
$s.Invalidate()
|
|
1942
|
+
return
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
if ($script:pressed) {
|
|
1946
|
+
# Movement inside the click tolerance keeps the window highlight, so a
|
|
1947
|
+
# slightly shaky click still selects the window instead of a 3px sliver.
|
|
1948
|
+
if (-not $script:dragged) {
|
|
1949
|
+
if ([Math]::Abs($e.X - $script:origin.X) -lt 4 -and [Math]::Abs($e.Y - $script:origin.Y) -lt 4) {
|
|
1950
|
+
return
|
|
1951
|
+
}
|
|
1952
|
+
$script:dragged = $true
|
|
1953
|
+
$script:hoverTitle = ''
|
|
1954
|
+
}
|
|
1955
|
+
$x1 = $script:origin.X; $x2 = $e.X
|
|
1956
|
+
if ($x1 -gt $x2) { $t = $x1; $x1 = $x2; $x2 = $t }
|
|
1957
|
+
$y1 = $script:origin.Y; $y2 = $e.Y
|
|
1958
|
+
if ($y1 -gt $y2) { $t = $y1; $y1 = $y2; $y2 = $t }
|
|
1959
|
+
$script:sel = New-Object System.Drawing.Rectangle($x1, $y1, ($x2 - $x1), ($y2 - $y1))
|
|
1960
|
+
$script:mode = 'drag'
|
|
1961
|
+
$s.Invalidate()
|
|
1962
|
+
return
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
if ($script:phase -eq 'adjust') {
|
|
1966
|
+
# Cursor feedback is what makes the handles discoverable.
|
|
1967
|
+
$handle = Get-HandleAt $e.X $e.Y
|
|
1968
|
+
$button = Get-ButtonAt $e.X $e.Y
|
|
1969
|
+
if ($handle -eq 'nw' -or $handle -eq 'se') {
|
|
1970
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::SizeNWSE
|
|
1971
|
+
} elseif ($handle -eq 'ne' -or $handle -eq 'sw') {
|
|
1972
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::SizeNESW
|
|
1973
|
+
} elseif ($handle -eq 'n' -or $handle -eq 's') {
|
|
1974
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::SizeNS
|
|
1975
|
+
} elseif ($handle -eq 'e' -or $handle -eq 'w') {
|
|
1976
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::SizeWE
|
|
1977
|
+
} elseif ($button -ne '') {
|
|
1978
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::Hand
|
|
1979
|
+
} elseif ($script:tool -ne 'move' -and $script:sel.Contains($e.X, $e.Y)) {
|
|
1980
|
+
# A drawing tool owns the body, so the body stops advertising "move".
|
|
1981
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::Cross
|
|
1982
|
+
} elseif ($script:sel.Contains($e.X, $e.Y)) {
|
|
1983
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::SizeAll
|
|
1984
|
+
} else {
|
|
1985
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::Cross
|
|
1986
|
+
}
|
|
1987
|
+
if ($button -ne $script:hoverButton) {
|
|
1988
|
+
$script:hoverButton = $button
|
|
1989
|
+
$s.Invalidate()
|
|
1990
|
+
}
|
|
1991
|
+
return
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
$w = Resolve-Window $e.X $e.Y
|
|
1995
|
+
if ($null -ne $w) {
|
|
1996
|
+
Set-WindowSelection $w
|
|
1997
|
+
} else {
|
|
1998
|
+
$script:sel = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
1999
|
+
$script:mode = 'none'
|
|
2000
|
+
$script:hoverTitle = ''
|
|
2001
|
+
}
|
|
2002
|
+
$s.Invalidate()
|
|
2003
|
+
})
|
|
2004
|
+
|
|
2005
|
+
$form.Add_MouseUp({
|
|
2006
|
+
param($s, $e)
|
|
2007
|
+
if ($e.Button -ne [System.Windows.Forms.MouseButtons]::Left) { return }
|
|
2008
|
+
if ($script:drawing) {
|
|
2009
|
+
$script:drawing = $false
|
|
2010
|
+
Finish-Stroke $e.X $e.Y
|
|
2011
|
+
$s.Invalidate()
|
|
2012
|
+
return
|
|
2013
|
+
}
|
|
2014
|
+
if ($script:adjustMode -ne '') {
|
|
2015
|
+
$script:adjustMode = ''
|
|
2016
|
+
$s.Invalidate()
|
|
2017
|
+
return
|
|
2018
|
+
}
|
|
2019
|
+
if (-not $script:pressed) { return }
|
|
2020
|
+
$script:pressed = $false
|
|
2021
|
+
$wasDragged = $script:dragged
|
|
2022
|
+
$pressedWindow = $script:downWindow
|
|
2023
|
+
if ($wasDragged) {
|
|
2024
|
+
# Recompute from the release point: the last MouseMove can lag one message
|
|
2025
|
+
# behind the cursor, which would shave a few pixels off the region.
|
|
2026
|
+
$x1 = $script:origin.X; $x2 = $e.X
|
|
2027
|
+
if ($x1 -gt $x2) { $t = $x1; $x1 = $x2; $x2 = $t }
|
|
2028
|
+
$y1 = $script:origin.Y; $y2 = $e.Y
|
|
2029
|
+
if ($y1 -gt $y2) { $t = $y1; $y1 = $y2; $y2 = $t }
|
|
2030
|
+
$script:sel = New-Object System.Drawing.Rectangle($x1, $y1, ($x2 - $x1), ($y2 - $y1))
|
|
2031
|
+
# A real drag under 8px is a slip, not a region.
|
|
2032
|
+
if ($script:sel.Width -ge 8 -and $script:sel.Height -ge 8) {
|
|
2033
|
+
$script:phase = 'adjust'
|
|
2034
|
+
$script:mode = 'drag'
|
|
2035
|
+
$s.Cursor = [System.Windows.Forms.Cursors]::SizeAll
|
|
2036
|
+
} else {
|
|
2037
|
+
$script:phase = 'idle'
|
|
2038
|
+
$script:sel = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
2039
|
+
$script:mode = 'none'
|
|
2040
|
+
$script:hoverTitle = ''
|
|
2041
|
+
}
|
|
2042
|
+
$s.Invalidate()
|
|
2043
|
+
return
|
|
2044
|
+
}
|
|
2045
|
+
# No movement: the window under the cursor becomes the selection, still
|
|
2046
|
+
# editable, so a clicked window can be trimmed before confirming.
|
|
2047
|
+
if ($null -ne $pressedWindow) {
|
|
2048
|
+
Set-WindowSelection $pressedWindow
|
|
2049
|
+
$script:phase = 'adjust'
|
|
2050
|
+
} else {
|
|
2051
|
+
$script:phase = 'idle'
|
|
2052
|
+
$script:sel = New-Object System.Drawing.Rectangle(0, 0, 0, 0)
|
|
2053
|
+
$script:mode = 'none'
|
|
2054
|
+
$script:hoverTitle = ''
|
|
2055
|
+
}
|
|
2056
|
+
$s.Invalidate()
|
|
2057
|
+
})
|
|
2058
|
+
|
|
2059
|
+
# Double-click inside the selection is the fast confirm path.
|
|
2060
|
+
$form.Add_DoubleClick({
|
|
2061
|
+
param($s, $e)
|
|
2062
|
+
# With a drawing tool the second click is part of a stroke, not a confirm.
|
|
2063
|
+
if ($script:phase -eq 'adjust' -and $script:tool -eq 'move' -and $script:sel.Contains($e.X, $e.Y)) {
|
|
2064
|
+
$script:committed = $true
|
|
2065
|
+
Activate-HostWindow
|
|
2066
|
+
$s.Close()
|
|
2067
|
+
}
|
|
2068
|
+
})
|
|
2069
|
+
|
|
2070
|
+
$form.Add_KeyDown({
|
|
2071
|
+
param($s, $e)
|
|
2072
|
+
if ($e.KeyCode -eq [System.Windows.Forms.Keys]::Escape) {
|
|
2073
|
+
$script:committed = $false
|
|
2074
|
+
$s.Close()
|
|
2075
|
+
return
|
|
2076
|
+
}
|
|
2077
|
+
if ($e.Control -and $e.KeyCode -eq [System.Windows.Forms.Keys]::Z) {
|
|
2078
|
+
if ($script:strokes.Count -gt 0) {
|
|
2079
|
+
$script:strokes.RemoveAt($script:strokes.Count - 1)
|
|
2080
|
+
Rebuild-Ink
|
|
2081
|
+
}
|
|
2082
|
+
return
|
|
2083
|
+
}
|
|
2084
|
+
if ($e.KeyCode -eq [System.Windows.Forms.Keys]::Enter -and $script:phase -eq 'adjust') {
|
|
2085
|
+
$script:committed = $true
|
|
2086
|
+
Activate-HostWindow
|
|
2087
|
+
$s.Close()
|
|
2088
|
+
}
|
|
2089
|
+
})
|
|
2090
|
+
|
|
2091
|
+
$timer = New-Object System.Windows.Forms.Timer
|
|
2092
|
+
$timer.Interval = 1000
|
|
2093
|
+
$timer.Add_Tick({
|
|
2094
|
+
$script:tick++
|
|
2095
|
+
# An overlay left behind by a dead host would cover the desktop with a
|
|
2096
|
+
# topmost form that no one can dismiss.
|
|
2097
|
+
if (-not (Test-ParentAlive)) {
|
|
2098
|
+
$script:committed = $false
|
|
2099
|
+
$script:form.Close()
|
|
2100
|
+
return
|
|
2101
|
+
}
|
|
2102
|
+
if ($script:tick -ge $script:timeout) { $script:form.Close() }
|
|
2103
|
+
# Repaint while the "已复制" acknowledgement is still on screen, so the label
|
|
2104
|
+
# reverts to "复制" even if the mouse never moves again.
|
|
2105
|
+
if ($script:copiedAt -gt 0) {
|
|
2106
|
+
$age = [Environment]::TickCount - $script:copiedAt
|
|
2107
|
+
if ($age -ge 0 -and $age -lt 2000) { $script:form.Invalidate() }
|
|
2108
|
+
}
|
|
2109
|
+
})
|
|
2110
|
+
$timer.Start()
|
|
2111
|
+
|
|
2112
|
+
$form.Add_Shown({
|
|
2113
|
+
param($s, $e)
|
|
2114
|
+
$s.Activate()
|
|
2115
|
+
$s.BringToFront()
|
|
2116
|
+
})
|
|
2117
|
+
|
|
2118
|
+
try {
|
|
2119
|
+
if ($script:serveMode -and $null -ne $script:gestureWatch) {
|
|
2120
|
+
[Console]::Out.WriteLine('SHOWN|' + $script:gestureWatch.ElapsedMilliseconds)
|
|
2121
|
+
[Console]::Out.Flush()
|
|
2122
|
+
}
|
|
2123
|
+
[void]$form.ShowDialog()
|
|
2124
|
+
} finally {
|
|
2125
|
+
$timer.Stop()
|
|
2126
|
+
$timer.Dispose()
|
|
2127
|
+
$form.Dispose()
|
|
2128
|
+
$script:form = $null
|
|
2129
|
+
Restore-HostWindow
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
try {
|
|
2133
|
+
if (-not $script:committed) { return @{ Status = 'CANCELLED' } }
|
|
2134
|
+
|
|
2135
|
+
# Closing the overlay hands the foreground back to whatever owned it before,
|
|
2136
|
+
# which undoes the raise done on the way out; ask again now that it is gone.
|
|
2137
|
+
Start-Sleep -Milliseconds 150
|
|
2138
|
+
Activate-HostWindow
|
|
2139
|
+
|
|
2140
|
+
$sel = $script:sel
|
|
2141
|
+
if ($sel.X -lt 0) { $sel.Width += $sel.X; $sel.X = 0 }
|
|
2142
|
+
if ($sel.Y -lt 0) { $sel.Height += $sel.Y; $sel.Y = 0 }
|
|
2143
|
+
if (($sel.X + $sel.Width) -gt $script:vs.Width) { $sel.Width = $script:vs.Width - $sel.X }
|
|
2144
|
+
if (($sel.Y + $sel.Height) -gt $script:vs.Height) { $sel.Height = $script:vs.Height - $sel.Y }
|
|
2145
|
+
if ($sel.Width -lt 4 -or $sel.Height -lt 4) { return @{ Status = 'CANCELLED' } }
|
|
2146
|
+
|
|
2147
|
+
$file = Save-Crop $sel $OutDir
|
|
2148
|
+
Restore-HostWindow
|
|
2149
|
+
return @{ Status = 'OK'; Path = $file }
|
|
2150
|
+
} finally {
|
|
2151
|
+
if ($null -ne $script:measureG) { try { $script:measureG.Dispose() } catch { } }
|
|
2152
|
+
if ($null -ne $script:inkG) { try { $script:inkG.Dispose() } catch { } }
|
|
2153
|
+
if ($null -ne $script:inkBmp) { try { $script:inkBmp.Dispose() } catch { } }
|
|
2154
|
+
if ($null -ne $script:bmp) { try { $script:bmp.Dispose() } catch { } }
|
|
2155
|
+
$script:measureG = $null
|
|
2156
|
+
$script:inkG = $null
|
|
2157
|
+
$script:inkBmp = $null
|
|
2158
|
+
$script:bmp = $null
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
# Relay one result line to the host. Flush matters: PowerShell buffers redirected
|
|
2163
|
+
# stdout, and the host is blocked waiting for this line.
|
|
2164
|
+
function Write-Result {
|
|
2165
|
+
param([string]$Status, [string]$Detail = '')
|
|
2166
|
+
[Console]::Out.WriteLine('RESULT|' + $Status + '|' + $Detail)
|
|
2167
|
+
[Console]::Out.Flush()
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
# Serve mode reports when the overlay is actually on screen, so the host (and a
|
|
2171
|
+
# test) can tell the perceived click latency apart from the time spent waiting
|
|
2172
|
+
# for the user.
|
|
2173
|
+
$script:serveMode = [bool]$Serve
|
|
2174
|
+
$script:gestureWatch = $null
|
|
2175
|
+
|
|
2176
|
+
# The first grab of a desktop this size allocates ~28MB and JITs the GDI
|
|
2177
|
+
# entry points on the way; doing that once at spawn keeps the first click as
|
|
2178
|
+
# quick as the tenth instead of paying ~1.2s on the first shot of the session.
|
|
2179
|
+
function Initialize-CapturePath {
|
|
2180
|
+
try {
|
|
2181
|
+
$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
|
|
2182
|
+
$probe = New-Object System.Drawing.Bitmap(8, 8)
|
|
2183
|
+
$probeG = [System.Drawing.Graphics]::FromImage($probe)
|
|
2184
|
+
$probeG.CopyFromScreen($screen.X, $screen.Y, 0, 0, (New-Object System.Drawing.Size(8, 8)))
|
|
2185
|
+
$probeG.Dispose()
|
|
2186
|
+
$probe.Dispose()
|
|
2187
|
+
$full = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height)
|
|
2188
|
+
$fullG = [System.Drawing.Graphics]::FromImage($full)
|
|
2189
|
+
$fullG.Clear([System.Drawing.Color]::Black)
|
|
2190
|
+
$fullG.Dispose()
|
|
2191
|
+
$full.Dispose()
|
|
2192
|
+
} catch { }
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
if ($Serve) {
|
|
2196
|
+
Set-Hotkey $Hotkey
|
|
2197
|
+
Set-Float $Float
|
|
2198
|
+
Initialize-CapturePath
|
|
2199
|
+
# Announce readiness only after the expensive part (process start, assembly
|
|
2200
|
+
# load, two Add-Type compiles) is behind us, so the host knows a click will be
|
|
2201
|
+
# served warm.
|
|
2202
|
+
[Console]::Out.WriteLine('READY')
|
|
2203
|
+
[Console]::Out.Flush()
|
|
2204
|
+
|
|
2205
|
+
while ($true) {
|
|
2206
|
+
$line = [Console]::In.ReadLine()
|
|
2207
|
+
# EOF means the host went away; do not outlive it.
|
|
2208
|
+
if ($null -eq $line) { break }
|
|
2209
|
+
$line = $line.Trim()
|
|
2210
|
+
if ($line -eq '') { continue }
|
|
2211
|
+
if ($line -eq 'QUIT') { break }
|
|
2212
|
+
|
|
2213
|
+
$parts = $line.Split('|')
|
|
2214
|
+
if ($parts[0] -eq 'FLOAT') {
|
|
2215
|
+
$mode = 'on'
|
|
2216
|
+
if ($parts.Count -gt 1) { $mode = $parts[1] }
|
|
2217
|
+
Set-Float $mode
|
|
2218
|
+
[Console]::Out.WriteLine('FLOATSTATE|ok|' + $mode)
|
|
2219
|
+
[Console]::Out.Flush()
|
|
2220
|
+
continue
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
if ($parts[0] -eq 'HOTKEY') {
|
|
2224
|
+
$spec = 'off'
|
|
2225
|
+
if ($parts.Count -gt 1) { $spec = $parts[1] }
|
|
2226
|
+
Set-Hotkey $spec
|
|
2227
|
+
[Console]::Out.WriteLine('HOTKEYSTATE|ok|' + $spec)
|
|
2228
|
+
[Console]::Out.Flush()
|
|
2229
|
+
continue
|
|
2230
|
+
}
|
|
2231
|
+
|
|
2232
|
+
if ($parts[0] -ne 'CAPTURE') {
|
|
2233
|
+
Write-Result 'ERROR' ('unknown command: ' + $parts[0])
|
|
2234
|
+
continue
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
$delay = 0
|
|
2238
|
+
if ($parts.Count -gt 1) { [void][int]::TryParse($parts[1], [ref]$delay) }
|
|
2239
|
+
$full = $false
|
|
2240
|
+
if ($parts.Count -gt 2 -and $parts[2] -eq '1') { $full = $true }
|
|
2241
|
+
|
|
2242
|
+
try {
|
|
2243
|
+
$script:gestureWatch = [System.Diagnostics.Stopwatch]::StartNew()
|
|
2244
|
+
$r = Invoke-OneCapture -OutDir $OutDir -TimeoutSeconds $TimeoutSeconds -FullScreen $full -DelaySeconds $delay
|
|
2245
|
+
if ($r.Status -eq 'OK') { Write-Result 'OK' $r.Path } else { Write-Result 'CANCELLED' }
|
|
2246
|
+
} catch {
|
|
2247
|
+
Write-Result 'ERROR' ($_.Exception.Message -replace "`r?`n", ' ')
|
|
2248
|
+
} finally {
|
|
2249
|
+
$script:gestureWatch = $null
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
exit 0
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
try {
|
|
2256
|
+
$r = Invoke-OneCapture -OutDir $OutDir -TimeoutSeconds $TimeoutSeconds -FullScreen:$FullScreen -DelaySeconds $DelaySeconds
|
|
2257
|
+
if ($r.Status -eq 'OK') {
|
|
2258
|
+
Write-Output $r.Path
|
|
2259
|
+
} else {
|
|
2260
|
+
Write-Output 'CANCELLED'
|
|
2261
|
+
}
|
|
2262
|
+
exit 0
|
|
2263
|
+
} catch {
|
|
2264
|
+
[Console]::Error.WriteLine($_.Exception.Message)
|
|
2265
|
+
exit 1
|
|
2266
|
+
}
|