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

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

问题描述

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

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.

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

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

推荐答案

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

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.

每个操作系统都带有自己的GUI工具包,头文件套件和API调用,以及它们自己的处理方式。也有跨平台工具包,例如 GTK Qt wxWidgets 帮助您构建可在任何地方运行的程序。他们通过在每个平台上使用相同的API调用来实现这一点,但是对于那些调用本机OS API调用的API函数却采用不同的实现。

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.

它们之间的共同点是事件循环,这与CLI程序有所不同。那里的基本思想有些复杂,而且很难压缩,但是从本质上讲,这意味着您的主类/ main函数中不会发生很多麻烦,除了:

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.

基于事件的编程有很多资源。如果您具有JavaScript的使用经验,那么它是相同的基本思想,除了脚本编写者无法访问或控制事件循环本身或存在哪些事件之外,您唯一的工作就是编写和注册处理程序。

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.

通常应注意,GUI编程非常复杂且困难。如果您可以选择,将嵌入式Web服务器集成到程序中并具有基于HTML / Web的界面实际上要容易得多。我遇到的一个例外是Apple的可可 + Xcode +界面构建器+教程,使它成为了我所见过的GUI编程新手最容易接近的环境。

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天全站免登陆