是否可以将WPF窗口打包为COM对象 [英] Is it possible to package WPF window as COM Object

查看:107
本文介绍了是否可以将WPF窗口打包为COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用来自一个旧的c ++非托管gtk gui应用程序的WPF窗口。是否可以打包WPF窗口(包括xaml文件),并将其在c ++ gui应用程序中用作常规com对象。你预见到这种方法的任何问题或问题吗?

I am trying to use a WPF window from a legacy c++ unmanaged gtk gui application. Is it possible to package the WPF window (including the xaml file) and use it in the c++ gui application as a regular com object. Do you foresee any issues or problems with this approach?

如果可能,任何链接或教程或任何关于如何做的建议将是非常有帮助的。
感谢。

If possible any links or tutorials or any suggestions on how to do it will be very helpful. Thanks.

推荐答案

我不知道有任何在线教程,但它不应该是一个大问题。我试过实现这样的smth,它对我来说很好,下面是顺序,如果我完成了以下步骤:

I'm not aware of any tutorials online for doing this; but it shouldn't be a big problem at all. I've tried implementing smth like this and it worked fine for me, below is sequence if steps I've done:

1.添加一个wpf用户控件 wpf自定义控件库到您的解决方案。

1.add a "wpf user control" or "wpf custom control" library to your solution.

2.在新项目中添加一个新的WPF窗口类(Add-> Window - > ...)。然后添加什么wpf控制你喜欢到你的新窗口只是为了检查它是否工作

2.add a new WPF Window class (Add->Window->...) into the new project. Then add what ever wpf controls you like to your new window just to check if it works later

3.添加新的类和接口到库项目,并定义它像一个例如:

3.add new class and interface to the library project and define it like an example below:

[ComVisible(true)]
[Guid("694C1820-04B6-4988-928F-FD858B95C881")]
public interface ITestWPFInterface
{
    [DispId(1)]
    void TestWPF();
}

[ComVisible(true)]
[Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123F"),
ClassInterface(ClassInterfaceType.None)]
public class TestWPFInterface : ITestWPFInterface
{
    public void TestWPF()
    {
        Window1 form = new Window1();
        form.Show();
    }
}

4.使您的程序集可见项目属性的构建选项卡中的interop键),并为其分配一个强名称(请参阅签名选项卡);使用sn实用程序生成密钥

4.make your assembly com visible (Register For COM interop key in the Build tab of the project properties) and assign a strong name to it (see signing tab); generate key with sn utility

5.完成上述所有操作后,您将在debug\release文件夹中生成一个your_wpf_lib.tlb文件

5.Once all above done you going to have a your_wpf_lib.tlb file generated in the debug\release folder

6.在你的c ++应用程序(我想你有它的源代码并可以重新编译),添加以下行:

6.In your c++ application (I guess you have sources for it and can recompile), add following line:


import
C:\full_path_to_your_tlb\your_wpf_lib.tlb

import "C:\full_path_to_your_tlb\your_wpf_lib.tlb"

这应该会生成相应的tlh文件您的win32项目调试输出文件夹。

this should generate appropriate tlh file in the your win32 project debug output folder.

现在可以从c ++代码调用您的表单:

7.now you can call your form from the c++ code:

TestWPFForms::ITestWPFInterfacePtr comInterface(__uuidof(TestWPFForms::TestWPFInterface));
comInterface->TestWPF();

这应该会显示您的wpf表单。

this should show your wpf form.

我相信下面的链接可能对您有用:

Also I believe links below might be useful for you:

从非托管C ++代码调用托管.NET C#COM对象

WPF和Win32互操作概述

希望这有助于您,

这篇关于是否可以将WPF窗口打包为COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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