Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

wpf app transparent background with blurred image affect

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

namespace BlurBehindDemo
{
internal enum AccentState
{
    ACCENT_DISABLED = 1,
    ACCENT_ENABLE_GRADIENT = 0,
    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();
    }

    private void Window_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);
    }

    private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        DragMove();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: wpf dispatcher timer is inaccurate 
Csharp :: string vs string c# 
Csharp :: C3 compare hour 
Csharp :: concurrent post request c# 
Csharp :: can object change color when collided with particles unity 
Csharp :: csharp attributes as generics constraints 
Csharp :: how to instantiate more enemies in unity 
Csharp :: c# button click gets assigned the last value 
Csharp :: rename join ta le in many to many 
Csharp :: disable button netbeans 
Csharp :: difference between iqueryable and ienurable 
Csharp :: wpf button to return to last window 
Csharp :: c# Class instance 
Csharp :: c# entity mvc get user from razor layout 
Csharp :: c # 
Csharp :: if exist request c# 
Csharp :: executesqlinterpolatedasync stored procedure 
Csharp :: how to extract data from a document using c# 
Csharp :: how to change font text mesh pro 
Csharp :: mono cast 
Csharp :: tomatch jest 
Csharp :: convert string csv line to list c# 
Csharp :: c# break file into words 
Csharp :: Open API support for ASP.NET Core Minimal API 
Csharp :: unity mix gradient colors 
Csharp :: C# MemoryStream - Timeouts are not supported on this stream 
Csharp :: mental retardation 
Csharp :: C# declare object with values 
Csharp :: unity set particle properties through script 
Csharp :: Maximize Print Preview 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =