将 GUI 添加到 C++ 应用程序的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6233325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to add a GUI to C++ app
提问by Johannes Gerer
I am producing a piece numerical software in C++ and want to add a GUI (mainly for Windows). I know how to produce GUIs using comfortable editors in modern languages like Java or .NET. Now my question is what is the easiest and most comfortable way to add a GUI frontendfor my program. In the choice of the tools am completely free (open source and portability would be nice), but please also keep in mind how much boilerplate code and interfaces that have to be maintained are required if the GUI is implemented in another language (Like C#).
我正在用 C++ 制作一个数字软件,并想添加一个 GUI(主要用于 Windows)。我知道如何使用 Java 或 .NET 等现代语言使用舒适的编辑器生成 GUI。现在我的问题是为我的程序添加 GUI 前端的最简单和最舒适的方法是什么。工具的选择是完全免费的(开源和可移植性会很好),但也请记住,如果 GUI 是用另一种语言(如 C#)实现的,则需要维护多少样板代码和接口.
Please don't suggest switching the whole project from C++! And note that the program does not require not much interaction between the C++ code and the GUI.
请不要建议从 C++ 切换整个项目!请注意,该程序不需要 C++ 代码和 GUI 之间的太多交互。
采纳答案by cardiff space man
The statements about ISO C++ in this answer's comments are poorly edited. None of the solutions presented here would impose on the computational code a requirement of changing to a different dialect of C++. The GUI code itself might be another story.
此答案评论中有关 ISO C++ 的声明编辑不当。此处介绍的解决方案都不会将更改为不同的 C++ 方言的要求强加给计算代码。GUI 代码本身可能是另一回事。
The answers about using Windows Forms with managed C++ are probably the most practical. A lot of the UI-related code would be in a dialect (extension) of C++ where the .NET-garbage-collected pointers would be living alongside the traditional ISO C++ pointers. I haven't used this method but from what I read it might be better than what I have used.
关于将 Windows 窗体与托管 C++ 一起使用的答案可能是最实用的。许多与 UI 相关的代码将使用 C++ 的方言(扩展),其中 .NET 垃圾收集的指针将与传统的 ISO C++ 指针并存。我没有使用过这种方法,但从我读到的内容来看,它可能比我使用过的要好。
MFC might be more practical if you don't want to invest in .NET knowledge for this task. It uses standard C++ constructs to wrap the Windows API. The Windows API uses a particular calling convention which C++ doesn't normally use, and it takes an extension to C++ to work with that, but this is no different than having a C++ program that calls some functions that are extern "C". Visual Studio has a GUI layout tool that is really good at layout of dialogs and at interfacing the dialog widgets to variables that reflect the state of the widgets. I've used this. If you can boil your GUI down to a dialog box then this would be a great option, otherwise you'd be using one of MFC's layout-managed windows.
如果您不想为此任务投资 .NET 知识,MFC 可能更实用。它使用标准的 C++ 构造来包装 Windows API。Windows API 使用 C++ 通常不使用的特定调用约定,并且需要对 C++ 进行扩展才能使用该约定,但这与具有调用某些外部“C”函数的 C++ 程序没有什么不同。Visual Studio 有一个 GUI 布局工具,它非常擅长对话框的布局以及将对话框小部件与反映小部件状态的变量连接起来。我用过这个。如果您可以将 GUI 归结为对话框,那么这将是一个不错的选择,否则您将使用 MFC 的布局管理窗口之一。
Obviously the above options do not handle other platforms you might have in mind.
显然,上述选项无法处理您可能想到的其他平台。
There are also various toolkits that were born on other platforms and have been decently ported to Windows. GTK, QT, FLTK, and WxWindows come to mind. I haven't used any of the dialog-layout/application designer tools that work with those, and I haven't used QT at all. When I last reviewed QT it had a special preprocessing pass for the GUI code. Other than that these portable tool kits are pure ISO C++ as far as I know.
还有各种工具包是在其他平台上诞生的,并且已经很好地移植到了 Windows 上。想到 GTK、QT、FLTK 和 WxWindows。我没有使用过任何与它们配合使用的对话框布局/应用程序设计器工具,而且我根本没有使用过 QT。当我上次查看 QT 时,它对 GUI 代码进行了特殊的预处理。除此之外,据我所知,这些便携式工具包是纯 ISO C++。
As a really-out-there option one could program to the X Window System protocol using "libx". As far as I know this would involve no non-ISO C++ code.
作为一种真正的选择,可以使用“libx”对 X Window System 协议进行编程。据我所知,这不会涉及非 ISO C++ 代码。
回答by shoosh
Qt is a decent choice. It's stable, has a wonderful C++ interface (as opposed to MFC) and has a convenient designer tool.
The overhead of learning it from scratch might however be more that what you're willing to invest. It does have a certain learning curve.
Qt 是一个不错的选择。它很稳定,有一个很棒的 C++ 接口(与 MFC 相对),并且有一个方便的设计器工具。
然而,从头开始学习它的开销可能比你愿意投资的要多。它确实有一定的学习曲线。
回答by Billy ONeal
As much as I love C++, it's difficult to build a GUI with. I'd build a quick C# WinForms application which would let you use things like Visual Studio's visual designers (Drag and drop buttons and such), and call your C++ application using P/Invoke.
尽管我很喜欢 C++,但用它来构建 GUI 还是很困难的。我会构建一个快速的 C# WinForms 应用程序,它可以让您使用 Visual Studio 的可视化设计器(拖放按钮等)之类的东西,并使用 P/Invoke 调用您的 C++ 应用程序。
C++ will often produce smoother and nicer GUIs, but it takes a bit more work out of the box.
C++ 通常会产生更流畅、更好的 GUI,但它需要更多的开箱即用的工作。
回答by Sandeep
回答by x4u
If it's going to be just a simple GUI consisting mostly of standard controls I would do it with MFC. It may be outdated and was never really good, but it's still useful to get a native Windows GUI up and running quickly.
如果它只是一个主要由标准控件组成的简单 GUI,我会用 MFC 来完成。它可能已经过时并且从来都不是很好,但是让原生 Windows GUI 快速启动和运行仍然很有用。
回答by Peter Hayman
I have to chuckle at all the ways to skin this cat. Life is good. My response is a product I manage. XVT can do it for you. The easiest way is let us do it for you or give you a template to get there. It's just a matter of if you have more money than time. Then I'd look at us. It would be the fastest and least amount of effort on your part.
我不得不对所有剥这只猫的皮的方法嗤之以鼻。生活很好。我的回答是我管理的产品。XVT 可以为您做到。最简单的方法是让我们为您做或给您一个模板来实现。这只是你是否有更多的钱而不是时间的问题。那我就看看我们。这将是您最快和最少的努力。
回答by Simon
Depending on what you need, there's a nice imgui made by Mikko floating around that you can simply plug-in and use fairly quickly. It's done in opengl though so it won't be your standard windows-gui but it's really small and really easy to work with. You can download r'lyeh's version from here: http://code.google.com/p/colony9/source/browse/include/goo/imgui.h.
根据您的需要,有一个由 Mikko 制作的不错的 imgui,您可以简单地插入并快速使用它。它是在 opengl 中完成的,所以它不会是你的标准 windows-gui,但它真的很小而且很容易使用。您可以从这里下载 r'lyeh 的版本:http: //code.google.com/p/colony9/source/browse/include/goo/imgui.h。
That's the easiest and most comfortable way I know, it is dependent on SDL though.
这是我所知道的最简单、最舒适的方法,但它依赖于 SDL。
回答by Farshid Zaker
MFC Dialog Based application could answer the needs.
基于 MFC Dialog 的应用程序可以满足需求。