C# WPF如何实现毛玻璃效果?

C# WPF怎么实现下图的毛玻璃卡片效果。站内外关于WPF如何实现没有看到有关介绍。
最后想要的效果是大概是Border或者其他容器本身做到图片中的毛玻璃小卡片(有悬浮的感觉)效果

img

img

毛玻璃吗?这个有很多文章呀,你贴的图片是要问阴影立体效果吧?

阴影立体效果参考:
https://blog.csdn.net/weixin_42535339/article/details/95031458

阴影效果:
https://blog.csdn.net/weixin_43783135/article/details/125746532

望采纳!!!点击回答右侧采纳即可!!!

BlurEffect类可以在控件上应用模糊效果。

下面是一个示例代码:

1.首先,在xaml中添加一个名为"card"的StackPanel作为卡片的容器,并将需要添加毛玻璃效果的子元素添加到该容器中。

<StackPanel x:Name="card" Width="300" Height="200">
    <TextBlock Text="My Card" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="36" />
</StackPanel>

2.然后,在code behind中创建一个BlurEffect对象,并将其应用到卡片容器上。

BlurEffect blur = new BlurEffect();
blur.Radius = 20;
card.Effect = blur;

3.可以使用Radius属性来调整模糊程度。
这样就可以实现毛玻璃卡片效果了

在 C# WPF 中实现毛玻璃效果可以使用 Win32 API 和 Windows Presentation Foundation (WPF) 的互操作性 (Interoperability)。

首先,你需要引用 System.Runtime.InteropServices 命名空间。然后,你可以使用如下代码设置窗口的毛玻璃效果:


```c#
[DllImport("user32.dll")]
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

private void EnableBlur(Window window)
{
    var windowHelper = new WindowInteropHelper(window);
    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);
}
然后你可以在窗口的加载事件中调用 EnableBlur 方法来启用毛玻璃效果。例如:

```c#
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    EnableBlur(this);
}

此外,你还可以在窗口的样式中设置背景颜色为透明来让毛玻璃效果更加明显。例如:


```c#
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    EnableBlur(this);
}


```
希望这些代码能帮助你实现毛玻璃效果。

在 C# WPF 中实现毛玻璃卡片效果可以使用以下步骤:

1、使用 "Windows Presentation Foundation" (WPF) 创建一个新项目。

2、在 XAML 代码中添加一个名为 "GlassEffect" 的资源。这个资源将定义毛玻璃效果。

3、为要使用毛玻璃效果的控件设置 "Effect" 属性并将其设置为 "GlassEffect"。

4、在 C# 代码中编写代码来应用毛玻璃效果,如设置透明度、模糊半径等。

以下是一个示例 XAML 代码片段,它定义了一个名为 "card" 的控件,并应用了毛玻璃效果:

<Grid>
    <Grid.Resources>
        <DropShadowEffect x:Key="GlassEffect" Color="Black" Opacity="0.5" BlurRadius="10" />
    </Grid.Resources>
    <Border CornerRadius="10" Background="#FFFFFF" Effect="{StaticResource GlassEffect}">
        <!-- card content here -->
    </Border>
</Grid>

简单来说就是通过定义一个DropShadowEffect类型的资源作为控件的Effect属性,来实现毛玻璃效果。
望采纳!

我写给你参看一下,若有帮助,还望采纳,点击回答右侧采纳即可。
创建一个叫AeroGlass.cs 的类,代码如下:

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
    public MARGINS(Thickness t)
    {
        Left = (int)t.Left;
        Right = (int)t.Right;
        Top = (int)t.Top;
        Bottom = (int)t.Bottom;
    }
    public int Left;
    public int Right;
    public int Top;
    public int Bottom;
}

public class GlassHelper
{
    [DllImport("dwmapi.dll", PreserveSig = false)]
    static extern void DwmExtendFrameIntoClientArea(
        IntPtr hWnd, ref MARGINS pMarInset);
    [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();

    public static bool ExtendGlassFrame(Window window, Thickness margin)
    {
        if (!DwmIsCompositionEnabled())
            return false;

        IntPtr hwnd = new WindowInteropHelper(window).Handle;
        if (hwnd == IntPtr.Zero)
            throw new InvalidOperationException(
            "The Window must be shown before extending glass.");

        // Set the background to transparent from both the WPF and Win32 perspectives
        window.Background = Brushes.Transparent;
        HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

        MARGINS margins = new MARGINS(margin);
        DwmExtendFrameIntoClientArea(hwnd, ref margins);
        return true;
    }
}

把这段代码加到主窗体就可以了!

protected override void OnSourceInitialized(EventArgs e){
            base.OnSourceInitialized(e);
           GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
}