解决方案

Solution A

命名空间:xmlns:ab="clr-namespace:WpfAppBar"

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
550
551
552
553
554
555
556
557
558
559
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace WpfAppBar
{
using System.Windows.Media;
using static WpfAppBar.NativeMethods;

public class AppBarWindow : Window
{
private bool IsAppBarRegistered;
private bool IsInAppBarResize;

static AppBarWindow()
{
ShowInTaskbarProperty.OverrideMetadata(typeof(AppBarWindow), new FrameworkPropertyMetadata(false));
MinHeightProperty.OverrideMetadata(typeof(AppBarWindow), new FrameworkPropertyMetadata(20d, MinMaxHeightWidth_Changed));
MinWidthProperty.OverrideMetadata(typeof(AppBarWindow), new FrameworkPropertyMetadata(20d, MinMaxHeightWidth_Changed));
MaxHeightProperty.OverrideMetadata(typeof(AppBarWindow), new FrameworkPropertyMetadata(MinMaxHeightWidth_Changed));
MaxWidthProperty.OverrideMetadata(typeof(AppBarWindow), new FrameworkPropertyMetadata(MinMaxHeightWidth_Changed));
}

public AppBarWindow()
{
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
Topmost = true;
}

public AppBarDockMode DockMode
{
get { return (AppBarDockMode)GetValue(DockModeProperty); }
set { SetValue(DockModeProperty, value); }
}

public static readonly DependencyProperty DockModeProperty = DependencyProperty.Register(
"DockMode",
typeof(AppBarDockMode),
typeof(AppBarWindow),
new FrameworkPropertyMetadata(AppBarDockMode.Left, DockLocation_Changed)
);

public MonitorInfo Monitor
{
get { return (MonitorInfo)GetValue(MonitorProperty); }
set { SetValue(MonitorProperty, value); }
}

public static readonly DependencyProperty MonitorProperty = DependencyProperty.Register(
"Monitor",
typeof(MonitorInfo),
typeof(AppBarWindow),
new FrameworkPropertyMetadata(null, DockLocation_Changed)
);

public int DockedWidthOrHeight
{
get { return (int)GetValue(DockedWidthOrHeightProperty); }
set { SetValue(DockedWidthOrHeightProperty, value); }
}

public static readonly DependencyProperty DockedWidthOrHeightProperty =
DependencyProperty.Register(
"DockedWidthOrHeight",
typeof(int),
typeof(AppBarWindow),
new FrameworkPropertyMetadata(200, DockLocation_Changed, DockedWidthOrHeight_Coerce)
);

private static object DockedWidthOrHeight_Coerce(DependencyObject d, object baseValue)
{
var @this = (AppBarWindow)d;
var newValue = (int)baseValue;
return @this.DockMode switch
{
AppBarDockMode.Left or AppBarDockMode.Right => BoundIntToDouble(newValue, @this.MinWidth, @this.MaxWidth),
AppBarDockMode.Top or AppBarDockMode.Bottom => (object)BoundIntToDouble(newValue, @this.MinHeight, @this.MaxHeight),
_ => throw new NotSupportedException(),
};
}

private static int BoundIntToDouble(int value, double min, double max)
{
if (min > value)
return (int)Math.Ceiling(min);
if (max < value)
return (int)Math.Floor(max);
return value;
}

private static void MinMaxHeightWidth_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
d.CoerceValue(DockedWidthOrHeightProperty);
}

private static void DockLocation_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var @this = (AppBarWindow)d;
if (@this.IsAppBarRegistered)
@this.OnDockLocationChanged();
}

protected override void OnDpiChanged(DpiScale oldDpi, DpiScale newDpi)
{
base.OnDpiChanged(oldDpi, newDpi);
OnDockLocationChanged();
}

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var source = (HwndSource)PresentationSource.FromVisual(this);
if (!ShowInTaskbar)
{
var exstyle = (ulong)GetWindowLongPtr(source.Handle, GWL_EXSTYLE);
exstyle |= WS_EX_TOOLWINDOW;
SetWindowLongPtr(source.Handle, GWL_EXSTYLE, unchecked((IntPtr)exstyle));
}
source.AddHook(WndProc);
var abd = GetAppBarData();
SHAppBarMessage(ABM.NEW, ref abd);
IsAppBarRegistered = true;
OnDockLocationChanged();
}

protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (e.Cancel)
return;
if (IsAppBarRegistered)
{
var abd = GetAppBarData();
SHAppBarMessage(ABM.REMOVE, ref abd);
IsAppBarRegistered = false;
}
}

private int WpfDimensionToDesktop(double dim)
{
var dpi = VisualTreeHelper.GetDpi(this);
return (int)Math.Ceiling(dim * dpi.PixelsPerDip);
}

private double DesktopDimensionToWpf(double dim)
{
var dpi = VisualTreeHelper.GetDpi(this);
return dim / dpi.PixelsPerDip;
}

private void OnDockLocationChanged()
{
if (IsInAppBarResize)
return;
var abd = GetAppBarData();
abd.rc = (RECT)GetSelectedMonitor().ViewportBounds;
SHAppBarMessage(ABM.QUERYPOS, ref abd);
var dockedWidthOrHeightInDesktopPixels = WpfDimensionToDesktop(DockedWidthOrHeight);
switch (DockMode)
{
case AppBarDockMode.Top:
abd.rc.bottom = abd.rc.top + dockedWidthOrHeightInDesktopPixels;
break;
case AppBarDockMode.Bottom:
abd.rc.top = abd.rc.bottom - dockedWidthOrHeightInDesktopPixels;
break;
case AppBarDockMode.Left:
abd.rc.right = abd.rc.left + dockedWidthOrHeightInDesktopPixels;
break;
case AppBarDockMode.Right:
abd.rc.left = abd.rc.right - dockedWidthOrHeightInDesktopPixels;
break;
default:
throw new NotSupportedException();
}
SHAppBarMessage(ABM.SETPOS, ref abd);
IsInAppBarResize = true;
try
{
WindowBounds = (Rect)abd.rc;
}
finally
{
IsInAppBarResize = false;
}
}

private MonitorInfo GetSelectedMonitor()
{
var monitor = Monitor;
var allMonitors = MonitorInfo.GetAllMonitors();
if (monitor == null || !allMonitors.Contains(monitor))
monitor = allMonitors.First(f => f.IsPrimary);
return monitor;
}

private APPBARDATA GetAppBarData()
{
return new APPBARDATA()
{
cbSize = Marshal.SizeOf(typeof(APPBARDATA)),
hWnd = new WindowInteropHelper(this).Handle,
uCallbackMessage = AppBarMessageId,
uEdge = (int)DockMode
};
}

private static int _AppBarMessageId;
public static int AppBarMessageId
{
get
{
if (_AppBarMessageId == 0)
_AppBarMessageId = RegisterWindowMessage("AppBarMessage_EEDFB5206FC3");
return _AppBarMessageId;
}
}

public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_WINDOWPOSCHANGING && !IsInAppBarResize)
{
var wp = Marshal.PtrToStructure<WINDOWPOS>(lParam);
wp.flags |= SWP_NOMOVE | SWP_NOSIZE;
Marshal.StructureToPtr(wp, lParam, false);
}
else if (msg == WM_ACTIVATE)
{
var abd = GetAppBarData();
SHAppBarMessage(ABM.ACTIVATE, ref abd);
}
else if (msg == WM_WINDOWPOSCHANGED)
{
var abd = GetAppBarData();
SHAppBarMessage(ABM.WINDOWPOSCHANGED, ref abd);
}
else if (msg == AppBarMessageId)
{
switch ((ABN)(int)wParam)
{
case ABN.POSCHANGED:
OnDockLocationChanged();
handled = true;
break;
}
}
return IntPtr.Zero;
}

private Rect WindowBounds
{
set
{
Left = DesktopDimensionToWpf(value.Left);
Top = DesktopDimensionToWpf(value.Top);
Width = DesktopDimensionToWpf(value.Width);
Height = DesktopDimensionToWpf(value.Height);
}
}
}

public enum AppBarDockMode
{
Left,
Top,
Right,
Bottom
}

public sealed class MonitorInfo : IEquatable<MonitorInfo>
{
public Rect ViewportBounds { get; }

public Rect WorkAreaBounds { get; }

public bool IsPrimary { get; }

public string DeviceId { get; }

internal MonitorInfo(MONITORINFOEX mex)
{
ViewportBounds = (Rect)mex.rcMonitor;
WorkAreaBounds = (Rect)mex.rcWork;
IsPrimary = mex.dwFlags.HasFlag(MONITORINFOF.PRIMARY);
DeviceId = mex.szDevice;
}

public static IEnumerable<MonitorInfo> GetAllMonitors()
{
var monitors = new List<MonitorInfo>();
MonitorEnumDelegate callback = delegate (IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
{
MONITORINFOEX mi = new MONITORINFOEX();
mi.cbSize = Marshal.SizeOf(typeof(MONITORINFOEX));
if (!GetMonitorInfo(hMonitor, ref mi))
throw new Win32Exception();
monitors.Add(new MonitorInfo(mi));
return true;
};
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, IntPtr.Zero);
return monitors;
}

public override string ToString() => DeviceId;

public override bool Equals(object obj) => Equals(obj as MonitorInfo);

public override int GetHashCode() => DeviceId.GetHashCode();

public bool Equals(MonitorInfo other) => DeviceId == other?.DeviceId;

public static bool operator ==(MonitorInfo a, MonitorInfo b)
{
if (ReferenceEquals(a, b))
return true;
if (ReferenceEquals(a, null))
return false;
return a.Equals(b);
}

public static bool operator !=(MonitorInfo a, MonitorInfo b) => !(a == b);
}

internal static class NativeMethods
{
public enum ABEdge
{
Left,
Top,
Right,
Bottom,
None
}

[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;
public RECT rc;
public IntPtr lParam;
}

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;

public static explicit operator POINT(Point p)
{
return new POINT { X = (int)p.X, Y = (int)p.Y };
}

public static explicit operator Point(POINT r)
{
return new Point(r.X, r.Y);
}
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;

public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}

public int Width
{
get { return right - left; }
}

public int Height
{
get { return bottom - top; }
}

public void Offset(int dx, int dy)
{
left += dx;
top += dy;
right += dx;
bottom += dy;
}

public bool IsEmpty
{
get { return left >= right || top >= bottom; }
}

public static explicit operator Int32Rect(RECT r)
{
return new Int32Rect(r.left, r.top, r.Width, r.Height);
}

public static explicit operator Rect(RECT r)
{
return new Rect(r.left, r.top, r.Width, r.Height);
}

public static explicit operator RECT(Rect r)
{
return new RECT((int)r.Left, (int)r.Top, (int)r.Right, (int)r.Bottom);
}
}

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;

public Rect Bounds
{
get { return new Rect(x, y, cx, cy); }
set
{
x = (int)value.X;
y = (int)value.Y;
cx = (int)value.Width;
cy = (int)value.Height;
}
}
}

public const int
SWP_NOMOVE = 0x0002,
SWP_NOSIZE = 0x0001;

public const int
WM_ACTIVATE = 0x0006,
WM_WINDOWPOSCHANGED = 0x0047,
WM_SYSCOMMAND = 0x0112,
WM_WINDOWPOSCHANGING = 0x0046;

public const int
SC_MOVE = 0xF010;

public static int SC_FROM_WPARAM(IntPtr wparam)
{
return (int)wparam & 0xfff0;
}

public enum ABM
{
NEW,
REMOVE,
QUERYPOS,
SETPOS,
GETSTATE,
GETTASKBARPOS,
ACTIVATE,
GETAUTOHIDEBAR,
SETAUTOHIDEBAR,
WINDOWPOSCHANGED,
SETSTATE
}

public enum ABN
{
STATECHANGE,
POSCHANGED,
FULLSCREENAPP,
WINDOWARRANGE
}

public const int GWL_EXSTYLE = -20;

public const int WS_EX_TOOLWINDOW = 0x00000080;

[DllImport("shell32.dll", ExactSpelling = true)]
public static extern uint SHAppBarMessage(ABM dwMessage, ref APPBARDATA pData);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int RegisterWindowMessage(string msg);

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint flags);

public static IntPtr GetWindowLongPtr(IntPtr hWnd, int index) =>
IntPtr.Size == 4 ? GetWindowLongPtr32(hWnd, index) : GetWindowLongPtr64(hWnd, index);

[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int index);

[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int index);

public static IntPtr SetWindowLongPtr(IntPtr hWnd, int index, IntPtr newLong) =>
IntPtr.Size == 4 ? SetWindowLongPtr32(hWnd, index, newLong) : SetWindowLongPtr64(hWnd, index, newLong);

[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int index, IntPtr newLong);

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int index, IntPtr newLong);

[Flags]
public enum MONITORINFOF
{
PRIMARY = 0x1
}

[Flags]
public enum MONITOR_DPI_TYPE
{
MDT_EFFECTIVE_DPI = 0,
MDT_ANGULAR_DPI = 1,
MDT_RAW_DPI = 2,
MDT_DEFAULT = MDT_EFFECTIVE_DPI
}

private const int CCHDEVICENAME = 32;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MONITORINFOEX
{
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public MONITORINFOF dwFlags;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string szDevice;
}

public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFOEX lpmi);

[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);

[DllImport("shcore.dll", ExactSpelling = true, PreserveSig = false)]
public static extern void GetDpiForMonitor(IntPtr hMonitor, MONITOR_DPI_TYPE dpiType, out int dpiX, out int dpiY);
}
}

Solution B

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
public static class AppBar
{
public enum ABEdge
{
Left,
Top,
Right,
Bottom,
None
}

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

[StructLayout(LayoutKind.Sequential)]
private struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;
public RECT rc;
public IntPtr lParam;
}

private enum ABMsg
{
ABM_NEW,
ABM_REMOVE,
ABM_QUERYPOS,
ABM_SETPOS,
ABM_GETSTATE,
ABM_GETTASKBARPOS,
ABM_ACTIVATE,
ABM_GETAUTOHIDEBAR,
ABM_SETAUTOHIDEBAR,
ABM_WINDOWPOSCHANGED,
ABM_SETSTATE
}

private enum ABNotify
{
ABN_STATECHANGE,
ABN_POSCHANGED,
ABN_FULLSCREENAPP,
ABN_WINDOWARRANGE
}

[DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
private static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int RegisterWindowMessage(string msg);

private class RegisterInfo
{
public int CallbackId { get; set; }
public bool IsRegistered { get; set; }
public Window Window { get; set; }
public ABEdge Edge { get; set; }
public WindowStyle OriginalStyle { get; set; }
public Point OriginalPosition { get; set; }
public Size OriginalSize { get; set; }
public ResizeMode OriginalResizeMode { get; set; }


public IntPtr WndProc(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (uMsg == CallbackId)
{
if (wParam.ToInt32() == (int)ABNotify.ABN_POSCHANGED)
{
ABSetPos(Edge, Window);
handled = true;
}
}
return IntPtr.Zero;
}
}

private static Dictionary<Window, RegisterInfo> s_RegisteredWindowInfo = new Dictionary<Window, RegisterInfo>();

private static RegisterInfo GetRegisterInfo(Window appbarWindow)
{
RegisterInfo reg;
if (s_RegisteredWindowInfo.ContainsKey(appbarWindow))
{
reg = s_RegisteredWindowInfo[appbarWindow];
}
else
{
reg = new RegisterInfo()
{
CallbackId = 0,
Window = appbarWindow,
IsRegistered = false,
Edge = ABEdge.Top,
OriginalStyle = appbarWindow.WindowStyle,
OriginalPosition = new Point(appbarWindow.Left, appbarWindow.Top),
OriginalSize = new Size(appbarWindow.ActualWidth, appbarWindow.ActualHeight),
OriginalResizeMode = appbarWindow.ResizeMode,
};
s_RegisteredWindowInfo.Add(appbarWindow, reg);
}
return reg;
}

private static void RestoreWindow(Window appbarWindow)
{
RegisterInfo info = GetRegisterInfo(appbarWindow);
appbarWindow.WindowStyle = info.OriginalStyle;
appbarWindow.ResizeMode = info.OriginalResizeMode;
appbarWindow.Topmost = false;
Rect rect = new Rect(info.OriginalPosition.X, info.OriginalPosition.Y, info.OriginalSize.Width, info.OriginalSize.Height);
appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new ResizeDelegate(DoResize), appbarWindow, rect);
}

public static void SetAppBar(Window appbarWindow, ABEdge edge)
{
RegisterInfo info = GetRegisterInfo(appbarWindow);
info.Edge = edge;
APPBARDATA abd = new APPBARDATA();
abd.cbSize = Marshal.SizeOf(abd);
abd.hWnd = new WindowInteropHelper(appbarWindow).Handle;
if (edge == ABEdge.None)
{
if (info.IsRegistered)
{
SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);
info.IsRegistered = false;
}
RestoreWindow(appbarWindow);
return;
}
if (!info.IsRegistered)
{
info.IsRegistered = true;
info.CallbackId = RegisterWindowMessage("AppBarMessage");
abd.uCallbackMessage = info.CallbackId;
uint ret = SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);
HwndSource source = HwndSource.FromHwnd(abd.hWnd);
source.AddHook(new HwndSourceHook(info.WndProc));
}
appbarWindow.WindowStyle = WindowStyle.None;
appbarWindow.ResizeMode = ResizeMode.NoResize;
appbarWindow.Topmost = true;
ABSetPos(info.Edge, appbarWindow);
}

private delegate void ResizeDelegate(Window appbarWindow, Rect rect);

private static void DoResize(Window appbarWindow, Rect rect)
{
appbarWindow.Width = rect.Width;
appbarWindow.Height = rect.Height;
appbarWindow.Top = rect.Top;
appbarWindow.Left = rect.Left;
}

private static void ABSetPos(ABEdge edge, Window appbarWindow)
{
APPBARDATA barData = new APPBARDATA();
barData.cbSize = Marshal.SizeOf(barData);
barData.hWnd = new WindowInteropHelper(appbarWindow).Handle;
barData.uEdge = (int)edge;
if (barData.uEdge == (int)ABEdge.Left || barData.uEdge == (int)ABEdge.Right)
{
barData.rc.top = 0;
barData.rc.bottom = (int)SystemParameters.PrimaryScreenHeight;
if (barData.uEdge == (int)ABEdge.Left)
{
barData.rc.left = 0;
barData.rc.right = (int)Math.Round(appbarWindow.ActualWidth);
}
else
{
barData.rc.right = (int)SystemParameters.PrimaryScreenWidth;
barData.rc.left = barData.rc.right - (int)Math.Round(appbarWindow.ActualWidth);
}
}
else
{
barData.rc.left = 0;
barData.rc.right = (int)SystemParameters.PrimaryScreenWidth;
if (barData.uEdge == (int)ABEdge.Top)
{
barData.rc.top = 0;
barData.rc.bottom = (int)Math.Round(appbarWindow.ActualHeight);
}
else
{
barData.rc.bottom = (int)SystemParameters.PrimaryScreenHeight;
barData.rc.top = barData.rc.bottom - (int)Math.Round(appbarWindow.ActualHeight);
}
}
SHAppBarMessage((int)ABMsg.ABM_QUERYPOS, ref barData);
SHAppBarMessage((int)ABMsg.ABM_SETPOS, ref barData);
Rect rect = new Rect(barData.rc.left, barData.rc.top, barData.rc.right - barData.rc.left, barData.rc.bottom - barData.rc.top);
appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new ResizeDelegate(DoResize), appbarWindow, rect);
}
}

参考资料

https://stackoverflow.com/questions/75785

https://blog.walterlv.com/post/dock-window-into-windows-desktop.html

https://github.com/PhilipRieck/WpfAppBar

https://github.com/mgaffigan/WpfAppBar

https://github.com/maxstreese/AppBarServices

https://github.com/adamecr/AppSwitcherBar

https://github.com/ArcadeRenegade/SidebarDiagnostics