Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取WindowsFormsHost的父控件?_C#_Wpf_Winforms - Fatal编程技术网

C# 如何获取WindowsFormsHost的父控件?

C# 如何获取WindowsFormsHost的父控件?,c#,wpf,winforms,C#,Wpf,Winforms,在我的应用程序中,在WindowsFormsHost中添加我的控件(WF),并在Windows(WPF)中添加此主机 查询: 现在我需要从“网格”(在WF项目中而不是WPF中)获取“testWindow”的句柄值,以捕获testWindow的图像 我尝试过“grid.Parent作为WinFormsAdapter”,但它是内部类。我发现下面的文章,但它都显示在WPF项目 请有人建议我如何做到这一点 谢谢,尝试从网格中获取WindowsFormsHost。一旦你有了它,你可以试着找到Window

在我的应用程序中,在WindowsFormsHost中添加我的控件(WF),并在Windows(WPF)中添加此主机

查询: 现在我需要从“网格”(在WF项目中而不是WPF中)获取“testWindow”的句柄值,以捕获testWindow的图像

我尝试过“grid.Parent作为WinFormsAdapter”,但它是内部类。我发现下面的文章,但它都显示在WPF项目

请有人建议我如何做到这一点


谢谢,

尝试从
网格中获取
WindowsFormsHost
。一旦你有了它,你可以试着找到
WindowsFormsHost
的父项

Control parent = grid.Parent;
Reflection.Assembly adapterAssembly = GetType(WindowsFormsHost).Assembly;
Type adapterType = adapterAssembly.GetType("System.Windows.Forms.Integration.WinFormsAdapter");
object adapterParent = adapterType.InvokeMember("_host", BindingFlags.NonPublic |BindingFlags.GetField | BindingFlags.Instance, null, adapter, new object[] {});
WindowsFormsHost winHost = adapterParent as WindowsFormsHost;

请注意,上面的代码未经测试,但它旨在帮助您,并有望引导您实现最终目标。

谢谢您的建议。现在我可以获取WindowsFormsHost的父级。但是Handle(IntPtr)值返回为“0”,因此无法获取图像。控制适配器=DataGrid.Parent;组件asm=类型(WindowsFormsHost)。组件;Type=asm.GetType(“System.Windows.Forms.Integration.WinFormsAdapter”);object parent=type.InvokeMember(“_host”,BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance,null,适配器,新对象[]{});WindowsFormsHost=作为WindowsFormsHost的父级;Window tw=(Window)host.Parent;IntPtr twHandle=WindowInteropHelper(tw).Handle;我已经尝试了下面的代码,它运行良好var windows=System.windows.Application.Current.windows;IntPtr twHandle=new System.Windows.Interop.WindowInteropHelper(Windows[2]).Handle;
Control parent = grid.Parent;
Reflection.Assembly adapterAssembly = GetType(WindowsFormsHost).Assembly;
Type adapterType = adapterAssembly.GetType("System.Windows.Forms.Integration.WinFormsAdapter");
object adapterParent = adapterType.InvokeMember("_host", BindingFlags.NonPublic |BindingFlags.GetField | BindingFlags.Instance, null, adapter, new object[] {});
WindowsFormsHost winHost = adapterParent as WindowsFormsHost;