Закругленные углы окон с прозрачной размытой формой - PullRequest
0 голосов
/ 10 октября 2018

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);

Into "(this) »передал размер формы и не учел закругления краев.

PS Пожалуйста, не кидайте мне ссылку на Kamdroid, он не нашел решения и нет ничего нового для меня.

...