2012-10-13 35 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

我很擔心這件事很簡單。謝謝! –