0

I found the implementation of WPF transparent window WITH BORDERS here C# WPF transparent window with a border. I tried implementing it, but my window is not transparent. No errors come out. Below are the xaml and code behind.

<Window x:Class="WpfApplication9.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Extended Glass in WPF" Height="300" Width="400" Background="Transparent" Loaded="Window_Loaded"> <Grid ShowGridLines="True" Background="Transparent"> </Grid> </Window> 

Code behind

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Runtime.InteropServices; using System.Drawing; namespace WpfApplication9 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { var transparancyConverter = new TransparancyConverter(this); transparancyConverter.MakeTransparent(); } [StructLayout(LayoutKind.Sequential)] public struct MARGINS { public int cxLeftWidth; // width of left border that retains its size public int cxRightWidth; // width of right border that retains its size public int cyTopHeight; // height of top border that retains its size public int cyBottomHeight; // height of bottom border that retains its size }; [DllImport("DwmApi.dll")] public static extern int DwmExtendFrameIntoClientArea( IntPtr hwnd, ref MARGINS pMarInset); } class TransparancyConverter { private readonly Window _window; public TransparancyConverter(Window window) { _window = window; } public void MakeTransparent() { var mainWindowPtr = new WindowInteropHelper(_window).Handle; var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); if (mainWindowSrc != null) if (mainWindowSrc.CompositionTarget != null) mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0); var margins = new Margins { cxLeftWidth = 0, cxRightWidth = Convert.ToInt32(_window.Width) * Convert.ToInt32(_window.Width), cyTopHeight = 0, cyBottomHeight = Convert.ToInt32(_window.Height) * Convert.ToInt32(_window.Height) }; if (mainWindowSrc != null) DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); } [StructLayout(LayoutKind.Sequential)] public struct Margins { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; } [DllImport("DwmApi.dll")] public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins pMarInset); } } 

In addition, I have tried setting basic transparency in the code behind WITHOUT keeping the border. One will also find my xaml and code behind for this below. The created window is not transparent and just has a white background. What am I doing wrong in both of these examples? I feel like there is a common thread.

<Window x:Class="WpfApplication10.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" x:Name="MainWindowGUI" > <Grid> </Grid> </Window> 

Code Behind

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication10 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += MainView_Loaded; } void MainView_Loaded(object sender, RoutedEventArgs e) { Window yourParentWindow = Window.GetWindow(MainWindowGUI); yourParentWindow.WindowStyle = WindowStyle.None; yourParentWindow.AllowsTransparency = true; SolidColorBrush Transparent = new SolidColorBrush(System.Windows.Media.Colors.Transparent); yourParentWindow.Background = Transparent; } } } 
1
  • I'm using Windows 7. Commented Jul 12, 2016 at 17:26

1 Answer 1

1

You need to set AllowsTransparency="True"; for that to work, you're required to set WindowStyle="None". WindowStyle="None" will lose you your non-client area (borders, titlebar, min/max/close buttons), but that's easy enough to work around.

<Window x:Class="WpfApplication9.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Extended Glass in WPF" Height="300" Width="400" Background="Transparent" AllowsTransparency="True" WindowStyle="None" > <Grid ShowGridLines="True" Background="Transparent"> </Grid> </Window> 
Sign up to request clarification or add additional context in comments.

16 Comments

The point with the above code, though, is that I want a WindowStyle. The reference Stack Over Flow response is supposed to solve that.
@Ben You. Can't. Have. One. The non-client area is OS stuff. You might fake up something by creating a second window with nothing but a titlebar and kludging stuff to make the transparent window act like that's its titlebar.
I can't tell if that's sarcasm. If so, LOL. If not, please see the cited post. This code should enable me to do that.
@Ben Should, but it doesn't?
Exactly. I can't figure out what I've written wrong.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.