-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
532 lines (459 loc) · 20.2 KB
/
App.xaml.cs
File metadata and controls
532 lines (459 loc) · 20.2 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
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
namespace ScreenGrid
{
public partial class App : Application
{
// ── State ───────────────────────────────────────────────────────
private NotifyIcon? _trayIcon;
private OverlayWindow? _overlay;
private DispatcherTimer? _mouseTracker; private GridConfig _gridConfig = GridConfig.CreateDefault();
// Win-event hook (must be stored to prevent GC)
private NativeMethods.WinEventDelegate? _winEventDelegate;
private IntPtr _winEventHook;
private NativeMethods.LowLevelMouseProc? _mouseHookProc;
private IntPtr _mouseHook;
// Drag state
private IntPtr _draggedWindowHandle;
private bool _isDragging;
private bool _overlayVisible;
// ── Startup ─────────────────────────────────────────────────────
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_gridConfig = GridConfig.LoadActive();
SetupTrayIcon();
SetupOverlay();
SetupWinEventHook();
SetupMouseWheelHook();
SetupMouseTracker();
}
// ── Tray Icon ───────────────────────────────────────────────────
private void SetupTrayIcon()
{
_trayIcon = new NotifyIcon
{
Icon = CreateGridIcon(),
Visible = true,
Text = "ScreenGrid – Hold Shift while dragging a window"
};
var menu = new ContextMenuStrip();
var header = new ToolStripMenuItem("ScreenGrid v1.0") { Enabled = false };
menu.Items.Add(header);
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Create / Edit Grid", null, (_, _) => ShowGridEditor());
menu.Items.Add("Load Grid from File…", null, (_, _) => LoadGridFromFile());
menu.Items.Add("Reset Grid to Defaults", null, (_, _) => ResetGridToDefaults());
menu.Items.Add(new ToolStripSeparator());
var startupItem = new ToolStripMenuItem("Run at Startup")
{
CheckOnClick = true,
Checked = StartupManager.IsRegistered()
};
startupItem.CheckedChanged += (_, _) =>
{
try
{
if (startupItem.Checked)
StartupManager.Register();
else
StartupManager.Unregister();
}
catch (Exception ex)
{
MessageBox.Show($"Failed to update startup setting:\n{ex.Message}",
"ScreenGrid", MessageBoxButton.OK, MessageBoxImage.Warning);
startupItem.Checked = StartupManager.IsRegistered();
}
};
menu.Items.Add(startupItem);
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("How to use", null, (_, _) => ShowUsageInfo());
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Exit", null, (_, _) => ExitApp());
_trayIcon.ContextMenuStrip = menu;
_trayIcon.DoubleClick += (_, _) => ShowUsageInfo();
}
private static void ShowUsageInfo()
{
MessageBox.Show(
"ScreenGrid is running in the background.\n\n" +
"HOW TO USE:\n" +
"1. Start dragging any window by its title bar.\n" +
"2. While dragging, hold the SHIFT key.\n" +
"3. Two grid variants are shown at a time.\n" +
"4. Scroll the mouse wheel (up/down) to switch pairs.\n" +
"5. Drag to the desired zone – it highlights.\n" +
"6. Release the mouse button to snap the window.\n\n" +
"Release SHIFT at any time to cancel.\n" +
"Tip: the overlay header shows Pair X/Y and reminds\n" +
"you to scroll for more variants.",
"ScreenGrid",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
/// <summary>Creates a simple 16×16 grid icon for the system tray.</summary>
private static Icon CreateGridIcon()
{
var bmp = new Bitmap(16, 16);
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
using var pen = new Pen(Color.CornflowerBlue, 1f);
g.DrawRectangle(pen, 1, 1, 13, 13);
g.DrawLine(pen, 5, 1, 5, 14);
g.DrawLine(pen, 10, 1, 10, 14);
g.DrawLine(pen, 1, 5, 14, 5);
g.DrawLine(pen, 1, 10, 14, 10);
}
return Icon.FromHandle(bmp.GetHicon());
}
// ── Overlay ─────────────────────────────────────────────────────
private void SetupOverlay()
{
_overlay = new OverlayWindow(); _overlay.SetGridConfig(_gridConfig);
}
private void ApplyGridConfig(GridConfig config)
{
_gridConfig = config;
_overlay?.SetGridConfig(config);
}
// ── Grid Editor ─────────────────────────────────────────────────────
private GridEditorWindow? _editorWindow;
private void ShowGridEditor()
{
if (_editorWindow != null && _editorWindow.IsVisible)
{
_editorWindow.Activate();
return;
}
_editorWindow = new GridEditorWindow(_gridConfig);
_editorWindow.Applied += config => ApplyGridConfig(config);
_editorWindow.Closed += (_, _) => _editorWindow = null;
_editorWindow.Show();
}
private void LoadGridFromFile()
{
var dlg = new Microsoft.Win32.OpenFileDialog
{
Filter = GridConfig.FileFilter,
DefaultExt = GridConfig.FileExtension,
InitialDirectory = GridConfig.GetAppDataFolder()
};
if (dlg.ShowDialog() == true)
{
try
{
var config = GridConfig.LoadFromFile(dlg.FileName);
config.SaveAsActive();
ApplyGridConfig(config);
MessageBox.Show($"Loaded grid: {config.Name}\n({config.Rows.Count} rows)",
"Grid Loaded", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load grid file:\n{ex.Message}",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void ResetGridToDefaults()
{
var result = MessageBox.Show(
"Reset the grid layout to the built-in defaults?\n\n" +
"This will restore default pairs (full-height + ½H variants).\n" +
"Your current layout will be replaced.",
"Reset to Defaults",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
var config = GridConfig.CreateDefault();
config.SaveAsActive();
ApplyGridConfig(config);
}
}
// ── Win-Event Hook (detect window drag start / end) ─────────────
private void SetupWinEventHook()
{
_winEventDelegate = WinEventCallback;
_winEventHook = NativeMethods.SetWinEventHook(
NativeMethods.EVENT_SYSTEM_MOVESIZESTART,
NativeMethods.EVENT_SYSTEM_MOVESIZEEND,
IntPtr.Zero,
_winEventDelegate,
0, 0,
NativeMethods.WINEVENT_OUTOFCONTEXT | NativeMethods.WINEVENT_SKIPOWNPROCESS);
}
private void WinEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
try
{
if (eventType == NativeMethods.EVENT_SYSTEM_MOVESIZESTART)
{
// Only activate for normal application windows (title bar, visible, not games/tools)
if (!IsNormalAppWindow(hwnd))
return;
_isDragging = true;
_draggedWindowHandle = hwnd;
_mouseTracker?.Start();
// If Shift is already held, show overlay immediately
if (IsShiftHeld())
ShowOverlayOnCurrentMonitor();
}
else if (eventType == NativeMethods.EVENT_SYSTEM_MOVESIZEEND)
{
if (_overlayVisible)
{
SnapWindowToHighlightedZone();
HideOverlay();
}
_isDragging = false;
_mouseTracker?.Stop();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"WinEventCallback error: {ex.Message}");
}
}
// ── Mouse tracker (polls cursor pos + Shift state at ~60fps) ────
private void SetupMouseTracker()
{
_mouseTracker = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(16)
};
_mouseTracker.Tick += OnMouseTrackerTick;
}
private void SetupMouseWheelHook()
{
_mouseHookProc = MouseHookCallback;
_mouseHook = NativeMethods.SetWindowsHookEx(
NativeMethods.WH_MOUSE_LL,
_mouseHookProc,
IntPtr.Zero,
0);
}
private IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0
&& wParam.ToInt32() == NativeMethods.WM_MOUSEWHEEL
&& _isDragging
&& _overlayVisible
&& IsShiftHeld()
&& _overlay != null)
{
var data = Marshal.PtrToStructure<NativeMethods.MSLLHOOKSTRUCT>(lParam);
short delta = unchecked((short)((data.mouseData >> 16) & 0xFFFF));
int direction = delta > 0 ? -1 : 1;
Dispatcher.BeginInvoke(new Action(() =>
{
_overlay?.ScrollVariantPair(direction);
}));
}
return NativeMethods.CallNextHookEx(_mouseHook, nCode, wParam, lParam);
}
private void OnMouseTrackerTick(object? sender, EventArgs e)
{
if (!_isDragging)
{
_mouseTracker?.Stop();
return;
}
bool shiftHeld = IsShiftHeld();
// Show overlay when Shift is pressed mid-drag
if (shiftHeld && !_overlayVisible)
{
ShowOverlayOnCurrentMonitor();
return;
}
// Hide overlay when Shift is released mid-drag
if (!shiftHeld && _overlayVisible)
{
HideOverlay();
return;
}
// Update zone highlight
if (_overlayVisible && _overlay != null)
{
NativeMethods.GetCursorPos(out var pos);
var zone = _overlay.GetZoneAtPoint(pos.X, pos.Y);
_overlay.HighlightZone(zone);
}
}
// ── Overlay management ──────────────────────────────────────────
private void ShowOverlayOnCurrentMonitor()
{
if (_overlayVisible || _overlay == null) return;
NativeMethods.GetCursorPos(out var cursorPos);
var hMonitor = NativeMethods.MonitorFromPoint(cursorPos, NativeMethods.MONITOR_DEFAULTTONEAREST);
var mi = new NativeMethods.MONITORINFO
{
cbSize = Marshal.SizeOf<NativeMethods.MONITORINFO>()
};
if (!NativeMethods.GetMonitorInfo(hMonitor, ref mi))
return;
_overlay.ResetVariantPair();
_overlay.SetupForScreen(mi.rcWork);
_overlay.ShowOverlay();
_overlayVisible = true;
}
private void HideOverlay()
{
_overlayVisible = false; // Immediately mark as hidden
_overlay?.HideOverlay();
}
// ── Window snapping ─────────────────────────────────────────────
private void SnapWindowToHighlightedZone()
{
if (_overlay == null) return;
var zone = _overlay.GetHighlightedZone();
if (zone == null || _draggedWindowHandle == IntPtr.Zero) return;
IntPtr hwnd = _draggedWindowHandle;
// Validate the window handle is still valid
if (!NativeMethods.IsWindow(hwnd))
{
System.Diagnostics.Debug.WriteLine("SnapWindow: handle is no longer valid");
return;
}
// If the window is maximized, restore it first so MoveWindow/SetWindowPos works
if (NativeMethods.IsZoomed(hwnd))
{
NativeMethods.ShowWindow(hwnd, NativeMethods.SW_RESTORE);
// Small delay to let the window finish restoring
System.Threading.Thread.Sleep(50);
}
int x = (int)zone.SnapBounds.X;
int y = (int)zone.SnapBounds.Y;
int w = (int)zone.SnapBounds.Width;
int h = (int)zone.SnapBounds.Height;
// Compensate for Windows 10/11 invisible borders (DWM extended frame)
try
{
int hr = NativeMethods.DwmGetWindowAttribute(
hwnd,
NativeMethods.DWMWA_EXTENDED_FRAME_BOUNDS,
out NativeMethods.RECT frameRect,
Marshal.SizeOf<NativeMethods.RECT>());
if (hr == 0)
{
NativeMethods.GetWindowRect(hwnd, out NativeMethods.RECT windowRect);
int borderL = frameRect.Left - windowRect.Left;
int borderT = frameRect.Top - windowRect.Top;
int borderR = windowRect.Right - frameRect.Right;
int borderB = windowRect.Bottom - frameRect.Bottom;
x -= borderL;
y -= borderT;
w += borderL + borderR;
h += borderT + borderB;
}
}
catch
{
// Fallback: just use raw zone bounds
}
// Try MoveWindow first, then SetWindowPos as fallback
if (!NativeMethods.MoveWindow(hwnd, x, y, w, h, true))
{
System.Diagnostics.Debug.WriteLine("MoveWindow failed, trying SetWindowPos");
NativeMethods.SetWindowPos(hwnd, IntPtr.Zero, x, y, w, h,
NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_FRAMECHANGED);
}
}
// ── Helpers ─────────────────────────────────────────────────────
private static bool IsShiftHeld()
{
return (NativeMethods.GetAsyncKeyState(NativeMethods.VK_SHIFT) & 0x8000) != 0;
}
/// <summary>
/// Returns true only for normal resizable app windows with a title bar.
/// Rejects fullscreen/exclusive games, tool windows, invisible windows, etc.
/// </summary>
private static bool IsNormalAppWindow(IntPtr hwnd)
{
if (hwnd == IntPtr.Zero || !NativeMethods.IsWindow(hwnd))
return false;
if (!NativeMethods.IsWindowVisible(hwnd))
return false;
int style = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);
int exStyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_EXSTYLE);
// Reject tool windows (small floating palettes) unless they also have WS_EX_APPWINDOW
bool isToolWindow = (exStyle & NativeMethods.WS_EX_TOOLWINDOW) != 0;
bool isAppWindow = (exStyle & NativeMethods.WS_EX_APPWINDOW) != 0;
if (isToolWindow && !isAppWindow)
return false;
// Reject known fullscreen game/overlay class names
var sb = new System.Text.StringBuilder(256);
NativeMethods.GetClassName(hwnd, sb, 256);
string className = sb.ToString();
// Common fullscreen game / overlay classes to reject
string[] blockedClasses = {
"UnityWndClass", "UnrealWindow", "LaunchUnrealUWindowsClient",
"SDL_app", "GLFW30", "ConsoleWindowClass",
"CryENGINE", "Source Engine", "Valve001"
};
foreach (var blocked in blockedClasses)
{
if (className.Equals(blocked, StringComparison.OrdinalIgnoreCase))
return false;
}
// Reject windows covering the entire monitor (borderless fullscreen games)
NativeMethods.GetWindowRect(hwnd, out var wndRect);
var pt = new NativeMethods.POINT { X = wndRect.Left + 1, Y = wndRect.Top + 1 };
var hMon = NativeMethods.MonitorFromPoint(pt, NativeMethods.MONITOR_DEFAULTTONEAREST);
var mi = new NativeMethods.MONITORINFO { cbSize = Marshal.SizeOf<NativeMethods.MONITORINFO>() };
if (NativeMethods.GetMonitorInfo(hMon, ref mi))
{
var mon = mi.rcMonitor;
bool coversFullScreen =
wndRect.Left <= mon.Left &&
wndRect.Top <= mon.Top &&
wndRect.Right >= mon.Right &&
wndRect.Bottom >= mon.Bottom;
// A true maximized window is fine (it sits in the work area),
// but a borderless fullscreen window covers the whole monitor
if (coversFullScreen && !NativeMethods.IsZoomed(hwnd))
return false;
}
return true;
}
// ── Shutdown ────────────────────────────────────────────────────
private void ExitApp()
{
if (_winEventHook != IntPtr.Zero)
{
NativeMethods.UnhookWinEvent(_winEventHook);
_winEventHook = IntPtr.Zero;
}
if (_mouseHook != IntPtr.Zero)
{
NativeMethods.UnhookWindowsHookEx(_mouseHook);
_mouseHook = IntPtr.Zero;
}
_mouseTracker?.Stop();
_trayIcon?.Dispose();
_trayIcon = null;
_overlay?.Close();
Shutdown();
}
protected override void OnExit(ExitEventArgs e)
{
if (_mouseHook != IntPtr.Zero)
{
NativeMethods.UnhookWindowsHookEx(_mouseHook);
_mouseHook = IntPtr.Zero;
}
_trayIcon?.Dispose();
base.OnExit(e);
}
}
}