代码之家  ›  专栏  ›  技术社区  ›  SledgeHammer

“glitchy”背后的WPF/WindowChrome/Blur

  •  3
  • SledgeHammer  · 技术社区  · 5 年前

    正在尝试对我的应用程序使用WPF/Blur-Behind/WindowChrome。我要后面模糊,窗口大小调整,窗口拖放阴影。

    当我在调试器F5中运行时,玻璃在WPF窗口最终绘制之前出现了大约一秒钟。我该怎么解决这个问题?

    Xaml编号:

    <Window x:Class="WpfApp3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            ResizeMode="CanResizeWithGrip"
            WindowStyle="None"
            Background="Transparent"
            WindowStartupLocation="CenterScreen"
            Title="MainWindow" Height="450" Width="800">
        <WindowChrome.WindowChrome>
            <WindowChrome CaptionHeight="24" GlassFrameThickness="1" UseAeroCaptionButtons="False" />
        </WindowChrome.WindowChrome>
        <Grid Background="#D000FF00">
    
        </Grid>
    </Window>
    

    C#:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Interop;
    
    namespace WpfApp3
    {
        internal enum AccentState
        {
            ACCENT_DISABLED = 0,
            ACCENT_ENABLE_GRADIENT = 1,
            ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
            ACCENT_ENABLE_BLURBEHIND = 3,
            ACCENT_INVALID_STATE = 4
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct AccentPolicy
        {
            public AccentState AccentState;
            public int AccentFlags;
            public int GradientColor;
            public int AnimationId;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct WindowCompositionAttributeData
        {
            public WindowCompositionAttribute Attribute;
            public IntPtr Data;
            public int SizeOfData;
        }
    
        internal enum WindowCompositionAttribute
        {
            // ...
            WCA_ACCENT_POLICY = 19
            // ...
        }
    
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            [DllImport("user32.dll")]
            internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
    
            public MainWindow()
            {
    
                InitializeComponent();
                this.Loaded += MainWindow_Loaded;
            }
    
            private void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                EnableBlur();
            }
    
            internal void EnableBlur()
            {
                var windowHelper = new WindowInteropHelper(this);
    
                var accent = new AccentPolicy();
                accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
    
                var accentStructSize = Marshal.SizeOf(accent);
    
                var accentPtr = Marshal.AllocHGlobal(accentStructSize);
                Marshal.StructureToPtr(accent, accentPtr, false);
    
                var data = new WindowCompositionAttributeData();
                data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
                data.SizeOfData = accentStructSize;
                data.Data = accentPtr;
    
                SetWindowCompositionAttribute(windowHelper.Handle, ref data);
    
                Marshal.FreeHGlobal(accentPtr);
            }
        }
    }
    
    0 回复  |  直到 5 年前