-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
549 lines (475 loc) · 15.9 KB
/
Copy pathProgram.cs
File metadata and controls
549 lines (475 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
namespace DOS.MiniMag;
public static class Program
{
[STAThread]
public static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new MiniMagForm());
}
}
internal sealed class MiniMagForm : Form
{
private const int LensMargin = 18;
private const int HotKeyToggle = 1;
private const int HotKeyFreeze = 2;
private const int HotKeyZoomIn = 3;
private const int HotKeyZoomOut = 4;
private const int HotKeyZoom2x = 5;
private const int HotKeyZoom3x = 6;
private const int HotKeyZoom4x = 7;
private const int HotKeyZoomInNumpad = 8;
private const int HotKeyZoomOutNumpad = 9;
private const int HotKeyCloseLens = 10;
private static readonly Size[] LensSizes =
[
new(400, 198),
new(480, 240),
new(640, 318)
];
private readonly System.Windows.Forms.Timer refreshTimer = new();
private readonly NotifyIcon trayIcon = new();
private readonly LowLevelMouseProc mouseHookProc;
private readonly Icon trayMiniIcon;
private Bitmap? frame;
private bool frozen;
private bool peekDown;
private bool sidePeekDown;
private bool visibleBeforePeek;
private bool closeLensHotKeyRegistered;
private int zoom = 3;
private int lensSizeIndex = 1;
private IntPtr mouseHook;
public MiniMagForm()
{
mouseHookProc = MouseHookCallback;
Text = "DOS MiniMag";
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
DoubleBuffered = true;
BackColor = Color.Black;
ForeColor = Color.White;
Size = LensSizes[lensSizeIndex];
StartPosition = FormStartPosition.Manual;
refreshTimer.Interval = 50;
refreshTimer.Tick += (_, _) => RefreshTick();
trayMiniIcon = CreateTrayIcon();
trayIcon.Text = "DOS MiniMag";
trayIcon.Icon = trayMiniIcon;
trayIcon.Visible = true;
trayIcon.ContextMenuStrip = BuildTrayMenu();
trayIcon.DoubleClick += (_, _) => ToggleVisible();
}
protected override CreateParams CreateParams
{
get
{
const int wsExToolWindow = 0x00000080;
const int wsExNoActivate = 0x08000000;
var cp = base.CreateParams;
cp.ExStyle |= wsExToolWindow | wsExNoActivate;
return cp;
}
}
protected override bool ShowWithoutActivation => true;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var cursor = Cursor.Position;
MoveNearCursor(cursor, GetCaptureRectangle(cursor));
RegisterHotKeys();
SyncCloseLensHotKey();
mouseHook = SetWindowsHookEx(14, mouseHookProc, IntPtr.Zero, 0);
refreshTimer.Start();
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
refreshTimer.Stop();
if (mouseHook != IntPtr.Zero)
{
UnhookWindowsHookEx(mouseHook);
}
UnregisterHotKeys();
frame?.Dispose();
trayIcon.Visible = false;
trayIcon.Dispose();
trayMiniIcon.Dispose();
base.OnFormClosed(e);
}
protected override void WndProc(ref Message m)
{
const int wmHotKey = 0x0312;
const int wmNcHitTest = 0x0084;
const int htTransparent = -1;
if (m.Msg == wmHotKey)
{
HandleHotKey(m.WParam.ToInt32());
return;
}
if (m.Msg == wmNcHitTest)
{
m.Result = new IntPtr(htTransparent);
return;
}
base.WndProc(ref m);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
HideLens();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.Black);
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
if (frame is not null)
{
e.Graphics.DrawImage(frame, ClientRectangle);
}
using var font = new Font("Segoe UI", 22f, FontStyle.Bold);
var label = frozen ? $"FROZEN {zoom}x" : $"{zoom}x";
var labelSize = TextRenderer.MeasureText(label, font, Size.Empty, TextFormatFlags.NoPadding);
var labelRect = new Rectangle(
ClientSize.Width - labelSize.Width - 8,
ClientSize.Height - labelSize.Height - 6,
labelSize.Width,
labelSize.Height);
TextRenderer.DrawText(
e.Graphics,
label,
font,
labelRect,
Color.Red,
TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
}
private ContextMenuStrip BuildTrayMenu()
{
var menu = new ContextMenuStrip();
menu.Items.Add("Show / hide (Alt+M)", null, (_, _) => ToggleVisible());
menu.Items.Add("Freeze / unfreeze (Alt+F)", null, (_, _) => ToggleFreeze());
menu.Items.Add("Zoom in (Alt++)", null, (_, _) => ChangeZoom(1));
menu.Items.Add("Zoom out (Alt+-)", null, (_, _) => ChangeZoom(-1));
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Zoom 2x (Alt+1)", null, (_, _) => SetZoom(2));
menu.Items.Add("Zoom 3x (Alt+2)", null, (_, _) => SetZoom(3));
menu.Items.Add("Zoom 4x (Alt+3)", null, (_, _) => SetZoom(4));
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Exit", null, (_, _) => Close());
return menu;
}
private static Icon CreateTrayIcon()
{
using var bitmap = new Bitmap(32, 32);
using (var g = Graphics.FromImage(bitmap))
{
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
using var lensPen = new Pen(Color.Red, 3);
using var handlePen = new Pen(Color.Red, 3);
g.DrawEllipse(lensPen, 5, 4, 17, 17);
g.DrawLine(handlePen, 19, 20, 27, 28);
using var shinePen = new Pen(Color.FromArgb(230, 255, 255, 255), 2);
g.DrawArc(shinePen, 9, 8, 8, 8, 190, 90);
}
var handle = bitmap.GetHicon();
try
{
using var icon = Icon.FromHandle(handle);
return (Icon)icon.Clone();
}
finally
{
DestroyIcon(handle);
}
}
private void RefreshLens()
{
if (!Visible)
{
return;
}
var cursor = Cursor.Position;
var source = GetCaptureRectangle(cursor);
MoveNearCursor(cursor, source);
if (!frozen)
{
CaptureSource(source);
}
Invalidate();
}
private void RefreshTick()
{
UpdatePeekMode();
RefreshLens();
}
private void UpdatePeekMode()
{
var isPeekDown = IsKeyDown(Keys.F13) || sidePeekDown;
if (isPeekDown && !peekDown)
{
visibleBeforePeek = Visible;
Visible = true;
TopMost = true;
SyncCloseLensHotKey();
RefreshLens();
}
else if (!isPeekDown && peekDown && !visibleBeforePeek)
{
Visible = false;
SyncCloseLensHotKey();
}
peekDown = isPeekDown;
}
private Rectangle GetCaptureRectangle(Point cursor)
{
var captureWidth = Math.Max(1, Width / zoom);
var captureHeight = Math.Max(1, Height / zoom);
var bounds = SystemInformation.VirtualScreen;
return new Rectangle(
Math.Clamp(cursor.X - captureWidth / 2, bounds.Left, bounds.Right - captureWidth),
Math.Clamp(cursor.Y - captureHeight / 2, bounds.Top, bounds.Bottom - captureHeight),
captureWidth,
captureHeight);
}
private void CaptureSource(Rectangle source)
{
frame ??= new Bitmap(source.Width, source.Height);
if (frame.Width != source.Width || frame.Height != source.Height)
{
frame.Dispose();
frame = new Bitmap(source.Width, source.Height);
}
using var g = Graphics.FromImage(frame);
try
{
g.CopyFromScreen(source.Location, Point.Empty, source.Size, CopyPixelOperation.SourceCopy);
}
catch
{
g.Clear(Color.Black);
}
}
private void MoveNearCursor(Point cursor, Rectangle source)
{
var screen = Screen.FromPoint(cursor).WorkingArea;
var candidates = new[]
{
new Point(source.Right + LensMargin, source.Bottom + LensMargin),
new Point(source.Left - Width - LensMargin, source.Bottom + LensMargin),
new Point(source.Right + LensMargin, source.Top - Height - LensMargin),
new Point(source.Left - Width - LensMargin, source.Top - Height - LensMargin)
};
foreach (var candidate in candidates)
{
var lens = new Rectangle(candidate, Size);
if (screen.Contains(lens) && !lens.IntersectsWith(source))
{
Location = candidate;
return;
}
}
foreach (var candidate in candidates)
{
var clamped = new Point(
Math.Clamp(candidate.X, screen.Left, screen.Right - Width),
Math.Clamp(candidate.Y, screen.Top, screen.Bottom - Height));
var lens = new Rectangle(clamped, Size);
if (!lens.IntersectsWith(source))
{
Location = clamped;
return;
}
}
Location = new Point(
Math.Clamp(source.Right + LensMargin, screen.Left, screen.Right - Width),
Math.Clamp(source.Bottom + LensMargin, screen.Top, screen.Bottom - Height));
}
private void RegisterHotKeys()
{
const uint modAlt = 0x0001;
RegisterHotKey(Handle, HotKeyToggle, modAlt, (uint)Keys.M);
RegisterHotKey(Handle, HotKeyFreeze, modAlt, (uint)Keys.F);
RegisterHotKey(Handle, HotKeyZoomIn, modAlt, (uint)Keys.Oemplus);
RegisterHotKey(Handle, HotKeyZoomOut, modAlt, (uint)Keys.OemMinus);
RegisterHotKey(Handle, HotKeyZoomInNumpad, modAlt, (uint)Keys.Add);
RegisterHotKey(Handle, HotKeyZoomOutNumpad, modAlt, (uint)Keys.Subtract);
RegisterHotKey(Handle, HotKeyZoom2x, modAlt, (uint)Keys.D1);
RegisterHotKey(Handle, HotKeyZoom3x, modAlt, (uint)Keys.D2);
RegisterHotKey(Handle, HotKeyZoom4x, modAlt, (uint)Keys.D3);
}
private void SyncCloseLensHotKey()
{
if (Visible && !closeLensHotKeyRegistered)
{
closeLensHotKeyRegistered = RegisterHotKey(Handle, HotKeyCloseLens, 0, (uint)Keys.Escape);
}
else if (!Visible && closeLensHotKeyRegistered)
{
UnregisterCloseLensHotKey();
}
}
private void UnregisterCloseLensHotKey()
{
if (!closeLensHotKeyRegistered)
{
return;
}
UnregisterHotKey(Handle, HotKeyCloseLens);
closeLensHotKeyRegistered = false;
}
private void UnregisterHotKeys()
{
UnregisterHotKey(Handle, HotKeyToggle);
UnregisterHotKey(Handle, HotKeyFreeze);
UnregisterHotKey(Handle, HotKeyZoomIn);
UnregisterHotKey(Handle, HotKeyZoomOut);
UnregisterHotKey(Handle, HotKeyZoomInNumpad);
UnregisterHotKey(Handle, HotKeyZoomOutNumpad);
UnregisterHotKey(Handle, HotKeyZoom2x);
UnregisterHotKey(Handle, HotKeyZoom3x);
UnregisterHotKey(Handle, HotKeyZoom4x);
UnregisterCloseLensHotKey();
}
private void HandleHotKey(int id)
{
switch (id)
{
case HotKeyToggle:
ToggleVisible();
break;
case HotKeyFreeze:
ToggleFreeze();
break;
case HotKeyZoomIn:
case HotKeyZoomInNumpad:
ChangeZoom(1);
break;
case HotKeyZoomOut:
case HotKeyZoomOutNumpad:
ChangeZoom(-1);
break;
case HotKeyZoom2x:
SetZoom(2);
break;
case HotKeyZoom3x:
SetZoom(3);
break;
case HotKeyZoom4x:
SetZoom(4);
break;
case HotKeyCloseLens:
HideLens();
break;
}
}
private void ToggleVisible()
{
Visible = !Visible;
if (Visible)
{
TopMost = true;
RefreshLens();
}
SyncCloseLensHotKey();
}
private void HideLens()
{
Visible = false;
SyncCloseLensHotKey();
}
private void ToggleFreeze()
{
frozen = !frozen;
Invalidate();
}
private void ChangeZoom(int direction)
{
SetZoom(zoom + direction);
}
private void SetZoom(int value)
{
zoom = Math.Clamp(value, 2, 6);
frame?.Dispose();
frame = null;
RefreshLens();
}
private void SetLensSize(int index)
{
lensSizeIndex = Math.Clamp(index, 0, LensSizes.Length - 1);
Size = LensSizes[lensSizeIndex];
frame?.Dispose();
frame = null;
RefreshLens();
}
private IntPtr MouseHookCallback(int code, IntPtr wParam, IntPtr lParam)
{
const int wmXButtonDown = 0x020B;
const int wmXButtonUp = 0x020C;
const int xbutton1 = 1;
const int xbutton2 = 2;
if (code >= 0)
{
var message = wParam.ToInt32();
if (message == wmXButtonDown || message == wmXButtonUp)
{
var info = Marshal.PtrToStructure<MouseHookInfo>(lParam);
var button = (int)((info.MouseData >> 16) & 0xffff);
if (button == xbutton1)
{
sidePeekDown = message == wmXButtonDown;
return new IntPtr(1);
}
if (button == xbutton2 && message == wmXButtonDown)
{
BeginInvoke(ToggleFreeze);
return new IntPtr(1);
}
}
}
return CallNextHookEx(mouseHook, code, wParam, lParam);
}
private static bool IsKeyDown(Keys key)
{
const int keyPressed = 0x8000;
return (GetAsyncKeyState((int)key) & keyPressed) != 0;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct MouseHookInfo
{
public NativePoint Point;
public uint MouseData;
public uint Flags;
public uint Time;
public IntPtr ExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct NativePoint
{
public int X;
public int Y;
}
}