通过COM Interop以Win32形式托管WinForm和WPF用户控件 [英] Hosting WinForm and WPF user controls in Win32 form via COM Interop

查看:66
本文介绍了通过COM Interop以Win32形式托管WinForm和WPF用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试通过COM Interop在Win32窗体中同时托管WinForm和WPF用户控件.不能同时使用单独的Win32窗体.真的没什么花哨的,只需要将用户控件重新绑定到完全没有其他内容的Win32窗体即可.

WinForm部分工作正常,看起来像这样:

We are trying to host both WinForm and WPF user controls inside Win32 forms via COM Interop. Not at the same time, separate Win32 forms.  Nothing fancy really, just need to reparent the user control to the Win32 form that has nothing else in it at all.

The WinForm part works fine and looks like this:

 

<身体>
内部公共部分类:InternalWinFormUserControl:UserControl
{
公用public InternalWinFormUserControl()
{
InitializeComponent();
}
.[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
.[Guid("CA645972-8330-4FD1-B7D6-9935750F6F18")]
公共部分类WinFormUserControl:IWin32FormsInterop
.{
私有InternalWinFormUserControl uc;
公共public void ShowParented(uint Hwnd)
.{
uc = InternalWinFormUserControl();
WinApi.SetParent(uc.Handle,new IntPtr(Hwnd));
uc.Visible = > true ;
    public partial class InternalWinFormUserControl : UserControl  
    {  
        public InternalWinFormUserControl()  
        {  
            InitializeComponent();  
        }  
    }  
 
    [ComVisible(true)]  
    [ClassInterface(ClassInterfaceType.None)]  
    [Guid("CA645972-8330-4FD1-B7D6-9935750F6F18")]  
    public partial class WinFormUserControl : IWin32FormsInterop  
    {  
        private InternalWinFormUserControl uc;  
 
        public void ShowParented(uint Hwnd)  
        {  
            uc = new InternalWinFormUserControl();  
            WinApi.SetParent(uc.Handle, new IntPtr(Hwnd));  
            uc.Visible = true;  
        }  
    }  
 
 

推荐答案

在WPF中托管本机代码,您可以直接与 HwndSource 一起使用,并使其与

谢谢
When hosting WPF in native code, you could directly work with HwndSource, and reparent it to the WIN32 hwnd parent, no need to put the ElementHost inbetween, have you tried directly working with HwndSource instead?

Thanks


这篇关于通过COM Interop以Win32形式托管WinForm和WPF用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆