-
Notifications
You must be signed in to change notification settings - Fork 214
/
WindowResizer.cs
529 lines (439 loc) · 19.1 KB
/
WindowResizer.cs
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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
namespace Fasetto.Word
{
/// <summary>
/// The dock position of the window
/// </summary>
public enum WindowDockPosition
{
/// <summary>
/// Not docked
/// </summary>
Undocked = 0,
/// <summary>
/// Docked to the left of the screen
/// </summary>
Left = 1,
/// <summary>
/// Docked to the right of the screen
/// </summary>
Right = 2,
/// <summary>
/// Docked to the top/bottom of the screen
/// </summary>
TopBottom = 3,
/// <summary>
/// Docked to the top-left of the screen
/// </summary>
TopLeft = 4,
/// <summary>
/// Docked to the top-right of the screen
/// </summary>
TopRight = 5,
/// <summary>
/// Docked to the bottom-left of the screen
/// </summary>
BottomLeft = 6,
/// <summary>
/// Docked to the bottom-right of the screen
/// </summary>
BottomRight = 7,
}
/// <summary>
/// Fixes the issue with Windows of Style <see cref="WindowStyle.None"/> covering the taskbar
/// </summary>
public class WindowResizer
{
#region Private Members
/// <summary>
/// The window to handle the resizing for
/// </summary>
private Window mWindow;
/// <summary>
/// The last calculated available screen size
/// </summary>
private Rect mScreenSize = new Rect();
/// <summary>
/// How close to the edge the window has to be to be detected as at the edge of the screen
/// </summary>
private int mEdgeTolerance = 1;
/// <summary>
/// The transform matrix used to convert WPF sizes to screen pixels
/// </summary>
private DpiScale? mMonitorDpi;
/// <summary>
/// The last screen the window was on
/// </summary>
private IntPtr mLastScreen;
/// <summary>
/// The last known dock position
/// </summary>
private WindowDockPosition mLastDock = WindowDockPosition.Undocked;
/// <summary>
/// A flag indicating if the window is currently being moved/dragged
/// </summary>
private bool mBeingMoved = false;
#endregion
#region DLL Imports
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out POINT lpPoint);
[DllImport("user32.dll")]
static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr MonitorFromPoint(POINT pt, MonitorOptions dwFlags);
[DllImport("user32.dll")]
static extern IntPtr MonitorFromWindow(IntPtr hwnd, MonitorOptions dwFlags);
#endregion
#region Public Events
/// <summary>
/// Called when the window dock position changes
/// </summary>
public event Action<WindowDockPosition> WindowDockChanged = (dock) => { };
/// <summary>
/// Called when the window starts being moved/dragged
/// </summary>
public event Action WindowStartedMove = () => { };
/// <summary>
/// Called when the window has been moved/dragged and then finished
/// </summary>
public event Action WindowFinishedMove = () => { };
#endregion
#region Public Properties
/// <summary>
/// The size and position of the current monitor the window is on
/// </summary>
public Rectangle CurrentMonitorSize { get; set; } = new Rectangle();
/// <summary>
/// The margin around the window for the current window to compensate for any non-usable area
/// such as the task bar
/// </summary>
public Thickness CurrentMonitorMargin { get; private set; } = new Thickness();
/// <summary>
/// The size and position of the current screen in relation to the multi-screen desktop
/// For example a second monitor on the right will have a Left position of
/// the X resolution of the screens on the left
/// </summary>
public Rect CurrentScreenSize => mScreenSize;
#endregion
#region Constructor
/// <summary>
/// Default constructor
/// </summary>
/// <param name="window">The window to monitor and correctly maximize</param>
/// <param name="adjustSize">The callback for the host to adjust the maximum available size if needed</param>
public WindowResizer(Window window)
{
mWindow = window;
// Listen out for source initialized to setup
mWindow.SourceInitialized += Window_SourceInitialized;
// Monitor for edge docking
mWindow.SizeChanged += Window_SizeChanged;
mWindow.LocationChanged += Window_LocationChanged;
}
#endregion
#region Initialize
/// <summary>
/// Initialize and hook into the windows message pump
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_SourceInitialized(object sender, System.EventArgs e)
{
// Get the handle of this window
var handle = (new WindowInteropHelper(mWindow)).Handle;
var handleSource = HwndSource.FromHwnd(handle);
// If not found, end
if (handleSource == null)
return;
// Hook into it's Windows messages
handleSource.AddHook(WindowProc);
}
#endregion
#region Edge Docking
/// <summary>
/// Monitor for moving of the window and constantly check for docked positions
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_LocationChanged(object sender, EventArgs e)
{
Window_SizeChanged(null, null);
}
/// <summary>
/// Monitors for size changes and detects if the window has been docked (Aero snap) to an edge
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
// Make sure our monitor info is up-to-date
WmGetMinMaxInfo(IntPtr.Zero, IntPtr.Zero);
// Get the monitor transform for the current position
mMonitorDpi = VisualTreeHelper.GetDpi(mWindow);
// Cannot calculate size until we know monitor scale
if (mMonitorDpi == null)
return;
// Get window rectangle
var top = mWindow.Top;
var left = mWindow.Left;
var bottom = top + mWindow.Height;
var right = left + mWindow.Width;
// Get window position/size in device pixels
var windowTopLeft = new Point(left * mMonitorDpi.Value.DpiScaleX, top * mMonitorDpi.Value.DpiScaleX);
var windowBottomRight = new Point(right * mMonitorDpi.Value.DpiScaleX, bottom * mMonitorDpi.Value.DpiScaleX);
// Check for edges docked
var edgedTop = windowTopLeft.Y <= (mScreenSize.Top + mEdgeTolerance) && windowTopLeft.Y >= (mScreenSize.Top - mEdgeTolerance);
var edgedLeft = windowTopLeft.X <= (mScreenSize.Left + mEdgeTolerance) && windowTopLeft.X >= (mScreenSize.Left - mEdgeTolerance);
var edgedBottom = windowBottomRight.Y >= (mScreenSize.Bottom - mEdgeTolerance) && windowBottomRight.Y <= (mScreenSize.Bottom + mEdgeTolerance);
var edgedRight = windowBottomRight.X >= (mScreenSize.Right - mEdgeTolerance) && windowBottomRight.X <= (mScreenSize.Right + mEdgeTolerance);
// Get docked position
var dock = WindowDockPosition.Undocked;
// Left docking
if (edgedTop && edgedBottom && edgedLeft)
dock = WindowDockPosition.Left;
// Right docking
else if (edgedTop && edgedBottom && edgedRight)
dock = WindowDockPosition.Right;
// Top/bottom
else if (edgedTop && edgedBottom)
dock = WindowDockPosition.TopBottom;
// Top-left
else if (edgedTop && edgedLeft)
dock = WindowDockPosition.TopLeft;
// Top-right
else if (edgedTop && edgedRight)
dock = WindowDockPosition.TopRight;
// Bottom-left
else if (edgedBottom && edgedLeft)
dock = WindowDockPosition.BottomLeft;
// Bottom-right
else if (edgedBottom && edgedRight)
dock = WindowDockPosition.BottomRight;
// None
else
dock = WindowDockPosition.Undocked;
// If dock has changed
if (dock != mLastDock)
// Inform listeners
WindowDockChanged(dock);
// Save last dock position
mLastDock = dock;
}
#endregion
#region Windows Message Pump
/// <summary>
/// Listens out for all windows messages for this window
/// </summary>
/// <param name="hwnd"></param>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <param name="handled"></param>
/// <returns></returns>
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
// Handle the GetMinMaxInfo of the Window
case 0x0024: // WM_GETMINMAXINFO
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
// Once the window starts being moved
case 0x0231: // WM_ENTERSIZEMOVE
mBeingMoved = true;
WindowStartedMove();
break;
// Once the window has finished being moved
case 0x0232: // WM_EXITSIZEMOVE
mBeingMoved = false;
WindowFinishedMove();
break;
}
return (IntPtr)0;
}
#endregion
/// <summary>
/// Get the min/max window size for this window
/// Correctly accounting for the task bar size and position
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
private void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
{
// Get the point position to determine what screen we are on
GetCursorPos(out var lMousePosition);
// Now get the current screen
var lCurrentScreen = mBeingMoved ?
// If being dragged get it from the mouse position
MonitorFromPoint(lMousePosition, MonitorOptions.MONITOR_DEFAULTTONULL) :
// Otherwise get it from the window position (for example being moved via Win + Arrow)
// in case the mouse is on another monitor
MonitorFromWindow(hwnd, MonitorOptions.MONITOR_DEFAULTTONULL);
var lPrimaryScreen = MonitorFromPoint(new POINT(0,0), MonitorOptions.MONITOR_DEFAULTTOPRIMARY);
// Try and get the current screen information
var lCurrentScreenInfo = new MONITORINFO();
if (GetMonitorInfo(lCurrentScreen, lCurrentScreenInfo) == false)
return;
// Try and get the primary screen information
var lPrimaryScreenInfo = new MONITORINFO();
if (GetMonitorInfo(lPrimaryScreen, lPrimaryScreenInfo) == false)
return;
// NOTE: Always update it
// If this has changed from the last one, update the transform
//if (lCurrentScreen != mLastScreen || mMonitorDpi == null)
mMonitorDpi = VisualTreeHelper.GetDpi(mWindow);
// Store last know screen
mLastScreen = lCurrentScreen;
// Get work area sizes and rations
var currentX = lCurrentScreenInfo.RCWork.Left - lCurrentScreenInfo.RCMonitor.Left;
var currentY = lCurrentScreenInfo.RCWork.Top - lCurrentScreenInfo.RCMonitor.Top;
var currentWidth = (lCurrentScreenInfo.RCWork.Right - lCurrentScreenInfo.RCWork.Left);
var currentHeight = (lCurrentScreenInfo.RCWork.Bottom - lCurrentScreenInfo.RCWork.Top);
var currentRatio = (float)currentWidth / (float)currentHeight;
var primaryX = lPrimaryScreenInfo.RCWork.Left - lPrimaryScreenInfo.RCMonitor.Left;
var primaryY = lPrimaryScreenInfo.RCWork.Top - lPrimaryScreenInfo.RCMonitor.Top;
var primaryWidth = (lPrimaryScreenInfo.RCWork.Right - lPrimaryScreenInfo.RCWork.Left);
var primaryHeight = (lPrimaryScreenInfo.RCWork.Bottom - lPrimaryScreenInfo.RCWork.Top);
var primaryRatio = (float)primaryWidth / (float)primaryHeight;
if (lParam != IntPtr.Zero)
{
// Get min/max structure to fill with information
var lMmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
//
// NOTE: The below setting of max sizes we no longer do
// as through observations, it appears Windows works
// correctly only when the max window size is set to
// EXACTLY the size of the primary window
//
// Anything else and the behavior is wrong and the max
// window width on a secondary monitor if larger than the
// primary then goes too large
//
// lMmi.PointMaxPosition.X = 0;
// lMmi.PointMaxPosition.Y = 0;
// lMmi.PointMaxSize.X = lCurrentScreenInfo.RCMonitor.Right - lCurrentScreenInfo.RCMonitor.Left;
// lMmi.PointMaxSize.Y = lCurrentScreenInfo.RCMonitor.Bottom - lCurrentScreenInfo.RCMonitor.Top;
//
// Instead we now just add a margin to the window itself
// to compensate when maximized
//
//
// NOTE: rcMonitor is the monitor size
// rcWork is the available screen size (so the area inside the task bar start menu for example)
// Size limits (used by Windows when maximized)
// relative to 0,0 being the current screens top-left corner
// Set to primary monitor size
lMmi.PointMaxPosition.X = lPrimaryScreenInfo.RCMonitor.Left;
lMmi.PointMaxPosition.Y = lPrimaryScreenInfo.RCMonitor.Top;
lMmi.PointMaxSize.X = lPrimaryScreenInfo.RCMonitor.Right;
lMmi.PointMaxSize.Y = lPrimaryScreenInfo.RCMonitor.Bottom;
// Set min size
var minSize = new Point(mWindow.MinWidth * mMonitorDpi.Value.DpiScaleX, mWindow.MinHeight * mMonitorDpi.Value.DpiScaleX);
lMmi.PointMinTrackSize.X = (int)minSize.X;
lMmi.PointMinTrackSize.Y = (int)minSize.Y;
// Now we have the max size, allow the host to tweak as needed
Marshal.StructureToPtr(lMmi, lParam, true);
}
// Set monitor size
CurrentMonitorSize = new Rectangle(currentX, currentY, currentWidth + currentX, currentHeight + currentY);
// Get margin around window
CurrentMonitorMargin = new Thickness(
(lCurrentScreenInfo.RCWork.Left - lCurrentScreenInfo.RCMonitor.Left) / mMonitorDpi.Value.DpiScaleX,
(lCurrentScreenInfo.RCWork.Top - lCurrentScreenInfo.RCMonitor.Top) / mMonitorDpi.Value.DpiScaleY,
(lCurrentScreenInfo.RCMonitor.Right - lCurrentScreenInfo.RCWork.Right) / mMonitorDpi.Value.DpiScaleX,
(lCurrentScreenInfo.RCMonitor.Bottom - lCurrentScreenInfo.RCWork.Bottom) / mMonitorDpi.Value.DpiScaleY
);
// Store new size
mScreenSize = new Rect(lCurrentScreenInfo.RCWork.Left, lCurrentScreenInfo.RCWork.Top, currentWidth, currentHeight);
}
/// <summary>
/// Gets the current cursor position in screen coordinates relative to an entire multi-desktop position
/// </summary>
/// <returns></returns>
public Point GetCursorPosition()
{
// Get mouse position
GetCursorPos(out var lMousePosition);
// Apply DPI scaling
return new Point(lMousePosition.X / mMonitorDpi.Value.DpiScaleX, lMousePosition.Y / mMonitorDpi.Value.DpiScaleY);
}
}
#region DLL Helper Structures
public enum MonitorOptions : uint
{
MONITOR_DEFAULTTONULL = 0x00000000,
MONITOR_DEFAULTTOPRIMARY = 0x00000001,
MONITOR_DEFAULTTONEAREST = 0x00000002
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
#pragma warning disable IDE1006 // Naming Styles
public int CBSize = Marshal.SizeOf(typeof(MONITORINFO));
public Rectangle RCMonitor = new Rectangle();
public Rectangle RCWork = new Rectangle();
public int DWFlags = 0;
#pragma warning restore IDE1006 // Naming Styles
}
[StructLayout(LayoutKind.Sequential)]
public struct Rectangle
{
#pragma warning disable IDE1006 // Naming Styles
public int Left, Top, Right, Bottom;
#pragma warning restore IDE1006 // Naming Styles
public Rectangle(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
#pragma warning disable IDE1006 // Naming Styles
public POINT PointReserved;
public POINT PointMaxSize;
public POINT PointMaxPosition;
public POINT PointMinTrackSize;
public POINT PointMaxTrackSize;
#pragma warning restore IDE1006 // Naming Styles
};
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// x coordinate of point.
/// </summary>
#pragma warning disable IDE1006 // Naming Styles
public int X;
#pragma warning restore IDE1006 // Naming Styles
/// <summary>
/// y coordinate of point.
/// </summary>
#pragma warning disable IDE1006 // Naming Styles
public int Y;
#pragma warning restore IDE1006 // Naming Styles
/// <summary>
/// Construct a point of coordinates (x,y).
/// </summary>
public POINT(int x, int y)
{
X = x;
Y = y;
}
public override string ToString()
{
return $"{X} {Y}";
}
}
#endregion
}