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

窗户圆角透明模糊

  •  2
  • Behavior  · 技术社区  · 6 年前

    enter image description here

    我想用模糊把透明形状的边缘磨圆。 在我看到的一些例子中,形状可能是半透明的,然后边缘是圆形的,但当我使用模糊时,它会模糊形状的原始大小。 为了让它更清楚,我将缩进10个像素,你会看到确切的问题是什么。

    enter image description here

    以下是我使用的代码:

    namespace Testui
    {
        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 Main : System.Windows.Window
        {
            [DllImport("user32.dll")]
            internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
            public Main()
            {
                InitializeComponent();
                CommandBindings.Add(new CommandBinding(ApplicationCommands.Close,
                    new ExecutedRoutedEventHandler(delegate(object sender, ExecutedRoutedEventArgs args) { this.Close(); })));
            }
    
            public void DragWindow(object sender, MouseButtonEventArgs args)
            {
                DragMove();
            }
    
            public void ButtonClicked(object sender, RoutedEventArgs args)
            {
    
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                EnableBlur();
            }
    
            internal void EnableBlur()
            {
                var windowHelper = new WindowInteropHelper(this);
    
                var accent = new AccentPolicy
                {
                    AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
                };
    
                int 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);
            }
        }
    }
    

    这是xaml代码:

    <Window x:Class="Testui.Main"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main" Height="250" Width="500"
    Background="#0000"
        AllowsTransparency="True"
        WindowStyle="None"
        BorderThickness="0"
        WindowStartupLocation="CenterScreen"
        Loaded="Window_Loaded">
    
    <Border x:Name="brder" Margin = "0" CornerRadius="15">
        <Grid>
    
            <Grid.RowDefinitions>
                <RowDefinition Height="35" />
            </Grid.RowDefinitions>
            <Border 
                Background="CadetBlue" 
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch" 
                CornerRadius="15,15,0,0" 
                Margin="-1,0,-1,0" 
                MouseLeftButtonDown="DragWindow"/>
    
        </Grid>
    </Border>
    

    我认为问题可能在这方面:

    var windowHelper = new WindowInteropHelper(this);
    

    进入“(此)”传输形式的大小,不考虑圆角边缘。

    另外,请不要给我一个Kamdroid的链接,他没有找到解决方案,对我来说也没有什么新鲜事。

    0 回复  |  直到 6 年前