删除最大化WPF自定义窗口的DropShadow

删除最大化WPF自定义窗口的DropShadow,wpf,windows,winapi,transparency,Wpf,Windows,Winapi,Transparency,我有一个带有自定义窗口边框的WPF应用程序(.NET Framework 4)。我已使用禁用玻璃边框,并绘制自己的边框。但是,我想在未最大化时在窗口的边界周围添加一个DropShadow。我添加了这样一个阴影: private static bool DropShadow(Window window) { try { WindowInteropHelper helper = new WindowInteropHelper(window); int

我有一个带有自定义窗口边框的WPF应用程序(.NET Framework 4)。我已使用禁用玻璃边框,并绘制自己的边框。但是,我想在未最大化时在窗口的边界周围添加一个DropShadow。我添加了这样一个阴影:

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;
    }
}
有关更多详细信息,请参阅:

使用
WindowState.Normal
时,此解决方案工作正常!但是,当我最大化应用程序并禁用
DWMWA\u NCRENDERING\u策略时
窗口的背景变得略微透明,我的大多数控件呈现出与我以前完全不同的效果

在下图中,您可以看到最大化状态与最初一样,并使用阴影代码。正如您所见,它完全改变了窗口的透明度,阴影代码为:o


有什么我遗漏的吗?我一直在通读这本书,但找不到答案

过了一会儿,我从另一个角度重新审视了这个问题,并找到了更好的解决方案:

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);
    }
}

在这里使用它可能会使您受益-它是一个非常好的WPF spy工具,允许您查看WPF应用程序的所有不同渲染层,以及当前属性值。窥探我的应用程序时没有出现任何问题,两个实例完全相同!winapi中的某个地方出了问题,我只是不知道为什么。。当应用程序最大化时,我很好地禁用了呈现策略!