我有一個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中調試時,一切都按預期工作。
當我沒有調試運行,沒有那麼多。
子窗口有一個奇怪的,不正確的應用玻璃框架......但只有當直接運行它沒有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;
}
實際上,您可以在第一張圖片的右下角看到一個小的神器。它看起來像是第二個客戶區的全部高度。 AeroExtend的代碼是什麼? – AndrewS
@AndrewS用代碼編輯我的問題。 –