Wpf窗口中打开WinForm窗口

时间:2021-08-30 12:09:33

获取wpf窗口对应的句柄窗口

using System;
using System.Windows;
using System.Windows.Interop;
using IWin32Window = System.Windows.Forms.IWin32Window;
public class WindowWrapper : IWin32Window
{
/// <summary>
/// 窗口句柄
/// </summary>
public IntPtr Handle { get; private set; }
/// <summary>
/// 将句柄封闭为实现IWin32Window的对象
/// </summary>
/// <param name="handle"></param>
public WindowWrapper(IntPtr handle)
{
Handle = handle;
}
/// <summary> 封装为实现IWin32Window的对象
/// </summary>
/// <param name="window">WPF窗口</param>
public WindowWrapper(Window window)
{
var helper = new WindowInteropHelper(window);
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
Handle = process.MainWindowHandle;
Handle = helper.Handle;
}
/// <summary> 得到某个WPF窗口的窗口句柄 </summary>
/// <param name="window">WPF窗口</param>
/// <returns>窗口句柄</returns>
public IntPtr GetHandle(Window window)
{
return new WindowInteropHelper(window).Handle;
}
}

打开窗口

frmselectwx myfrmwx = new frmselectwx();
myfrmwx.Show(new Utils.WindowWrapper(this));