public static void EnableGlass(Window window, Margins margins)
{
if (Environment.OSVersion.Version.Major < 6)
{
return;
}
if (margins.TopHeight == 0 && margins.BottomHeight == 0 && margins.RightWidth == 0 && margins.LeftWidth == 0)
{
margins = new Margins(true);
}
if (window == null)
{
throw new ArgumentNullException("window");
}
IntPtr windowHandle = new WindowInteropHelper(window).Handle;
if (windowHandle == IntPtr.Zero)
{
return;
}
// add Window Proc hook to capture DWM messages
HwndSource source = HwndSource.FromHwnd(windowHandle);
if (source == null)
{
throw new FormatException();
}
source.AddHook(WndProc);
// Set the Background to transparent from Win32 perspective
HwndSource handleSource = HwndSource.FromHwnd(windowHandle);
if (handleSource != null)
{
if (handleSource.CompositionTarget != null)
{
handleSource.CompositionTarget.BackgroundColor = Colors.Transparent;
}
}
// Set the Background to transparent from WPF perspective
window.Background = Brushes.Transparent;
ResetAeroGlass(margins, windowHandle);
}