Browse Source

Add blur to background on full-screen gallery

Ruben 5 years ago
parent
commit
53c91f9911

+ 101 - 2
PicView/SystemIntegration/NativeMethods.cs

@@ -2,6 +2,8 @@
 using System;
 using System.Runtime.InteropServices;
 using System.Security;
+using System.Windows;
+using System.Windows.Interop;
 
 namespace PicView.SystemIntegration
 {
@@ -141,8 +143,105 @@ namespace PicView.SystemIntegration
         [StructLayout(LayoutKind.Sequential)]
         internal struct Win32Point
         {
-            public Int32 X;
-            public Int32 Y;
+            public int X;
+            public int Y;
         };
+
+        enum AccentState
+        {
+            ACCENT_DISABLED = 0,
+            ACCENT_ENABLE_GRADIENT = 1,
+            ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
+            ACCENT_ENABLE_BLURBEHIND = 3,
+            ACCENT_INVALID_STATE = 4
+        }
+
+        struct AccentPolicy : IEquatable<AccentPolicy>
+        {
+            public AccentState AccentState;
+            public int AccentFlags;
+            public int GradientColor;
+            public int AnimationId;
+
+            public override bool Equals(object obj)
+            {
+                return obj is AccentPolicy policy && Equals(policy);
+            }
+
+            public bool Equals(AccentPolicy other)
+            {
+                return AnimationId == other.AnimationId;
+            }
+
+            public static bool operator ==(AccentPolicy left, AccentPolicy right)
+            {
+                return left.Equals(right);
+            }
+
+            public static bool operator !=(AccentPolicy left, AccentPolicy right)
+            {
+                return !(left == right);
+            }
+
+            public override int GetHashCode()
+            {
+                throw new NotImplementedException();
+            }
+        }
+
+        struct WindowCompositionAttributeData : IEquatable<WindowCompositionAttributeData>
+        {
+            public WindowCompositionAttribute Attribute;
+            public IntPtr Data;
+            public int SizeOfData;
+
+            public override bool Equals(object obj)
+            {
+                return obj is WindowCompositionAttributeData data && Equals(data);
+            }
+
+            public bool Equals(WindowCompositionAttributeData other)
+            {
+                return Attribute == other.Attribute &&
+                       Data.Equals(other.Data) &&
+                       SizeOfData == other.SizeOfData;
+            }
+
+            public override int GetHashCode()
+            {
+                throw new NotImplementedException();
+            }
+        }
+
+        enum WindowCompositionAttribute
+        {
+            WCA_ACCENT_POLICY = 19
+        }
+
+        [DllImport("user32.dll")]
+        static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
+
+        internal static void EnableBlur(Window window)
+        {
+            var windowHelper = new WindowInteropHelper(window);
+            var accent = new AccentPolicy
+            {
+                AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
+            };
+
+            var accentStructSize = Marshal.SizeOf(accent);
+            var accentPtr = Marshal.AllocHGlobal(accentStructSize);
+            Marshal.StructureToPtr(accent, accentPtr, false);
+
+            var Data = new WindowCompositionAttributeData
+            {
+                Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
+                SizeOfData = accentStructSize,
+                Data = accentPtr
+            };
+
+            _ = SetWindowCompositionAttribute(windowHelper.Handle, ref Data);
+            Marshal.FreeHGlobal(accentPtr);
+        }
     }
 }

+ 1 - 3
PicView/UI/Windows/FakeWindow.xaml

@@ -6,14 +6,12 @@
     AllowsTransparency="True"
     Background="{x:Null}"
     Focusable="False"
+    Foreground="{x:Null}"
     ResizeMode="NoResize"
     ShowInTaskbar="False"
     Topmost="False"
     WindowStartupLocation="CenterScreen"
     WindowStyle="None">
-    <WindowChrome.WindowChrome>
-        <WindowChrome CaptionHeight="0" UseAeroCaptionButtons="False" />
-    </WindowChrome.WindowChrome>
     <Grid
         x:Name="grid"
         Background="#A4000000"

+ 4 - 1
PicView/UI/Windows/FakeWindow.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PicView.SystemIntegration;
+using System;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Interop;
@@ -34,6 +35,8 @@ namespace PicView.UI.Windows
             LostFocus += FakeWindow_LostFocus;
             GotFocus += FakeWindow_LostFocus;
 
+            EnableBlur(this);
+
             // Hide from alt tab
             var helper = new WindowInteropHelper(this).Handle;
             _ = SetWindowLong(helper, GWL_EX_STYLE, (GetWindowLong(helper, GWL_EX_STYLE) | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW);