[英]WPF child window Aero glass displays incorrectly
我有一個WPF窗口,在SourceInitialized
事件期間自動啟用玻璃。 這非常有效。 我將使用最簡單的示例(只有一個窗口對象)來演示問題所在。
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
。 當我在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;
}
問題是你將MARGINS定義為一個類。 您會注意到,如果您嘗試為邊距使用不同的值集(例如,每條邊上10個像素),它仍將嘗試填充整個區域。 另外,正如我前幾天在評論中提到的那樣,你會注意到即使在未以模態顯示的原始窗口中,你的右下角也有一個神器。 如果只是將MARGINS從類更改為結構,則不會發生問題。 例如
[StructLayout(LayoutKind.Sequential)]
private struct MARGINS
或者你可以讓MARGINS成為一個類,但是你應該改變定義DwmExtendFrameIntoClientArea的方式。 例如
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, [MarshalAs(UnmanagedType.LPStruct)] MARGINS pMargins);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.