当父窗口关闭时,对话框应关闭。
public sealed partial class ModelessDialog : Window
{
public ModelessDialog(SizeInt32 windowSize, Window _parent)
{
this.InitializeComponent();
OverlappedPresenter presenter = (OverlappedPresenter)this.AppWindow.Presenter;
presenter.IsResizable = true;
this.AppWindow.SetPresenter(presenter);
this.AppWindow.IsShownInSwitchers = false;
this.AppWindow.Resize(windowSize);
IntPtr windowHandle = App.GetWindowHandle(this);
IntPtr parentHandle = App.GetWindowHandle(_parent);
SetParent(windowHandle, parentHandle);
}
public IntPtr GetWindowHandle(in Window _window)
{
return WinRT.Interop.WindowNative.GetWindowHandle(_window);
}
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
}
public partial class ModelessWindow : Window
{
public ModelessWindow(SizeInt32 size, Window parent) : base()
{
parent.Closed += (s, e) => this.Close();
this.AppWindow.MoveAndResize(
new RectInt32(
parent.AppWindow.Position.X,
parent.AppWindow.Position.Y,
size.Width,
size.Height));
}
}