o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
Create a transparent windows on WinUI 3 latest
- User
Create a transparent windows on WinUI 3 latest
- Assistant
Here’s a little step-by-step recipe for creating a truly transparent (or even acrylic-blurred) WinUI 3 window. We’ll:
- Create a WinUI 3 “Desktop” project (Windows App SDK)
- Set the window’s XAML background to Transparent
- P/Invoke into Win32’s
SetWindowCompositionAttributeto 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 itsBackgroundtoTransparent:<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.csadd: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 anAccentPolicystruct. AccentState.ACCENT_ENABLE_TRANSPARENTGRADIENTgives a true transparency (no blur),
whileACCENT_ENABLE_BLURBEHINDorACCENT_ENABLE_ACRYLICBLURBEHINDgives you blur or acrylic.- The
GradientColoris 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). 🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.