如何使用 C++ 为 Windows 应用程序创建 GUI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/488837/
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
How do I create a GUI for a windows application using C++?
提问by Zombies
I am deciding on how to develop a GUI for a small c++/win32 api project (working Visual Studio C++ 2008). The project will only need a few components to start off the main process so it will be very light weight (just 1 button and a text box pretty much...). My question is this:
我正在决定如何为小型 c++/win32 api 项目(工作 Visual Studio C++ 2008)开发 GUI。该项目只需要几个组件来启动主流程,因此重量非常轻(几乎只有 1 个按钮和一个文本框......)。我的问题是这样的:
I don't have experience developing GUIs on windows but I can learn easily. So, what should I use? A Visual editor (drag and drop code generationg: my preference for desktop GUI designing by far (java/swing)). Or should I use a speicific library? Either way, WHICH library or visual editor should I use? I heard someone mention writing the GUI in C#, then calling the C++ code... the thing is, that this is such a simple GUI I would find it easier to just keep it all in C++, but I'm open to whatever the best suggestion is.
我没有在 Windows 上开发 GUI 的经验,但我可以轻松学习。那么,我应该使用什么?一个可视化编辑器(拖放代码生成g:我目前对桌面 GUI 设计的偏好(java/swing))。或者我应该使用特定的库?无论哪种方式,我应该使用哪个库或可视化编辑器?我听说有人提到用 C# 编写 GUI,然后调用 C++ 代码......问题是,这是一个如此简单的 GUI,我发现将其全部保存在 C++ 中会更容易,但我对任何事情都持开放态度最好的建议是。
采纳答案by 17 of 26
If you're doing a very simple GUI and you're already using Visual Studio then it may make sense to just go with MFC. You can just use the Visual Studio MFC wizard to create a dialog based application, drop two controls on it and away you go.
如果您正在做一个非常简单的 GUI 并且您已经在使用 Visual Studio,那么使用 MFC 可能是有意义的。您只需使用 Visual Studio MFC 向导来创建一个基于对话框的应用程序,将两个控件放到它上面就可以了。
MFC is dated and has its fair share of annoyances, but it will certainly do the job for you if you're just talking about a button and a text box.
MFC 已经过时并且有很多烦恼,但如果您只是谈论一个按钮和一个文本框,它肯定会为您完成这项工作。
I don't have any experience with Qt, so I can't compare the two.
我对 Qt 没有任何经验,所以我无法比较两者。
回答by Javier
by far the best C++ GUI library out there is Qt, it's comprehensive, easy to learn, really fast, and multiplattform.
迄今为止最好的 C++ GUI 库是Qt,它是全面的、易于学习的、非常快速的和多平台的。
ah, it recently got a LGPL license, so now you can download it for free and include on commercial programs
啊,它最近获得了 LGPL 许可证,所以现在您可以免费下载并包含在商业程序中
回答by David Citron
I strongly prefer simply using Microsoft Visual Studio and writing a native Win32 app.
我非常喜欢简单地使用 Microsoft Visual Studio 并编写本机 Win32 应用程序。
For a GUI as simple as the one that you describe, you could simply create a Dialog Boxand use it as your main application window. The default application created by the Win32 Project wizard in Visual Studio actually pops a window, so you can replacethat window with your Dialog Box and replace the WndProc with a similar (but simpler) DialogProc.
对于像您描述的那样简单的 GUI,您可以简单地创建一个对话框并将其用作主应用程序窗口。由 Visual Studio 中的 Win32 项目向导创建的默认应用程序实际上会弹出一个窗口,因此您可以用Dialog Box替换该窗口,并将 WndProc 替换为类似(但更简单)的DialogProc。
The question, then, is one of tools and cost. The Express Editionof Visual C++ does everything you want except actually create the Dialog Template resource. For this, you could either code it in the RC file by handor in memory by hand. Related SO question: Windows API Dialogs without using resource files.
那么,问题是工具和成本之一。该Express版本的Visual C ++的,你想要做除了实际创建对话框模板资源的一切。为此,您可以手动在RC 文件中或在内存中对其进行编码。相关 SO 问题:Windows API Dialogs without using resource files。
Or you could try one of the free resource editorsthat others have recommended.
或者您可以尝试其他人推荐的免费资源编辑器之一。
Finally, the Visual Studio 2008 Standard Editionis a costlier option but gives you an integrated resource editor.
最后,Visual Studio 2008 标准版是一个更昂贵的选择,但为您提供了一个集成的资源编辑器。
回答by Jasper Bekkers
I strongly advise against using plain Win32 because it's pretty hard to make it work OK in all situations, it's pretty dull and tedious work and the Common Controls library isn't thatcomplete. Also, most of the work has been done for you.
我强烈建议不要使用普通的 Win32,因为它很难在所有情况下都正常工作,这是一项非常枯燥乏味的工作,而且 Common Controls 库也不是那么完整。此外,大部分工作都已为您完成。
Every time I end up doing plain Win32 I have to spent at least a couple of hours on the most trivial tasks because I have to look up all the parameters, flags, functions, macros and figure out how to hook them up properly. I'd generally prefer a simple drag-and-drop don't-make-me-use-my-brains type of solution and just slam the thing together in 2 minutes.
每次我最终完成普通的 Win32 时,我都必须在最琐碎的任务上花费至少几个小时,因为我必须查找所有参数、标志、函数、宏,并弄清楚如何正确连接它们。我通常更喜欢简单的拖放式解决方案,不要让我使用我的大脑,只需在 2 分钟内将事情撞在一起。
As a lightweight toolkit I'd suggest omguiwhich has a clean and pretty API. It doesn't, however, come with any tools.
作为一个轻量级的工具包,我建议使用omgui,它有一个干净漂亮的 API。但是,它没有附带任何工具。
If you need tool support, you'll probably end up wanting to go for either MFC (resource editor built into Visual Studio) or Qt. I don't know if wxWidgets has any tools, but I presume it has.
如果您需要工具支持,您可能最终想要使用 MFC(内置于 Visual Studio 的资源编辑器)或 Qt。我不知道 wxWidgets 是否有任何工具,但我认为它有。
Edit:David Citron mentions that apparently the resource editor in Visual Studio generates Win32 compatible resource files, so that's probably the preferred way to do things if you wanted to keep things simple.
编辑:David Citron 提到 Visual Studio 中的资源编辑器显然会生成与 Win32 兼容的资源文件,因此如果您想让事情变得简单,这可能是首选的处理方式。
回答by LarryF
Just create a new MFC C++ app. It's built in, pretty easy, and thousands of examples exist in the world...
只需创建一个新的 MFC C++ 应用程序。它是内置的,非常简单,世界上有成千上万的例子......
Plus, you can design your dialog box right in Visual Studio, give them variable names, and 90% of the code is generated for you.
此外,您可以直接在 Visual Studio 中设计对话框,为其指定变量名称,然后 90% 的代码都会为您生成。
回答by devdimi
For such a simple application even MFC would be overkill. If don't want to introduce another dependency just do it in plain vanilla Win32. It will be easier for you if you have never used MFC.
对于这样一个简单的应用程序,即使是 MFC 也会大材小用。如果不想引入另一个依赖项,只需在普通的 Win32 中进行即可。如果您从未使用过 MFC,这对您来说会更容易。
Check out the classic "Programming Windows" by Charles Petzold or some online tutorial (e.g. http://www.winprog.org/tutorial/) and you are ready to go.
查看 Charles Petzold 的经典“Programming Windows”或一些在线教程(例如http://www.winprog.org/tutorial/),您就可以开始了。
回答by Chris
Qt from Nokia is definitely the way to go. Another option is gtk, but Qt is better supported and documented. Either way, they are both free. And both of them are widely used and well known so it is easy to find answers to your questions.
诺基亚的 Qt 绝对是要走的路。另一种选择是 gtk,但 Qt 得到更好的支持和记录。无论哪种方式,它们都是免费的。它们都被广泛使用且广为人知,因此很容易找到问题的答案。
回答by Chris
Avoid QT (for noobs) or any useless libraries (absurd for a such basic thing)
避免 QT(对于新手)或任何无用的库(对于这样的基本事物来说是荒谬的)
Just use the VS Win32 api Wizard, ad the button and text box...and that's all !
只需使用 VS Win32 api 向导,添加按钮和文本框......就这样!
In 25 seconds !
在 25 秒内!
回答by Ismael
If you want to learn about win32, WTL http://wtl.sourceforge.net/is the pretty lightweight equivalent to MFC, but you have to love template to use it.
如果你想了解 win32,WTL http://wtl.sourceforge.net/是相当轻量级的 MFC 等价物,但你必须喜欢模板才能使用它。
If you want something simple MFC is already integrated with VS, also it has a large base of extra code and workarounds of know bugs in the net already.
如果你想要一些简单的 MFC 已经与 VS 集成,它也有大量的额外代码和网络中已知错误的解决方法。
Also Qt is really great framework it have a nice set of tools, dialog editor, themes, and a lot of other stuff, plus your application will be ready to be cross platform, although it will require some time to get accustomed.
此外,Qt 是一个非常棒的框架,它有一套很好的工具、对话框编辑器、主题和许多其他东西,而且您的应用程序将准备好跨平台,尽管它需要一些时间来适应。
You also have Gtk, wxWindow, and you will have no problems if you have already used it on linux.
你也有Gtk,wxWindow,如果你已经在linux上用过就没有问题了。
回答by MSalters
A simple "window" with some text and a button is just a MessageBox. You can create them with a single function call; you don't need any library whatsoever.
带有一些文本和按钮的简单“窗口”只是一个 MessageBox。您可以使用单个函数调用来创建它们;你不需要任何图书馆。