简体   繁体   English

如何使用 GUI 在 C++ 中创建框架/窗口?

[英]How to create a frame/window in C++ with GUI?

I want to basiically create a window with C++. It should have a control panel like structure, with the option to toggle a button.我想基本上用 C++ 创建一个 window。它应该有一个类似控制面板的结构,可以选择切换按钮。 The button's image should be from a bitmap, one for ON and other for OFF.按钮的图像应来自 bitmap,一个用于打开,另一个用于关闭。

在此处输入图像描述

It should have an array of such individual control buttons, like a control panel.它应该有一系列这样的独立控制按钮,就像一个控制面板。 I should be able to toggle ON/OFF.我应该能够打开/关闭。 But I've to build this tiny application using C++. Kindly guide me on to start and proceed?但我必须使用 C++ 构建这个小应用程序。请指导我开始并继续?

You should have a look on these:你应该看看这些:

  • Qt Qt
  • GTK+ GTK+
  • wxWidgets wxWidgets

and then choose depending on your need and preferencies.然后根据您的需要和偏好进行选择。

EDIT:编辑:

if you are on Windows, you can use WinAPI.如果您使用的是 Windows,则可以使用 WinAPI。 But you loose portability of your code among another OSs.但是您失去了代码在其他操作系统之间的可移植性。

Why not make it simple?为什么不让它变得简单呢? Produce your code in Java, to the point of having a jar file, and then create an executable for each of the three major O/S's from that jar using Launch4J.在 Java 中生成您的代码,直到拥有 jar 文件,然后使用 Launch 为该 jar 中的三个主要操作系统中的每一个创建一个可执行文件。

Java has frames (JFrames), panels (JPanels) and controls like buttons (JButtons), that you need to build your project, they offer lots of methods to customize each object, and they show doc and prototypes for each one, at least in NetBeans. Java 具有框架 (JFrames)、面板 (JPanels) 和按钮 (JButtons) 等控件,您需要构建项目,它们提供了许多方法来自定义每个 object,并且它们显示每个 ZA8CFDE6331B666Z 的文档和原型,至少在NetBeans。

Java is also object-oriented, but if you have to stick to C++ for a class then I wish you well. Java 也是面向对象的,但如果您必须坚持使用 C++ 来获得 class,那么祝您一切顺利。 This ease of use, in that it is so much like C++, is what led me to abandon that language for Java two decades ago and today is the first day I've come back to check on the status of C++ GUI's.这种易用性,因为它与 C++ 非常相似,是导致我在二十年前放弃 Java 的语言的原因,今天是我回来检查 ZF6F87C9FDCF8B3C3C207F93 状态的第一天。 It hasn't changed and I see no reason to go back.它没有改变,我认为没有理由返回 go。

Unfortunately GUI in C++ is not that simple.不幸的是,C++ 中的 GUI 并不是那么简单。

You're going to have to interact with the OS at some point to achieve this, which means you will need some sort of library to use.您将不得不在某些时候与操作系统交互来实现这一点,这意味着您将需要某种库来使用。

To better answer your question (as has been mentioned in the comments) you will need to identify what kind of OS you are targeting?为了更好地回答您的问题(如评论中所述),您需要确定您的目标是哪种操作系统? The usual three are:常见的三种是:

  • Windows Windows
  • Mac苹果电脑
  • Linux Linux

With what information you've given though: I recommend using the SFML Library or something similar.尽管您提供了哪些信息:我建议使用SFML 库或类似的东西。 There are tutorials available and it works quite well cross-platform.有可用的教程,它在跨平台上运行良好。 To open a window you just instantiate a class like so:要打开 window,您只需像这样实例化 class :

sf::Window window(sf::VideoMode(800, 600), "My window");

Drawing buttons is a little harder.绘制按钮有点困难。 Many libraries provide their own solutions, so I'll avoid giving a specific example, and the below is only psuedo-code.许多库都提供了自己的解决方案,因此我将避免给出具体示例,以下仅是伪代码。 In general though, you want to capture when the user clicks, then get their Mouse/Pointer Position (x, y) on screen, and check it against the boundaries of each corner of the button's rectangle:不过,一般来说,您希望捕获用户单击的时间,然后在屏幕上获取他们的鼠标/指针 Position (x, y),并检查按钮矩形每个角的边界:

if (ButtonPressed())
{
    int currentX = GetMousePositionX();
    int currentY = GetMousePositionY();

    if (currentX > button.Left() &&
        currentX < button.Right() &&
        currentY > button.Top() &&
        currentY < button.Bottom())
    {
        // The button is pressed
    }
}

The code example above does assume that the MousePosition starts at 0, 0 at the top left of the window, which different libraries do differently.上面的代码示例确实假设 MousePosition 从 window 左上角的 0、0 开始,不同的库做的不同。 So you'll want to check how the library you're using implements it.因此,您需要检查您使用的库是如何实现它的。

You might also want to consider foregoing the GUI entirely (if this is really just a small project, GUI in C++ can be surprisingly difficult).您可能还想考虑完全放弃 GUI(如果这真的只是一个小项目,C++ 中的 GUI 可能会非常困难)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM