SourceInitialized
イベント中にGlassを有効にするWPFウィンドウがあります。これは完全に機能します。問題がどこにあるかを示すために、可能な限り最も単純な例(1つのウィンドウオブジェクトのみ)を使用します。
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();
}
}
}
これにより、2つMainWindow
のが作成されます。Visual Studioでこれをデバッグすると、すべてが期待どおりに機能します。
デバッグせずに実行すると、それほど多くはありません。
子ウィンドウには、奇妙な、誤って適用されたガラスフレームがあります...ただし、VisualStudioのデバッグなしで直接実行した場合のみです。同じコードが2回実行されましたが、結果が異なります。2番目のウィンドウをいつ作成するかは問題ではなく、同じ出力のボタンクリックに関連付けました。
何か案は?
編集:これは私が使用しているコードの抜粋です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;
}