问题描述
我正在尝试使用传统 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 Window 类(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. 使您的程序集 com 可见(在项目属性的 Build 选项卡中注册 COM 互操作键)并为其分配一个强名称(请参阅签名选项卡);使用 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 elease 文件夹中生成 your_wpf_lib.tlb 文件
5.Once all above done you going to have a your_wpf_lib.tlb file generated in the debug elease folder
6.在你的 c++ 应用程序中(我猜你有它的源代码并且可以重新编译),添加以下行:
6.In your c++ application (I guess you have sources for it and can recompile), add following line:
进口"C:full_path_to_your_tlbyour_wpf_lib.tlb"
import "C:full_path_to_your_tlbyour_wpf_lib.tlb"
这应该会在您的 win32 项目调试输出文件夹中生成适当的 tlh 文件.
this should generate appropriate tlh file in the your win32 project debug output folder.
7.现在您可以从 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 互操作概述
希望这会有所帮助,问候
hope this helps, regards
这篇关于是否可以将 WPF 窗口打包为 COM 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!