public static void ExcludeElementFromAeroGlass(FrameworkElement element, Window window)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
if (window == null)
{
throw new ArgumentNullException("window");
}
IntPtr handle = new WindowInteropHelper(window).Handle;
if (!IsGlassEnabled)
{
return;
}
// calculate total size of window non-client area
var handleSource = PresentationSource.FromVisual(window) as HwndSource;
var windowRect = new Rect();
var clientRect = new Rect();
if (handleSource != null)
{
NativeMethods.GetWindowRect(handleSource.Handle, ref windowRect);
NativeMethods.GetClientRect(handleSource.Handle, ref clientRect);
}
var nonClientSize =
new Size(
(windowRect.Right - windowRect.Left) - (double)(clientRect.Right - clientRect.Left),
(windowRect.Bottom - windowRect.Top) - (double)(clientRect.Bottom - clientRect.Top));
// calculate size of element relative to non-client area
GeneralTransform transform = element.TransformToAncestor(window);
Point topLeftFrame = transform.Transform(new Point(0, 0));
Point bottomRightFrame =
transform.Transform(
new Point(element.ActualWidth + nonClientSize.Width, element.ActualHeight + nonClientSize.Height));
// Create a margin structure
var margins = new Margins(
(int)topLeftFrame.X,
(int)topLeftFrame.Y,
(int)(window.ActualWidth - bottomRightFrame.X),
(int)(window.ActualHeight - bottomRightFrame.Y));
// Extend the Frame into client area
if (NativeMethods.DwmExtendFrameIntoClientArea(handle, ref margins) != 0)
{
// throw new InvalidOperationException();
}
}