C语言 使用 C 设计 GUI 应用程序的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2658673/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:12:15  来源:igfitidea点击:

What is the best way to design GUI Applications with C?

cuser-interface

提问by saint

I always find good tutorials on the subject, but it almost always is C++. Is it hard or just doesn't make sense to make GUI applications with C?

我总能找到关于这个主题的好教程,但它几乎总是 C++。用 C 语言制作 GUI 应用程序很难还是没有意义?

A good book, guide or a tutorial is fine.

一本好书、指南或教程都可以。

Platform: Linux

平台:Linux

采纳答案by Michael Ekstrand

Use GTK+. It's written in C and exposes a well-designed C API. Their tutorialis quite excellent.

使用GTK+。它是用 C 编写的,并公开了一个精心设计的 C API。他们的教程非常好。

GTK+ is the toolkit for GNOME, so your applications will fit right in with many Linux desktops.

GTK+ 是 GNOME 的工具包,因此您的应用程序将适合许多 Linux 桌面。

回答by Romain Hippeau

If you are programming in C on Windows - Petzold's programming windows used to be the bible for C based ui work.

如果您在 Windows 上使用 C 进行编程 - Petzold 的编程窗口曾经是基于 C 的 ui 工作的圣经。

EDIT:Since I answered my question the post has been updated to be on Linux. I will leave the post here for anybody looking, but it really does not apply to Linux.

编辑:自从我回答了我的问题后,该帖子已更新为适用于 Linux。我会把这篇文章留在这里给任何人看,但它确实不适用于 Linux。

回答by Pavel Radzivilovsky

Many high quality GUI were written in C with, for example, Windows API. There's no particular reason why not, but object oriented programming was very successful in modeling interactive graphics. GUI elements somehow map naturally into C++ objects that can encapsulate complex behavior behind a simple interface.

许多高质量的 GUI 是用 C 编写的,例如,Windows API。没有什么特别的理由,但是面向对象编程在交互式图形建模方面非常成功。GUI 元素以某种方式自然地映射到 C++ 对象中,这些对象可以将复杂的行为封装在一个简单的界面后面。

回答by none

What is the best way to design GUI Applications with C?

使用 C 设计 GUI 应用程序的最佳方法是什么?

don't, if you don't have to :-)

不要,如果你不需要:-)

Is it hard or just doesn't make sense to make GUI applications with C?

用 C 语言制作 GUI 应用程序很难还是没有意义?

It is not necessarily that hard, but a pretty redundant and verbose task (like writing high level programs in assembly language), with lots of repeated boilerplate code.

它不一定那么难,但它是一项非常冗余和冗长的任务(比如用汇编语言编写高级程序),有很多重复的样板代码。

Personally, I find it more rewarding to simply embed a scripting interpreter (e.g. Lua, Nasal) with bindings for a GUI library and then code the UI in a high level scripting language and code only the core of the application itself in C. Python was previously mentioned, but I think that a dedicated extension language like Lua would be a better fit, because it doesn't bloat your source code and because it does not create any additional requirements (like library dependencies or architectural).

就我个人而言,我发现简单地嵌入一个带有 GUI 库绑定的脚本解释器(例如 Lua、Nasal),然后用高级脚本语言编写 UI 代码并仅用 C 编写应用程序本身的核心代码会更有意义。Python 是前面提到过,但我认为像 Lua 这样的专用扩展语言会更合适,因为它不会使您的源代码膨胀,并且因为它不会创建任何额外的要求(如库依赖项或架构)。

In other words, embedding something like Python, Perl or Ruby may be relatively straight forward (because of good documentation and community momentum), but often these languages are more complex than the host application itself.

换句话说,嵌入 Python、Perl 或 Ruby 之类的东西可能相对简单(因为良好的文档和社区动力),但这些语言通常比宿主应用程序本身更复杂。

This is also the approach taken by the AlgoScoresoftware, which uses an embedded Nasal interpreter with GTK bindings.

这也是AlgoScore软件采用的方法,它使用带有 GTK 绑定的嵌入式 Nasal 解释器。

回答by fabrizioM

Don't.

别。

Use python and then bind the computationally expensive calls written in C.

使用 python,然后绑定用 C 编写的计算成本高的调用。

回答by Jerry Coffin

It's hard enough (or, mostly, verbose enough) that most people figure it just doesn't make sense. The GUI part of an application (mostly) reacts to user input, so in this area speed is rarely critical -- a difference of a few microseconds (or even milliseconds) rarely makes much difference. As long as responses appear within 100 ms (or so), they're pretty much perceived as "instant".

这已经够难了(或者,大多数情况下,够冗长)以至于大多数人认为它没有意义。应用程序的 GUI 部分(主要)对用户输入做出反应,因此在这个领域速度很少是关键——几微秒(甚至几毫秒)的差异很少会产生太大差异。只要响应出现在 100 毫秒(左右)内,它们就会被视为“即时”。

Dynamic typing also tends to work quite nicely for a lot of GUI programming. C uses only static typing.

对于许多 GUI 编程,动态类型也往往能很好地工作。C 只使用静态类型。

回答by Stephen

If you're on a *nix system, you can use Xlib. But you're probably better off programming in C++ and calling out to your C code.

如果您使用的是 *nix 系统,则可以使用Xlib。但是您最好使用 C++ 编程并调用您的 C 代码。