|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows.Interop; |
| 8 | +using System.Windows; |
| 9 | +using System.Windows.Threading; |
| 10 | +using System.Reflection; |
| 11 | + |
| 12 | +namespace LemonApp; |
| 13 | + |
| 14 | +public static class DwmAnimation |
| 15 | +{ |
| 16 | + |
| 17 | + public static bool GetEnableDwmAnimation(DependencyObject obj) |
| 18 | + { |
| 19 | + return (bool)obj.GetValue(EnableDwmAnimationProperty); |
| 20 | + } |
| 21 | + |
| 22 | + public static void SetEnableDwmAnimation(DependencyObject obj, bool value) |
| 23 | + { |
| 24 | + obj.SetValue(EnableDwmAnimationProperty, value); |
| 25 | + } |
| 26 | + |
| 27 | + public static readonly DependencyProperty EnableDwmAnimationProperty = |
| 28 | + DependencyProperty.RegisterAttached("EnableDwmAnimation", |
| 29 | + typeof(bool), typeof(DwmAnimation), |
| 30 | + new PropertyMetadata(false,OnEnableDwmAnimationChanged)); |
| 31 | + |
| 32 | + public static void OnEnableDwmAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 33 | + { |
| 34 | + if (d is Window window) |
| 35 | + { |
| 36 | + if ((bool)e.NewValue) |
| 37 | + { |
| 38 | + if (window.IsLoaded) |
| 39 | + { |
| 40 | + EnableDwmAnimation(window); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + window.SourceInitialized += Window_SourceInitialized; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private static void Window_SourceInitialized(object? sender, EventArgs e) |
| 51 | + { |
| 52 | + if(sender is Window w) |
| 53 | + { |
| 54 | + EnableDwmAnimation(w); |
| 55 | + w.SourceInitialized -= Window_SourceInitialized; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [DllImport("user32.dll", EntryPoint = "SetWindowLong")] |
| 60 | + private static extern int SetWindowLong32(HandleRef hWnd, int nIndex, int dwNewLong); |
| 61 | + |
| 62 | + [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] |
| 63 | + private static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong); |
| 64 | + |
| 65 | + public const int GWL_STYLE = -16; |
| 66 | + public const long WS_CAPTION = 0x00C00000L, |
| 67 | + WS_MAXIMIZEBOX = 0x00010000L, |
| 68 | + WS_MINIMIZEBOX = 0x00020000L, |
| 69 | + WS_THICKFRAME = 0x00040000L, |
| 70 | + WS_OVERLAPPED= 0x00000000L, |
| 71 | + WS_SYSMENU= 0x00080000L, |
| 72 | + WS_BORDER= 0x00800000L; |
| 73 | + |
| 74 | + public static IntPtr SetWindowLongPtr(HandleRef hWnd, int nIndex, IntPtr dwNewLong) |
| 75 | + => IntPtr.Size == 8 |
| 76 | + ? SetWindowLongPtr64(hWnd, nIndex, dwNewLong) |
| 77 | + : new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32())); |
| 78 | + public static void EnableDwmAnimation(Window w) |
| 79 | + { |
| 80 | + var myHWND = new WindowInteropHelper(w).Handle; |
| 81 | + IntPtr myStyle = new(WS_CAPTION|WS_THICKFRAME|WS_MAXIMIZEBOX|WS_MINIMIZEBOX); |
| 82 | + if (w.ResizeMode == ResizeMode.NoResize||w.ResizeMode==ResizeMode.CanMinimize) |
| 83 | + { |
| 84 | + myStyle = new(WS_CAPTION | WS_MINIMIZEBOX); |
| 85 | + } |
| 86 | + SetWindowLongPtr(new HandleRef(null, myHWND), GWL_STYLE, myStyle); |
| 87 | + } |
| 88 | +} |
0 commit comments