2012-10-13 36 views
1

我有一个WPF窗口,在其SourceInitialized事件期间可以使玻璃自动启动。这工作完美。我将使用最简单的例子(只有一个窗口对象)来说明问题出在哪里。WPF子窗口Aero玻璃显示不正确

public partial class MainWindow : Window 
{ 
    public bool lolz = false; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.SourceInitialized += (x, y) => 
      { 
       AeroExtend(this); 
      }; 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (!lolz) 
     { 
      MainWindow mw = new MainWindow(); 
      mw.lolz = true; 
      mw.ShowDialog(); 
     } 
    } 
} 

这产生了两个MainWindow s。当我在Visual Studio中调试时,一切都按预期工作。 Perfect!

当我没有调试运行,没有那么多。 Not perfect...

子窗口有一个奇怪的,不正确的应用玻璃框架......但只有当直接运行它没有Visual Studio调试。相同的代码运行两次,但结果不同。当我创建第二个窗口时,无关紧要,我已将它绑定到具有相同输出的按钮单击。

任何想法?

编辑:这里是代码为AeroExtend

[DllImport("dwmapi.dll")] 
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins); 

[DllImport("dwmapi.dll", PreserveSig = false)] 
private static extern bool DwmIsCompositionEnabled(); 

[StructLayout(LayoutKind.Sequential)] 
private class MARGINS 
    { 
     public MARGINS(Thickness t) 
     { 
      cxLeftWidth = (int)t.Left; 
      cxRightWidth = (int)t.Right; 
      cyTopHeight = (int)t.Top; 
      cyBottomHeight = (int)t.Bottom; 
     } 
     public int cxLeftWidth, cxRightWidth, 
      cyTopHeight, cyBottomHeight; 
} 

... 

static public bool AeroExtend(this Window window) 
{ 
    if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled()) 
    { 
     IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle; 
     HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); 
     mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent; 

     window.Background = System.Windows.Media.Brushes.Transparent; 

     MARGINS margins = new MARGINS(new Thickness(-1)); 

     int result = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); 
     if (result < 0) 
     { 
      return false; 
     } 
     return true; 
    } 
    return false; 
} 
+0

实际上,您可以在第一张图片的右下角看到一个小的神器。它看起来像是第二个客户区的全部高度。 AeroExtend的代码是什么? – AndrewS

+0

@AndrewS用代码编辑我的问题。 –

回答

3

摘录我使用的问题是,你必须定义为类的利润率。您会注意到,如果您尝试使用一组不同的值(例如每个边上有10个像素),它仍然会尝试填充整个区域。正如我前几天在我的评论中提到的,即使在没有模式显示的原始窗口中,您也会注意到在右下角有一个神器。如果您只是将MARGINS从一个类更改为一个结构,那么问题就不会发生。例如

[StructLayout(LayoutKind.Sequential)] 
private struct MARGINS 

或者你可以离开利润率类,但那么你应该改变DwmExtendFrameIntoClientArea的定义方式。例如

[DllImport("dwmapi.dll")] 
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, [MarshalAs(UnmanagedType.LPStruct)] MARGINS pMargins); 
+0

但实际上,只是使MARGINS成为一个结构。 – BoltClock

+0

我很担心这件事很简单。谢谢! –