Remove DropShadow for WPF Advanced Custom Window

I have a WPF application (.NET Framework 4) with a custom window border. I disabled the glass frame using the WPF Shell integration library and draw my own border. However, I want to add DropShadow around the border of the window when it is not maximized. I added the shadow as follows:

private static bool DropShadow(Window window) { try { WindowInteropHelper helper = new WindowInteropHelper(window); int val = 2; int ret1 = DwmSetWindowAttribute(helper.Handle, 2, ref val, 4); if (ret1 == 0) { Margins m = new Margins { Bottom = 0, Left = 0, Right = 0, Top = 0 }; int ret2 = DwmExtendFrameIntoClientArea(helper.Handle, ref m); return ret2 == 0; } else { return false; } } catch (Exception ex) { // Probably dwmapi.dll not found (incompatible OS) return false; } } 

See DropShadow for a WPF Borderless Window for more details.

This solution works great when working with WindowState.Normal ! However, when I maximize the application and DWMWA_NCRENDERING_POLICY off DWMWA_NCRENDERING_POLICY , the background of the window becomes slightly transparent, and most of my controls are completely different from the one I'm used to.

In the following image, you see a maximized state, as it was originally, and with a shadow code. As you can see, it completely changes the transparency of the window with the shadow code: o enter image description here

Is there something I am missing? I read through the DWM function library but cannot find the answer ...

+4
source share
1 answer

After a while, I reviewed the problem from a different angle and realized the best solution:

 public class GlassWindow : Window { [SuppressUnmanagedCodeSecurity] internal static class DwmNativeMethods { [StructLayout(LayoutKind.Sequential)] internal struct DwmMargins { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; public DwmMargins(bool fullWindow) { this.cxLeftWidth = this.cxRightWidth = this.cyTopHeight = this.cyBottomHeight = fullWindow ? -1 : 0; } } [DllImport("DwmApi.dll")] internal static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref DwmMargins m); [DllImport("DwmApi.dll")] internal static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize); } private IntPtr windowHandle; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); WindowInteropHelper interopHelper = new WindowInteropHelper(this); this.windowHandle = interopHelper.Handle; this.ToggleAreoGlass(this.WindowState != WindowState.Maximized); this.StateChanged += this.GlassWindowStateChanged; } private void ToggleAreoGlass(bool value) { // Enable NcRenderingPolicy int attrValue = 2; int result = DwmNativeMethods.DwmSetWindowAttribute(this.windowHandle, 2, ref attrValue, 4); if (result == 0) { // Extend DwmFrame DwmNativeMethods.DwmMargins margins = new DwmNativeMethods.DwmMargins(value); DwmNativeMethods.DwmExtendFrameIntoClientArea(this.windowHandle, ref margins); } } private void GlassWindowStateChanged(object sender, EventArgs e) { this.ToggleAreoGlass(this.WindowState != WindowState.Maximized); } } 
+2
source

Source: https://habr.com/ru/post/1387171/


All Articles