欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用 AppBar 实现窗口停靠、自适应缩放、全屏响应和多窗口并排的 WPF(附带打包的即用附加属性)

最编程 2024-07-18 16:48:01
...
1 using System.ComponentModel; 2 using System.Diagnostics; 3 using System.Runtime.InteropServices; 4 using System.Windows; 5 using System.Windows.Interop; 6 using System.Windows.Threading; 7 8 namespace AppBarTest; 9 public static class AppBarCreator 10 { 11 public static readonly DependencyProperty AppBarProperty = 12 DependencyProperty.RegisterAttached( 13 "AppBar", 14 typeof(AppBar), 15 typeof(AppBarCreator), 16 new PropertyMetadata(null, OnAppBarChanged)); 17 private static void OnAppBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 18 { 19 if (d is Window window && e.NewValue is AppBar appBar) 20 { 21 appBar.AttachedWindow = window; 22 } 23 } 24 public static void SetAppBar(Window element, AppBar value) 25 { 26 if (value == null) return; 27 element.SetValue(AppBarProperty, value); 28 } 29 30 public static AppBar GetAppBar(Window element) 31 { 32 return (AppBar)element.GetValue(AppBarProperty); 33 } 34 } 35 36 public class AppBar : DependencyObject 37 { 38 /// <summary> 39 /// 附加到的窗口 40 /// </summary> 41 public Window AttachedWindow 42 { 43 get => _window; 44 set 45 { 46 if (value == null) return; 47 _window = value; 48 _window.Closing += _window_Closing; 49 _window.LocationChanged += _window_LocationChanged; 50 //获取窗口句柄hWnd 51 var handle = new WindowInteropHelper(value).Handle; 52 if (handle == IntPtr.Zero) 53 { 54 //Win32窗口未创建 55 _window.SourceInitialized += _window_SourceInitialized; 56 } 57 else 58 { 59 _hWnd = handle; 60 CheckPending(); 61 } 62 } 63 } 64 65 private void _window_LocationChanged(object? sender, EventArgs e) 66 { 67 Debug.WriteLine(_window.Title+ " LocationChanged: Top: "+_window.Top+" Left: "+_window.Left); 68 } 69 70 private void _window_Closing(object? sender, CancelEventArgs e) 71 { 72 _window.Closing -= _window_Closing; 73 if (Location != AppBarLocation.None) 74 DisableAppBar(); 75 } 76 77 /// <summary> 78 /// 检查是否需要应用之前的Location更改 79 /// </summary> 80 private void CheckPending() 81 { 82 //创建AppBar时提前触发的LocationChanged 83 if (_locationChangePending) 84 { 85 _locationChangePending = false; 86 LoadAppBar(Location); 87 } 88 } 89 /// <summary> 90 /// 载入AppBar 91 /// </summary> 92 /// <param name="e"></param> 93 private void LoadAppBar(AppBarLocation e,AppBarLocation? previous=null) 94 { 95 96 if (e != AppBarLocation.None) 97 { 98 if (e == AppBarLocation.RegisterOnly) 99 { 100 //仅注册AppBarMsg 101 //如果之前注册过有效的AppBar则先注销,以还原位置 102 if (previous.HasValue && previous.Value != AppBarLocation.RegisterOnly) 103 { 104 if (previous.Value != AppBarLocation.None) 105 { 106 //由生效的AppBar转为RegisterOnly,还原为普通窗口再注册空AppBar 107 DisableAppBar(); 108 } 109 RegisterAppBarMsg(); 110 } 111 else 112 { 113 //之前未注册过AppBar,直接注册 114 RegisterAppBarMsg(); 115 } 116 } 117 else 118 { 119 if (previous.HasValue && previous.Value != AppBarLocation.None) 120 { 121 //之前为RegisterOnly才备份窗口信息 122 if(previous.Value == AppBarLocation.RegisterOnly) 123 { 124 BackupWindowInfo(); 125 } 126 SetAppBarPosition(_originalSize); 127 ForceWindowStyles(); 128 } 129 else 130 EnableAppBar(); 131 } 132 } 133 else 134 { 135 DisableAppBar(); 136 } 137 } 138 private void _window_SourceInitialized(object? sender, EventArgs e) 139 { 140 _window.SourceInitialized -= _window_SourceInitialized; 141 _hWnd = new WindowInteropHelper(_window).Handle; 142 CheckPending(); 143 } 144 145 /// <summary> 146 /// 当有窗口进入或退出全屏时触发 bool参数为true时表示全屏状态 147 /// </summary> 148 public event EventHandler<bool>? OnFullScreenStateChanged; 149 /// <summary> 150 /// 期望将AppBar停靠到的位置 151 /// </summary> 152 public AppBarLocation Location 153 { 154 get { return (AppBarLocation)GetValue(LocationProperty); } 155 set { SetValue(LocationProperty, value); } 156 } 157 158 public static readonly DependencyProperty LocationProperty = 159 DependencyProperty.Register( 160 "Location", 161 typeof(AppBarLocation), typeof(AppBar), 162 new PropertyMetadata(AppBarLocation.None, OnLocationChanged)); 163 164 private bool _locationChangePending = false; 165 private static void OnLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 166 { 167 if (DesignerProperties.GetIsInDesignMode(d)) 168 return; 169 if (d is not AppBar appBar) return; 170 if (appBar.AttachedWindow == null) 171 { 172 appBar._locationChangePending = true; 173 return; 174 } 175 appBar.LoadAppBar((AppBarLocation)e.NewValue,(AppBarLocation)e.OldValue); 176 } 177 178 private int _callbackId = 0; 179 private bool _isRegistered = false; 180 private Window _window = null; 181 private IntPtr _hWnd; 182 private WindowStyle _originalStyle; 183 private Point _originalPosition; 184 private Size _originalSize = Size.Empty; 185 private ResizeMode _originalResizeMode; 186 private bool _originalTopmost; 187 public Rect? DockedSize { get; set; } = null; 188 private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, 189 IntPtr lParam, ref bool handled) 190 { 191 if (msg == _callbackId) 192 { 193 Debug.WriteLine(_window.Title + " AppBarMsg("+_callbackId+"): " + wParam.ToInt32() + " LParam: " + lParam.ToInt32()); 194 switch (wParam.ToInt32()) 195 { 196 case (int)Interop.AppBarNotify.ABN_POSCHANGED: 197 Debug.WriteLine("AppBarNotify.ABN_POSCHANGED ! "+_window.Title); 198 if (Location != AppBarLocation.RegisterOnly) 199 SetAppBarPosition(Size.Empty); 200 handled = true; 201 break; 202 case (int)Interop.AppBarNotify.ABN_FULLSCREENAPP: 203 OnFullScreenStateChanged?.Invoke(this, lParam.ToInt32() == 1); 204 handled = true; 205 break; 206 } 207 } 208 return IntPtr.Zero; 209 } 210 211 public void BackupWindowInfo() 212 { 213 _callbackId = 0; 214 DockedSize = null; 215 _originalStyle = _window.WindowStyle; 216 _originalSize = new Size(_window.ActualWidth, _window.ActualHeight); 217 _originalPosition = new Point(_window.Left, _window.Top); 218 _originalResizeMode = _window.ResizeMode; 219 _originalTopmost = _window.Topmost; 220 } 221 public void RestoreWindowInfo() 222 { 223 if (_originalSize != Size.Empty) 224 { 225 _window.WindowStyle = _originalStyle; 226 _window.ResizeMode = _originalResizeMode; 227 _window.Topmost = _originalTopmost; 228 _window.Left = _originalPosition.X; 229 _window.Top = _originalPosition.Y; 230 _window.Width = _originalSize.Width; 231 _window.Height = _originalSize.Height; 232 } 233 } 234 public void ForceWindowStyles() 235 { 236 _window.WindowStyle = WindowStyle.None; 237 _window.ResizeMode = ResizeMode.NoResize; 238 _window.Topmost = true; 239 } 240 241 public void RegisterAppBarMsg() 242 { 243 var data = new Interop.APPBARDATA(); 244 data.cbSize = Marshal.SizeOf(data); 245 data.hWnd = _hWnd; 246 247 _isRegistered = true; 248 _callbackId = Interop.RegisterWindowMessage(Guid.NewGuid().ToString()); 249 data.uCallbackMessage = _callbackId; 250 var success = Interop.SHAppBarMessage((int)Interop.AppBarMsg.ABM_NEW, ref data); 251 var source = HwndSource.FromHwnd(_hWnd); 252 Debug.WriteLineIf(source == null, "HwndSource is null!"); 253 source?.AddHook(WndProc); 254 Debug.WriteLine(_window.Title+" RegisterAppBarMsg: " + _callbackId); 255 } 256 public void EnableAppBar() 257 { 258 if (!_isRegistered) 259 { 260 //备份窗口信息并设置窗口样式 261 BackupWindowInfo(); 262 //注册成为AppBar窗口 263 RegisterAppBarMsg(); 264 ForceWindowStyles(); 265 } 266 //成为AppBar窗口之后(或已经是)只需要注册并移动窗口位置即可 267 SetAppBarPosition(_originalSize); 268 } 269 public void SetAppBarPosition(Size WindowSize) 270 { 271 var data = new Interop.APPBARDATA(); 272 data.cbSize = Marshal.SizeOf(data); 273 data.hWnd = _hWnd; 274 data.uEdge = (int)Location; 275 data.uCallbackMessage = _callbackId; 276 Debug.WriteLine("\r\nWindow: "+_window.Title); 277 278 //获取WPF单位与像素的转换矩阵 279 var compositionTarget = PresentationSource.FromVisual(_window)?.CompositionTarget; 280 if (compositionTarget == null) 281 throw new Exception("居然获取不到CompositionTarget?!"); 282 var toPixel = compositionTarget.TransformToDevice; 283 var toWpfUnit = compositionTarget.TransformFromDevice; 284 285 //窗口在屏幕的实际大小 286 if(WindowSize== Size.Empty) 287 WindowSize = new Size(_window.ActualWidth, _window.ActualHeight); 288 var actualSize = toPixel.Transform(new Vector(WindowSize.Width, WindowSize.Height)); 289 //屏幕的真实像素 290 var workArea = toPixel.Transform(new Vector(SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight)); 291 Debug.WriteLine("WorkArea Width: {0}, Height: {1}", workArea.X, workArea.Y); 292 293 if (Location is AppBarLocation.Left or AppBarLocation.Right) 294 { 295 data.rc.top = 0; 296 data.rc.bottom = (int)workArea.Y; 297 if (Location == AppBarLocation.Left) 298 { 299 data.rc.left = 0; 300 data.rc.right = (int)Math.Round(actualSize.X); 301 } 302 else 303 { 304 data.rc.right = (int)workArea.X; 305 data.rc.left = (int)workArea.X - (int)Math.Round(actualSize.X); 306 } 307 } 308 else 309 { 310 data.rc.left = 0; 311 data.rc.right = (int)workArea.X; 312 if (Location == AppBarLocation.Top) 313 { 314 data.rc.top = 0; 315 data.rc.bottom = (int)Math.Round(actualSize.Y); 316 } 317 else 318 { 319 data.rc.bottom = (int)workArea.Y; 320 data.rc.top = (int)workArea.Y - (int)Math.Round(actualSize.Y); 321 } 322 } 323 //以上生成的是四周都没有其他AppBar时的理想位置 324 //系统将自动调整位置以适应其他AppBar 325 Debug.WriteLine("Before QueryPos: Left: {0}, Top: {1}, Right: {2}, Bottom: {3}", data.rc.left, data.rc.top, data.rc.right, data.rc.bottom); 326 Interop.SHAppBarMessage((int)Interop.AppBarMsg.ABM_QUERYPOS, ref data); 327 Debug.WriteLine("After QueryPos: Left: {0}, Top: {1}, Right: {2}, Bottom: {3}", data.rc.left, data.rc.top, data.rc.right, data.rc.bottom); 328 //自定义对齐方式,确保Height和Width不会小于0 329 if (data.rc.bottom - data.rc.top < 0) 330 { 331 if (Location == AppBarLocation.Top) 332 data.rc.bottom = data.rc.top + (int)Math.Round(actualSize.Y);//上对齐 333 else if (Location == AppBarLocation.Bottom) 334 data.rc.top = data.rc.bottom - (int)Math.Round(actualSize.Y);//下对齐 335 } 336 if(data.rc.right - data.rc.left < 0) 337 { 338 if (Location == AppBarLocation.Left) 339 data.rc.right = data.rc.left + (int)Math.Round(actualSize.X);//左对齐 340 else if (Location == AppBarLocation.Right) 341 data.rc.left = data.rc.right - (int)Math.Round(actualSize.X);//右对齐 342 } 343 //调整完毕,设置为最终位置 344 Interop.SHAppBarMessage((int)Interop.AppBarMsg.ABM_SETPOS, ref data); 345 //应用到窗口 346 var location = toWpfUnit.Transform(new Point(data.rc.left, data.rc.top)); 347 var dimension = toWpfUnit.Transform(new Vector(data.rc.right - data.rc.left, 348 data.rc.bottom - data.rc.top)); 349 var rect = new Rect(location, new Size(dimension.X, dimension.Y)); 350 DockedSize = rect; 351 352 _window.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, () =>{ 353 _window.Left = rect.Left; 354 _window.Top = rect.Top; 355 _window.Width = rect.Width; 356 _window.Height = rect.Height; 357 }); 358 359 Debug.WriteLine("Set {0} Left: {1} ,Top: {2}, Width: {3}, Height: {4}", _window.Title, _window.Left, _window.Top, _window.Width, _window.Height); 360 } 361 public void DisableAppBar() 362 { 363 if (_isRegistered) 364 { 365 _isRegistered = false; 366 var data = new Interop.APPBARDATA(); 367 data.cbSize = Marshal.SizeOf(data); 368 data.hWnd = _hWnd; 369 data.uCallbackMessage = _callbackId; 370 Interop.SHAppBarMessage((int)Interop.AppBarMsg.ABM_REMOVE, ref data); 371 _isRegistered = false; 372 RestoreWindowInfo(); 373 Debug.WriteLine(_window.Title + " DisableAppBar"); 374 } 375 } 376 } 377 378 public enum AppBarLocation : int 379 { 380 Left = 0, 381

上一篇: 基于 Swoole 扩展框架(如 Hyperf 或 EasySwoole)的用户请求流。

下一篇: pssh 用户手册 - II.安装