private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
            // Ignore clicks if desktop composition isn't enabled
            case DwmApiInterop.WM_NCHITTEST:
                if (DwmApiInterop.IsCompositionEnabled() &&
                    DwmApiInterop.IsOnClientArea(hwnd, msg, wParam, lParam) &&
                    IsOnExtendedFrame(lParam.ToInt32()))
                {
                    handled = true;
                    return(new IntPtr(DwmApiInterop.HTCAPTION));
                }

                return(IntPtr.Zero);

            // Also toggle window frame painting on this window when desktop composition is toggled
            case DwmApiInterop.WM_DWMCOMPOSITIONCHANGED:
                try
                {
                    AdjustWindowFrame();
                }
                catch (InvalidOperationException)
                {
                    FallbackPaint();
                }
                return(IntPtr.Zero);

            default:
                return(IntPtr.Zero);
            }
        }
 private void AdjustWindowFrame()
 {
     if (DwmApiInterop.IsCompositionEnabled())
     {
         ExtendFrameIntoClientArea(0, 0, (int)(RootGrid.ActualHeight - ContentRectangle.ActualHeight), 0);
     }
     else
     {
         FallbackPaint();
     }
 }
        private void ExtendFrameIntoClientArea(int left, int right, int top, int bottom)
        {
            var margins = new Margins
            {
                cxLeftWidth    = left,
                cxRightWidth   = right,
                cyTopHeight    = top,
                cyBottomHeight = bottom
            };
            var hresult = DwmApiInterop.ExtendFrameIntoClientArea(_hwnd, ref margins);

            if (hresult == 0)
            {
                if (_hsource.CompositionTarget != null)
                {
                    _hsource.CompositionTarget.BackgroundColor = Colors.Transparent;
                }
                Background = Brushes.Transparent;
            }
            else
            {
                Trace.WriteLine("Could not extend window frames in the main window.");
            }
        }