我有一个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用代码编辑我的问题。 –