Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

Create a transparent windows on WinUI 3 latest

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219-thinking-32k’s answer.
  1. User

    Create a transparent windows on WinUI 3 latest

  2. Assistant

    Here’s a little step-by-step recipe for creating a truly transparent (or even acrylic-blurred) WinUI 3 window. We’ll:

    1. Create a WinUI 3 “Desktop” project (Windows App SDK)
    2. Set the window’s XAML background to Transparent
    3. P/Invoke into Win32’s SetWindowCompositionAttribute to turn on blur/transparent

    1) New WinUI 3 Desktop App

    File → New Project → “Blank App, Packaged (WinUI 3 in Desktop)” (Windows App SDK).

    2) XAML: Transparent Background

    Open your MainWindow.xaml (or whatever you named it) and set its Background to Transparent:

    <Window
      x:Class="MyApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Transparent WinUI 3 Window"
      Width="800" Height="600"
      Background="Transparent">
    
      <!-- Your UI goes here -->
      <Grid>
        <TextBlock Text="Hello, transparent world!" 
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontSize="24"/>
      </Grid>
    </Window>
    

    3) Code-behind: Enable Acrylic / Transparent

    In MainWindow.xaml.cs add:

    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Microsoft.UI.Xaml;
    using System;
    using System.Runtime.InteropServices;
    using Windows.Graphics;
    
    namespace MyApp
    {
      public sealed partial class MainWindow : Window
      {
        public MainWindow()
        {
          this.InitializeComponent();
    
          // Optional: extend content into the title bar
          var appWindow = GetAppWindowForCurrentWindow();
          appWindow.TitleBar.ExtendsContentIntoTitleBar = true;
          appWindow.TitleBar.ButtonBackgroundColor = Colors.Transparent;
          appWindow.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
    
          // Apply blur/transparent
          EnableBlurTransparent();
        }
    
        private AppWindow GetAppWindowForCurrentWindow()
        {
          IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
          return AppWindow.GetFromWindowId(
            Win32Interop.GetWindowIdFromWindow(hWnd));
        }
    
        private void EnableBlurTransparent()
        {
          IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
    
          var accent = new AccentPolicy
          {
            AccentState = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND,
            AccentFlags = 2,                // default flags
            GradientColor = 0x99FFFFFF,     // 0xAABBGGRR: AA=opacity
            AnimationId = 0
          };
          int size = Marshal.SizeOf(accent);
          IntPtr accentPtr = Marshal.AllocHGlobal(size);
          Marshal.StructureToPtr(accent, accentPtr, false);
    
          var data = new WindowCompositionAttributeData
          {
            Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
            SizeOfData = size,
            Data = accentPtr
          };
    
          SetWindowCompositionAttribute(hWnd, ref data);
          Marshal.FreeHGlobal(accentPtr);
        }
    
        #region PInvoke Stuff
    
        [DllImport("user32.dll")]
        private static extern int SetWindowCompositionAttribute(
          IntPtr hwnd, ref WindowCompositionAttributeData data);
    
        private enum AccentState
        {
          ACCENT_DISABLED = 0,
          ACCENT_ENABLE_GRADIENT = 1,
          ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
          ACCENT_ENABLE_BLURBEHIND = 3,
          ACCENT_ENABLE_ACRYLICBLURBEHIND = 4,
          ACCENT_INVALID_STATE = 5
        }
    
        [StructLayout(LayoutKind.Sequential)]
        private struct AccentPolicy
        {
          public AccentState AccentState;
          public int AccentFlags;
          public uint GradientColor;
          public int AnimationId;
        }
    
        private enum WindowCompositionAttribute
        {
          // ...
          WCA_ACCENT_POLICY = 19
          // ...
        }
    
        [StructLayout(LayoutKind.Sequential)]
        private struct WindowCompositionAttributeData
        {
          public WindowCompositionAttribute Attribute;
          public IntPtr Data;
          public int SizeOfData;
        }
        #endregion
      }
    }
    

    What’s going on?

    • We P/Invoke into SetWindowCompositionAttribute, passing an AccentPolicy struct.
    • AccentState.ACCENT_ENABLE_TRANSPARENTGRADIENT gives a true transparency (no blur),
      while ACCENT_ENABLE_BLURBEHIND or ACCENT_ENABLE_ACRYLICBLURBEHIND gives you blur or acrylic.
    • The GradientColor is ARGB (0xAABBGGRR); the alpha byte (AA) controls opacity.

    That’s it!
    Run your app and you’ll have a Window whose background is see-through (and optionally blurred). 🎉

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.