-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWindowHelper.cs
More file actions
279 lines (231 loc) · 11.4 KB
/
WindowHelper.cs
File metadata and controls
279 lines (231 loc) · 11.4 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
using System.Runtime.InteropServices;
using Microsoft.UI.Input;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using WinRT.Interop;
namespace NoteUI;
internal static class WindowHelper
{
private const int GWL_STYLE = -16;
private const long WS_BORDER = 0x00800000;
private const long WS_THICKFRAME = 0x00040000;
private const long WS_DLGFRAME = 0x00400000;
private const uint SWP_FRAMECHANGED = 0x0020;
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOZORDER = 0x0004;
private const int WM_NCCALCSIZE = 0x0083;
private const int WM_NCHITTEST = 0x0084;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int ResizeBorder = 6;
public static void RemoveWindowBorder(Window window)
{
var hwnd = WindowNative.GetWindowHandle(window);
var style = (long)GetWindowLongPtr(hwnd, GWL_STYLE);
style &= ~(WS_BORDER | WS_DLGFRAME);
SetWindowLongPtr(hwnd, GWL_STYLE, (IntPtr)style);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
/// <summary>
/// Removes the visible non-client frame while keeping WS_THICKFRAME for resize.
/// Subclasses the window to zero out the non-client area via WM_NCCALCSIZE
/// and handle WM_NCHITTEST for resize edges.
/// </summary>
public static void RemoveWindowBorderKeepResize(Window window)
{
var hwnd = WindowNative.GetWindowHandle(window);
// Remove caption styles but keep WS_THICKFRAME for resize
var style = (long)GetWindowLongPtr(hwnd, GWL_STYLE);
style &= ~(WS_BORDER | WS_DLGFRAME);
SetWindowLongPtr(hwnd, GWL_STYLE, (IntPtr)style);
// Subclass to zero out non-client area and handle resize hit-testing
var subclassProc = new SubclassProc(BorderlessResizeProc);
_pinnedDelegates.Add(subclassProc); // prevent GC
SetWindowSubclass(hwnd, subclassProc, IntPtr.Zero, IntPtr.Zero);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
private static IntPtr BorderlessResizeProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData)
{
if (uMsg == WM_NCCALCSIZE && wParam != IntPtr.Zero)
return IntPtr.Zero; // non-client area = 0 → no visible frame
if (uMsg == WM_NCHITTEST)
{
// Extract cursor position from lParam
int x = (short)(lParam.ToInt64() & 0xFFFF);
int y = (short)((lParam.ToInt64() >> 16) & 0xFFFF);
GetWindowRect(hWnd, out var rect);
bool left = x >= rect.Left && x < rect.Left + ResizeBorder;
bool right = x < rect.Right && x >= rect.Right - ResizeBorder;
bool top = y >= rect.Top && y < rect.Top + ResizeBorder;
bool bottom = y < rect.Bottom && y >= rect.Bottom - ResizeBorder;
if (top && left) return (IntPtr)HTTOPLEFT;
if (top && right) return (IntPtr)HTTOPRIGHT;
if (bottom && left) return (IntPtr)HTBOTTOMLEFT;
if (bottom && right) return (IntPtr)HTBOTTOMRIGHT;
if (left) return (IntPtr)HTLEFT;
if (right) return (IntPtr)HTRIGHT;
if (top) return (IntPtr)HTTOP;
if (bottom) return (IntPtr)HTBOTTOM;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT { public int Left, Top, Right, Bottom; }
// prevent delegate from being garbage-collected
private static readonly List<SubclassProc> _pinnedDelegates = [];
public static void CenterOnScreen(Window window)
{
var appWindow = window.AppWindow;
var display = DisplayArea.GetFromWindowId(appWindow.Id, DisplayAreaFallback.Primary);
var workArea = display.WorkArea;
var x = (workArea.Width - appWindow.Size.Width) / 2 + workArea.X;
var y = (workArea.Height - appWindow.Size.Height) / 2 + workArea.Y;
appWindow.Move(new Windows.Graphics.PointInt32(x, y));
}
public static void MoveToBottomRight(Window window, int margin = 12)
{
var appWindow = window.AppWindow;
var display = DisplayArea.GetFromWindowId(appWindow.Id, DisplayAreaFallback.Primary);
var workArea = display.WorkArea;
var x = workArea.X + Math.Max(0, workArea.Width - appWindow.Size.Width - margin);
var y = workArea.Y + Math.Max(0, workArea.Height - appWindow.Size.Height - margin);
appWindow.Move(new Windows.Graphics.PointInt32(x, y));
}
public static void MoveToVisibleArea(Window window, int x, int y)
{
var appWindow = window.AppWindow;
var display = DisplayArea.GetFromWindowId(appWindow.Id, DisplayAreaFallback.Primary);
var workArea = display.WorkArea;
var minX = workArea.X;
var minY = workArea.Y;
var maxX = workArea.X + Math.Max(0, workArea.Width - appWindow.Size.Width);
var maxY = workArea.Y + Math.Max(0, workArea.Height - appWindow.Size.Height);
var clampedX = Math.Clamp(x, minX, maxX);
var clampedY = Math.Clamp(y, minY, maxY);
appWindow.Move(new Windows.Graphics.PointInt32(clampedX, clampedY));
}
/// <summary>
/// Adds invisible XAML resize grips on left, right, bottom edges and corners.
/// Uses pointer capture (same pattern as DragBar) for native-feeling resize.
/// Top edge is handled by ExtendsContentIntoTitleBar / WM_NCHITTEST.
/// </summary>
public static void AddResizeGrips(Window window)
{
if (window.Content is not Grid rootGrid) return;
const int grip = 6;
const int cornerGrip = 12;
int rowSpan = Math.Max(1, rootGrid.RowDefinitions.Count);
int colSpan = Math.Max(1, rootGrid.ColumnDefinitions.Count);
// Resize directions: left=-1/0, right=+1/0, top=0/-1, bottom=0/+1
void AddGrip(HorizontalAlignment hAlign, VerticalAlignment vAlign,
double width, double height,
int dirX, int dirY, InputSystemCursorShape cursorShape)
{
var grip = new ResizeGrip(cursorShape)
{
HorizontalAlignment = hAlign,
VerticalAlignment = vAlign,
};
if (!double.IsNaN(width)) grip.Width = width;
if (!double.IsNaN(height)) grip.Height = height;
Grid.SetRowSpan(grip, rowSpan);
if (colSpan > 1) Grid.SetColumnSpan(grip, colSpan);
bool dragging = false;
POINT startCursor = default;
Windows.Graphics.PointInt32 startPos = default;
Windows.Graphics.SizeInt32 startSize = default;
var appWindow = window.AppWindow;
grip.PointerPressed += (s, e) =>
{
dragging = true;
GetCursorPos(out startCursor);
startPos = appWindow.Position;
startSize = appWindow.Size;
((UIElement)s!).CapturePointer(e.Pointer);
e.Handled = true;
};
grip.PointerMoved += (_, e) =>
{
if (!dragging) return;
GetCursorPos(out var current);
int dx = current.X - startCursor.X;
int dy = current.Y - startCursor.Y;
int newX = startPos.X;
int newY = startPos.Y;
int newW = startSize.Width;
int newH = startSize.Height;
if (dirX < 0) { newX = startPos.X + dx; newW = startSize.Width - dx; }
if (dirX > 0) { newW = startSize.Width + dx; }
if (dirY < 0) { newY = startPos.Y + dy; newH = startSize.Height - dy; }
if (dirY > 0) { newH = startSize.Height + dy; }
const int minW = 200, minH = 100;
if (newW < minW) { if (dirX < 0) newX = startPos.X + startSize.Width - minW; newW = minW; }
if (newH < minH) { if (dirY < 0) newY = startPos.Y + startSize.Height - minH; newH = minH; }
appWindow.MoveAndResize(new Windows.Graphics.RectInt32(newX, newY, newW, newH));
e.Handled = true;
};
grip.PointerReleased += (s, e) =>
{
dragging = false;
((UIElement)s!).ReleasePointerCapture(e.Pointer);
e.Handled = true;
};
rootGrid.Children.Add(grip);
}
// Edge grips (dirX, dirY)
AddGrip(HorizontalAlignment.Left, VerticalAlignment.Stretch, grip, double.NaN, -1, 0, InputSystemCursorShape.SizeWestEast);
AddGrip(HorizontalAlignment.Right, VerticalAlignment.Stretch, grip, double.NaN, +1, 0, InputSystemCursorShape.SizeWestEast);
AddGrip(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, double.NaN, grip, 0, +1, InputSystemCursorShape.SizeNorthSouth);
// Corner grips (larger, added after edges for higher z-order)
AddGrip(HorizontalAlignment.Left, VerticalAlignment.Bottom, cornerGrip, cornerGrip, -1, +1, InputSystemCursorShape.SizeNortheastSouthwest);
AddGrip(HorizontalAlignment.Right, VerticalAlignment.Bottom, cornerGrip, cornerGrip, +1, +1, InputSystemCursorShape.SizeNorthwestSoutheast);
}
/// <summary>Panel that sets the resize cursor via ProtectedCursor.</summary>
private sealed class ResizeGrip : Grid
{
public ResizeGrip(InputSystemCursorShape cursorShape)
{
ProtectedCursor = InputSystemCursor.Create(cursorShape);
Background = new SolidColorBrush(Microsoft.UI.Colors.Transparent);
}
}
[DllImport("user32.dll")]
private static extern bool GetCursorPos(out POINT lpPoint);
[StructLayout(LayoutKind.Sequential)]
private struct POINT { public int X, Y; }
public static OverlappedPresenter GetOverlappedPresenter(Window window)
{
var appWindow = window.AppWindow;
if (appWindow.Presenter is OverlappedPresenter existing)
return existing;
var presenter = OverlappedPresenter.Create();
appWindow.SetPresenter(presenter);
return presenter;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
private delegate IntPtr SubclassProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData);
[DllImport("comctl32.dll")]
private static extern bool SetWindowSubclass(IntPtr hWnd, SubclassProc pfnSubclass, IntPtr uIdSubclass, IntPtr dwRefData);
[DllImport("comctl32.dll")]
private static extern IntPtr DefSubclassProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
}