如何在 C++ 中构建图形用户界面? [英] How do I build a graphical user interface in C++?

查看:206
本文介绍了如何在 C++ 中构建图形用户界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我所有的 C++ 程序都使用命令行界面,而我使用过的唯一其他语言是 PHP,它不支持 GUI.

我从哪里开始使用 C++ 进行图形用户界面编程?我如何创建一个?

解决方案

本质上,操作系统的窗口系统公开了一些 API 调用,您可以执行这些调用来完成创建窗口或在窗口上放置按钮等工作.基本上,你会得到一套头文件,你可以在这些导入的库中调用函数,就像使用 stdlib 和 printf 一样.

每个操作系统都有自己的 GUI 工具包、头文件套件和 API 调用,以及它们自己的做事方式.还有跨平台工具包,如 GTKQtwxWidgets 帮助您构建可在任何地方运行的程序.他们通过在每个平台上使用相同的 API 调用来实现这一点,但对调用本机操作系统 API 调用的那些 API 函数采用不同的实现.

他们都有一个共同点,这与 CLI 程序不同,就是所谓的事件循环.那里的基本思想有些复杂,难以压缩,但本质上这意味着在您的主类/主函数中并没有太多内容,除了:

  • 检查事件队列是否有任何新事件
  • 如果有,将这些事件分派给适当的处理程序
  • 完成后,将控制权交还给操作系统(通常使用某种特殊的sleep"或select"或yield"函数调用)
  • 然后,当操作系统完成时,yield 函数将返回,并且您还有另一个循环.

有大量关于基于事件的编程的资源.如果您对 JavaScript 有任何经验,那么它的基本思想是相同的,只是您,脚本编写者,无法访问或控制事件循环本身,或者有哪些事件,您唯一的工作就是编写和注册处理程序.

您应该记住,一般来说,GUI 编程极其复杂和困难.如果您可以选择,将嵌入式网络服务器集成到您的程序中并具有基于 HTML/Web 的界面实际上要容易得多.我遇到的一个例外是 Apple 的 Cocoa + Xcode + 界面构建器 + 教程,使其成为我见过的 GUI 编程新手最容易上手的环境.

All of my C++ programs so far have been using the command line interface and the only other language I have experience with is PHP which doesn't support GUIs.

Where do I start with graphical user interface programming in C++? How do I create one?

解决方案

Essentially, an operating system's windowing system exposes some API calls that you can perform to do jobs like create a window, or put a button on the window. Basically, you get a suite of header files and you can call functions in those imported libraries, just like you'd do with stdlib and printf.

Each operating system comes with its own GUI toolkit, suite of header files, and API calls, and their own way of doing things. There are also cross platform toolkits like GTK, Qt, and wxWidgets that help you build programs that work anywhere. They achieve this by having the same API calls on each platform, but a different implementation for those API functions that call down to the native OS API calls.

One thing they'll all have in common, which will be different from a CLI program, is something called an event loop. The basic idea there is somewhat complicated, and difficult to compress, but in essence it means that not a hell of a lot is going in in your main class/main function, except:

  • check the event queue if there's any new events
  • if there is, dispatch those events to appropriate handlers
  • when you're done, yield control back to the operating system (usually with some kind of special "sleep" or "select" or "yield" function call)
  • then the yield function will return when the operating system is done, and you have another go around the loop.

There are plenty of resources about event-based programming. If you have any experience with JavaScript, it's the same basic idea, except that you, the scripter, have no access or control over the event loop itself, or what events there are, your only job is to write and register handlers.

You should keep in mind that GUI programming is incredibly complicated and difficult, in general. If you have the option, it's actually much easier to just integrate an embedded webserver into your program and have an HTML/web based interface. The one exception that I've encountered is Apple's Cocoa + Xcode + interface builder + tutorials that make it easily the most approachable environment for people new to GUI programming that I've seen.

这篇关于如何在 C++ 中构建图形用户界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆