透明的模糊形式的圆的角

时间:2018-10-10 07:40:08

标签: c# wpf transparent blur rounded-corners

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

将“(this)”传递为表格的大小,而不考虑舍入边缘。

P.S。请不要向我扔到Kamdroid的链接,他没有找到解决方案,对我来说没有什么新鲜的东西。

0 个答案:

没有答案