C语言 如何在 C 中进行 GUI 编程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5450047/
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 can I do GUI programming in C?
提问by Algorithmist
I want to do Graphics programming in C. I had searched a lot about the compiler that provides a rich set of functions for doing GUI programming in C, but I couldn't find anything.
我想用 C 进行图形编程。我搜索了很多关于提供丰富的 C 语言编程功能的编译器,但我找不到任何东西。
Basically I want to draw buttons and then accept the choice from the user and take an appropriate action. It would be helpful if you can suggest a C compiler, or a library that I can add to my compiler. I am working on the Windows operating system.
基本上我想绘制按钮,然后接受用户的选择并采取适当的行动。如果您能建议一个 C 编译器或一个我可以添加到我的编译器中的库,那将会很有帮助。我在 Windows 操作系统上工作。
Presently, I am using TURBO C compiler that does not support direct methods for creating buttons. Any help would be appreciated.
目前,我使用的 TURBO C 编译器不支持创建按钮的直接方法。任何帮助,将不胜感激。
回答by Cody Gray
This is guaranteed to have nothingto do with the compiler. All compilers do is compile the code that they are given. What you're looking for is a GUI library, which you can write code against using any compiler that you want.
这保证与编译器无关。所有编译器所做的就是编译它们给出的代码。您正在寻找的是一个 GUI 库,您可以使用任何您想要的编译器编写代码。
Of course, that being said, your first order of business should be to ditch Turbo C. That compiler is about 20 years old and continuing to use it isn't doing you any favors. You can't write modernGUI applications, as it will only produce 16-bit code. All modern operating systems are 32-bit, and many are now 64-bit. It's also worth noting that 64-bit editions of Windows will not run 16-bit applications natively. You'll need an emulator for that; it's not really going to engender much feeling of accomplishment if you can only write apps that work in a DOS emulator. :-)
当然,话虽如此,您的首要任务应该是放弃 Turbo C。该编译器已有大约 20 年的历史,继续使用它对您没有任何好处。您无法编写现代GUI 应用程序,因为它只能生成 16 位代码。所有现代操作系统都是 32 位的,现在很多都是 64 位的。还值得注意的是,64 位版本的 Windows 不会本机运行 16 位应用程序。为此,您需要一个模拟器;如果您只能编写可在 DOS 模拟器中运行的应用程序,那并不会真正产生多大的成就感。:-)
Microsoft's Visual Studio Express C++ is available as a free download. It includes the same compiler available in the full version of the suite. The C++ package also compiles pure C code.
Microsoft 的 Visual Studio Express C++可免费下载。它包括套件的完整版本中可用的相同编译器。C++ 包还编译纯 C 代码。
And since you're working in Windows, the Windows API is a natural choice. It allows you to write native Windows applications that have access to the full set of GUI controls. You'll find a nice tutorial hereon writing WinAPI applications in C. If you choose to go with Visual Studio, it also includes boilerplate code for a blank WinAPI application that will get you up and running quickly.
由于您在 Windows 中工作,因此 Windows API 是一个自然的选择。它允许您编写可以访问全套 GUI 控件的本机 Windows 应用程序。您会在此处找到有关用 C 编写 WinAPI 应用程序的不错教程。如果您选择使用 Visual Studio,它还包括空白 WinAPI 应用程序的样板代码,可让您快速启动和运行。
If you really care about learning to do this, Charles Petzold's Programming Windowsis the canonical resource of the subject, and definitely worth a read. The entire Windows API was written in C, and it's entirely possible to write full-featured Windows applications in C. You don't need no stinkin' C++.
如果您真的很在意学习如何做到这一点,Charles Petzold 的Windows 编程是该主题的规范资源,绝对值得一读。整个 Windows API 都是用 C 编写的,完全有可能用 C 编写功能齐全的 Windows 应用程序。您不需要讨厌的 C++。
That's the way I'ddo it, at least. As the other answers suggest, GTKis also an option. But the applications it generates are just downright horrible-looking on Windows.
就是这样我会做到这一点,至少。正如其他答案所暗示的那样,GTK也是一种选择。但是它生成的应用程序在 Windows 上看起来非常可怕。
EDIT:Oh dear... It looks like you're not alone in wanting to write "GUI" applications using an antiquated compiler. A Google search turns up the following library: TurboGUI: A GUI Framework for Turbo C/C++:
编辑:哦,亲爱的......看起来你并不是唯一一个想要使用过时的编译器编写“GUI”应用程序的人。谷歌搜索出现了以下库:TurboGUI:Turbo C/C++ 的 GUI 框架:


If you're another one of those poor people stuck in the hopelessly out-of-date Indian school system and forced to use Turbo C to complete your education, this might be an option. I'm loathe to recommend it, as learning to work around its limitations will be completely uselessto you once you graduate, but apparently it's out there for you if you're interested.
如果您是困在过时的印度学校系统中并被迫使用 Turbo C 完成学业的穷人中的另一个,那么这可能是一种选择。我不愿意推荐它,因为一旦你毕业,学习如何解决它的局限性对你来说完全没有用,但如果你有兴趣,它显然适合你。
回答by Sandro Munda
The most famous library to create some GUI in C language is certainly GTK.
最著名的用 C 语言创建一些 GUI 的库当然是GTK。
With this library you can easily create some buttons (for your example). When a user clicks on the button, a signal is emitted and you can write a handler to do some actions.
使用此库,您可以轻松创建一些按钮(例如)。当用户单击按钮时,会发出一个信号,您可以编写一个处理程序来执行某些操作。
回答by BlackBear
Use win APIs in your main function:
在主函数中使用 win API:
- RegisterClassEx()note: you have to provide a pointer to a function (usually called WndProc) which handles windows messages such as WM_CREATE, WM_COMMAND etc
- CreateWindowEx()
- ShowWindow()
- UpdateWindow()
- RegisterClassEx()注意:您必须提供一个指向处理 Windows 消息(例如 WM_CREATE、WM_COMMAND 等)的函数(通常称为WndProc)的指针
- 创建窗口Ex()
- 显示窗口()
- 更新窗口()
Then write another function which handles win's messages (mentioned in #1). When you receive the message WM_CREATE you have to call CreateWindow(). The class is what control is that window, for example "edit" is a text box and "button" is a.. button :). You have to specify an ID for each control (of your choice but unique among all). CreateWindow() returns a handle to that control, which needs to be memorized. When the user clicks on a control you receive the WM_COMMAND message with the ID of that control. Here you can handle that event. You might find useful SetWindowText()and GetWindowText()which allows you to set/get the text of any control.
You will need only the win32 SDK. You can get it here.
然后编写另一个处理 win 消息的函数(在 #1 中提到)。当您收到消息 WM_CREATE 时,您必须调用 CreateWindow()。该类是该窗口的控件,例如“编辑”是一个文本框,而“按钮”是一个..按钮:)。您必须为每个控件指定一个 ID(由您选择,但在所有控件中是唯一的)。CreateWindow() 返回该控件的句柄,需要记住该句柄。当用户单击某个控件时,您会收到带有该控件 ID 的 WM_COMMAND 消息。在这里您可以处理该事件。您可能会发现有用的SetWindowText()和GetWindowText()允许您设置/获取任何控件的文本。
您将只需要 win32 SDK。你可以在这里得到它。
回答by sergio
C is more of a hardware programming language, there are easy GUI builders for C, GTK, Glade, etc. The problem is making a program in C that is the easy part, making a GUI that is a easy part, the hard part is to combine both, to interface between your program and the GUI is a headache, and different GUI use different ways, some threw global variables, some use slots. It would be nice to have a GUI builder that would bind easily your C program variables, and outputs. CLI programming is easy when you overcome memory allocation and pointers, GUI you can use a IDE that uses drag and drop. But all around I think it could be simpler.
C 更像是一种硬件编程语言,C、GTK、Glade 等都有简单的 GUI 构建器。将两者结合起来,在你的程序和 GUI 之间进行接口是一件令人头疼的事情,不同的 GUI 使用不同的方式,有些抛出全局变量,有些使用插槽。如果有一个 GUI 构建器可以轻松绑定您的 C 程序变量和输出,那就太好了。当您克服内存分配和指针时,CLI 编程很容易,GUI 您可以使用使用拖放的 IDE。但我认为这可能更简单。
回答by Viju
Windows APIand Windows SDKif you want to build everything yourself (or) Windows APIand Visual C Express. Get the 2008 edition. This is a full blown IDE and a remarkable piece of software by Microsoft for Windows development.
Windows API和Windows SDK,如果你想自己构建所有东西(或)Windows API和Visual C Express。获取 2008 版。这是一个成熟的 IDE,也是 Microsoft 用于 Windows 开发的出色软件。
All operating systems are written in C. So, any application, console/GUI you write in C is the standard way of writing for the operating system.
所有操作系统都是用 C 编写的。因此,您用 C 编写的任何应用程序、控制台/GUI 都是操作系统的标准编写方式。
回答by David Brown
A C compiler itself won't provide you with GUI functionality, but there are plenty of libraries for that sort of thing. The most popular is probably GTK+, but it may be a little too complicated if you are just starting out and want to quickly get a GUI up and running.
AC 编译器本身不会为您提供 GUI 功能,但是有很多用于此类事情的库。最受欢迎的可能是GTK+,但如果您刚刚开始并希望快速启动并运行 GUI,它可能有点太复杂了。
For something a little simpler, I would recommend IUP. With it, you can use a simple GUI definition language called LED to layout controls (but you can do it with pure C, if you want to).
对于更简单的东西,我会推荐IUP。有了它,您可以使用一种称为 LED 的简单 GUI 定义语言来布局控件(但如果您愿意,也可以使用纯 C 来完成)。

