2011-12-20 34 views
4

我有一个自定义窗口边框的WPF应用程序(.NET Framework 4)。我已使用WPF Shell Integration Library禁用了玻璃边框,并绘制了我自己的边框。不过,我想在未最大化时在窗口边框周围添加DropShadow。我添加了一个阴影像这样:删除最大化WPF自定义窗口的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工作时DropShadow for WPF Borderless Window

这个解决方案正常工作!但是,当我最大化应用程序并禁用DWMWA_NCRENDERING_POLICY窗口的背景变得稍微透明,我的大多数控件渲染完全不同于我以前。

在下图中,您会看到最初的最大化状态以及影子代码。正如你所看到的,它完全改变了影子代码窗口的透明度:0 enter image description here

有什么我失踪了吗?我一直在读通过DWM Function library但无法找到答案......

+1

它可能使你受益在这里使用[Snoop](http://snoopwpf.codeplex.com/) - 这是一个非常好的WPF间谍工具,它允许您查看WPF应用程序的所有不同渲染层以及当前属性值。 – 2011-12-22 12:11:37

+0

侦听我的应用程序时,没有出现任何内容,两个实例完全相同!它在winapi中出现错误,我只是不知道为什么......因为我在应用程序最大化时很好地禁用了渲染策略! – Kolky 2011-12-23 10:51:45

回答

2

过了一会儿,我重新从不同的角度的问题,并想出一个更好的解决方案:

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