我可以使用 C++/CLI (.NET Winforms/WPF ) 为用本机 C + 编写的应用程序提供 GUI C++
我有一个用 C & 编写的应用程序C++。现在,我需要为此应用程序提供一个 GUI。 MFC 对我来说是最好的选择。但我对MFC不熟悉。
那么我可以使用.NET 为此构建GUI 吗?如果是这样,怎么办?请说清楚。
如果我可以使用.NET,我想我也可以使用WPF,对吗?
I've an app written C & C++. Now, I need to provide a GUI for this app. MFC is the best option for me. But I'm not familiar with MFC.
So can I use .NET to build GUI for this? If so, How? Please be clear.
If I can use .NET I guess I can use WPF too right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
技术上可以用 C++/CLI 编写 GUI,但我强烈建议不要这样做。 C++/CLI 非常适合围绕本机 C++ 编写 .NET 包装器并将其公开给其他 .NET 语言,但除此之外就没有什么了。
就您而言,如果您确实决定使用 WinForms/WPF,那么我建议使用 C++/CLI 围绕您的 C++ 代码创建一个包装器,然后在 C# 中构建实际的 GUI。
否则,C++ 库如 Qt 或 wxWidgets 也足以制作“本机”C++ GUI。
这里是 C++/CLI 的快速介绍。这是 C++/CLI 入门的快速入门指南。构建 C++/CLI DLL 后,您只需将其添加为对 C# 项目和 It Just Works(tm) 的引用即可。
You can technically write a GUI in C++/CLI, but I would highly discourage it. C++/CLI is good for writing .NET wrappers around native C++ and exposing it to other .NET languages, but not much else.
In your case, if you're really set on using WinForms/WPF, then I would suggest using C++/CLI to create a wrapper around your C++ code and then building the actual GUI in C#.
Otherwise, a C++ library like Qt or wxWidgets would also suffice for doing a "native" C++ GUI.
Here's a quick introduction to C++/CLI. This is a quick-start guide for getting started with C++/CLI. Once you build a C++/CLI DLL, you can just add it as a reference to your C# project and It Just Works(tm).
将 C++ 应用程序组织为“服务器”,导出可由 GUI“客户端”调用的函数。将此 C++ 代码构建为 DLL,导出所述函数。将 GUI 应用程序创建为 .NET EXE,并让它使用平台调用 (P/Invoke) 调用 DLL 中的所述函数。
Organize the C++ app as a "server", exporting functions which can be called by a GUI "client". Build this C++ code as a DLL, exporting said functions. Create your GUI app as a .NET EXE and let it call said functions in your DLL using Platform Invoke (P/Invoke).
这本书还值得特别提及,因为它涵盖了高级主题以及将 WPF 与 C++/CLI 结合使用:
C++/CLI 实践 (Manning)
This book also deserves particular mention since it covers advanced topics and using WPF with C++/CLI:
C++/CLI in Action (Manning)
取决于你是否想学习另一种语言?
如果您选择 C++/CLI 或 C# 路线,则在开始使用 GUI 之前您必须熟悉这些语言。
是的,MFC 不是最好的,但至少您仍然可以使用 C++。
这似乎是 Microsoft 希望您使用的,因为他们的大多数视频教程都是基于 MFC 的:
http://msdn.microsoft.com/en-us/visualc/bb693459。 aspx
如果您选择 WinForms C++/CLI 路线,Visual Studio 实际上附带了一个用于此目的的模板。即使 2010 也没有附带像 C# 这样的 WPF 项目模板,所以您应该会收到这样的消息:Microsoft 希望您使用 C# 来处理 GUI 内容。
无论如何,如果您要移植的是一个简单的应用程序或程序,那么使用 WinForms 并不难。实际上,它只是比使用 C# 难一点,因为您可以在 Visual Studio 中使用相同的 GUI 编辑器,但与使用 C# 相比,您必须手动编写更多代码。
而且您必须了解 Microsoft 的 C++/CLI,因为模板会自动生成其中的代码,并且您需要了解它的作用,这样您就可以忽略其中的大部分内容。
Ivor Horton 的 Visual C++ 2010 入门 和 Visual C++ 2008 如何编程(第 2 版)~ Paul J. Deitel、Harvey M. Deitel 是其中的 2 篇我见过的唯一一本涵盖 WinForm/C++/CLI 编程的书籍,因此您可能想看看这些书,或者按照每个人的推荐选择 C#。
Depends on if you want to learn another language?
If you choose the C++/CLI or C# route you will have to get familiar with those languages before you even start with your GUI.
Yeah MFC isn't the greatest but at least you can still use C++ with it.
And that seems to be what Microsoft wants you to use since most of their video tutorials are MFC based:
http://msdn.microsoft.com/en-us/visualc/bb693459.aspx
If you choose the WinForms C++/CLI route Visual Studio actually ships with a template for this. Even 2010 doesn't ship with a WPF project template like C# though so you should get the message that Microsoft wants you to use C# for GUI stuff.
Anyways, if it's a trivial app or program you are porting it's not that hard using WinForms. Actually, it's just a bit harder than using C# since you get to use the same GUI editor in Visual Studio but you have to write a lot more of the code by hand than using C#.
And you have to know Microsoft's C++/CLI since the template will autogenerate code in that and you need to understand what it does so you can ignore most of it.
Ivor Horton's Beginning Visual C++ 2010 and Visual C++ 2008 How to Program (2nd Edition) ~ Paul J. Deitel, Harvey M. Deitel are 2 of the only books I've seen that cover WinForm/C++/CLI programming so you might want to look at that or just go with C# as everyone recommends.