0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WPFをWindowsFormsのOwnerに設定する

Last updated at Posted at 2022-02-15

問題

WPFのウィンドウ(System.Windows.Window)を
WindowsFormsのフォーム(System.Windows.Forms.Form)のOwnerに設定したい。

検索すると一番上に↓が出てきたが欲しい答えではなかったので別解をメモしておく。
WPFウィンドウをWindowsフォームの親に設定する - MSDN

解法

WindowInteropHelperとIWin32Windowを利用する。

// WPFのウィンドウ
var window = new System.Windows.Window();

// WindowsFormsのフォーム
var form = new System.Windows.Forms.Form();

// おまじない
var wih = new System.Windows.Interop.WindowInteropHelper(window);
var wnd = new Win32Window(wih.Handle);

// WPFをWindowsFormsのOwnerに設定
form.Show(wnd);


// IWin32Windowを継承したクラスを用意しておく
class Win32Window : System.Windows.Forms.IWin32Window
{
    public IntPtr Handle { get; }

    public Win32Window(IntPtr handle)
    {
        this.Handle = handle;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?