First you need to set AllowTransperency to True . Then you can set the window background to a transparent (to the desired extent) brush:
<Window x:Class="MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStyle="None" AllowsTransparency="True" Background="{DynamicResource WindowBackground}"> <Window.Resources> <SolidColorBrush x:Key="WindowBackground" Color="White" Opacity="0.5"/> </Window.Resources> ... </Window>
Note that AllowTransperency can only be set to True if WindowStyle set to None .
Update: If you do not want to install WindowStyle in None and want to keep the standard borders and window buttons, there is an alternative that will only work on Windows Vista / 7 with the Windows Aero theme.
The trick is that you can expand the Glass area to the entire window using the following code:
public static class WindowUtils { /// <summary> /// Extends the glass area into the client area of the window /// </summary> /// <param name="window">Window to extend the glass on.</param> /// <param name="thikness">Thickness of border to extend.</param> public static void ExtendGlass(this Window window, Thickness thikness) { try { int isGlassEnabled = 0; Win32.DwmIsCompositionEnabled(ref isGlassEnabled); if (Environment.OSVersion.Version.Major > 5 && isGlassEnabled > 0) { // Get the window handle var helper = new WindowInteropHelper(window); var mainWindowSrc = HwndSource.FromHwnd(helper.Handle); if (mainWindowSrc != null) { if (mainWindowSrc.CompositionTarget != null) { mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent; } // Get the dpi of the screen System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowSrc.Handle); float dpiX = desktop.DpiX / 96; float dpiY = desktop.DpiY / 96; // Set Margins var margins = new MARGINS { cxLeftWidth = (int)(thikness.Left * dpiX), cxRightWidth = (int)(thikness.Right * dpiX), cyBottomHeight = (int)(thikness.Bottom * dpiY), cyTopHeight = (int)(thikness.Top * dpiY) }; window.Background = Brushes.Transparent; Win32.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); } } else { window.Background = SystemColors.WindowBrush; } } catch (DllNotFoundException) { } } } public class Win32 { [DllImport("dwmapi.dll")] public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); [DllImport("dwmapi.dll")] public static extern int DwmIsCompositionEnabled(ref int en); [DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y); [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int ClientToScreen(IntPtr hWnd, [In, Out] POINT pt); } [StructLayout(LayoutKind.Sequential)] public struct MARGINS { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; } [StructLayout(LayoutKind.Sequential)] public class POINT { public int x = 0; public int y = 0; }
To expand the glass to the entire window, you need to call the ExtendGlass extension method in the window's SizeChanged event handler and pass Thickness , which covers the whole window:
public MyWindow() { InitializeComponent(); SizeChanged += OnSizeChanged; } private void OnSizeChanged(object sender, SizeChangedEventArgs e) { double horisontalThickness = Width / 2; double verticalThickness = Height / 2; var glassThickness = new Thickness(horisontalThickness, verticalThickness, horisontalThickness, verticalThickness); this.ExtendGlass(glassThickness); }
Pavlo Glazkov
source share