littlebot
Published on 2025-04-08 / 3 Visits
0

【源码】基于C++的EasyGraphics图形库项目

项目简介

EasyGraphics是一个轻量的跨平台C++图形化接口,用于构建图形界面应用。该项目以易使用、源码易读、易扩展为设计目标,支持嵌入式Linux系统和Windows系统,旨在为用户提供一种简单直观的方式来创建图形界面应用程序。

项目的主要特性和功能

  1. 跨平台支持:可在嵌入式Linux和Windows等多种操作系统上运行。
  2. 易于使用:提供直观的API,方便用户创建图形界面应用。
  3. 丰富的控件:包含按钮、文本框、标签等各类控件,便于构建复杂图形界面。
  4. 动画系统:支持创建流畅的动画效果,增强用户体验。
  5. 事件系统:具备完善的事件系统,可处理点击、拖拽等用户交互事件。
  6. 渲染系统:提供高效的渲染系统,方便控制图形渲染过程。

安装使用步骤

假设用户已经下载了本项目的源码文件。 1. 安装C++编译器,如GCC或Clang。 2. 使用CMake构建项目。在终端中进入项目目录,执行以下命令: shell mkdir build && cd build cmake .. && make 3. 运行生成的可执行文件。

使用示例

以下是一个简单的计算器示例,展示了如何使用EasyGraphics库创建图形界面应用程序: ```c++

include "include/OverlapPanel.hh"

include "include/Grid.hh"

include "include/Label.hh"

include "system/SystemIO.hh"

include

include

using namespace easy;

int as_int(std::string s) { const char str = s.c_str() + 2; int x = 0; bool neg = false; if (str[0] == '-') str++, neg = true; while (str) { x = 10 * x + *str - '0'; str++; } return neg ? -x : x; }

int main() { Register();

int result = 0;
char op = '\0';
bool wait_new = true;

Label input = MakeLabel();
input->Margin = { 20 };
input->SpecSize = { 700, 70 };
input->BackgroundColor = Color::FromARGB(0xF0F0F0);
input->FontSize = FontSizeType::Large;
input->FontColor = Colors::Black;
input->VerticalAlignment = VerticalAlignType::Center;
input->HorizontalAlignment = HorizontalAlignType::Left;
input->FontHorizontalAlignment = HorizontalAlignType::Left;
input->FontVerticalAlignment = VerticalAlignType::Center;

Grid btns = MakeGrid({ 80,80,80,80 }, { 80,80,80,80 });
btns->Margin = { 20 };

auto MakeBtn = []() {
    Label btn = MakeLabel();
    btn->SpecSize = { 76, 76 };
    btn->VerticalAlignment = VerticalAlignType::Center;
    btn->HorizontalAlignment = HorizontalAlignType::Center;
    btn->BackgroundColor = Color::FromARGB(0xF0F0F0);
    return btn;
};

for (int i = 0; i < 10; ++i) {
    Label btn = MakeBtn();
    btn->Text = std::to_string(i);
    btn->Click += [&, i](Element, MouseEventArgs) {
        if (wait_new) input->Text = "  " + std::to_string(i), wait_new = false;
        else input->Text += std::to_string(i);
        Renderer::Invalidated() = true;
    };
    btn->BackgroundColor = Color::FromARGB(0xFAFAFA);
    if (i) btns->Set((i - 1) / 3, (i - 1) % 3, btn);
    else btns->Set(3, 1, btn);
}

for (int i = 0; i < 4; ++i) {
    Label btn = MakeBtn();
    btn->Text = "+-*/"[i];
    btn->Click += [&, i](Element, MouseEventArgs) {
        op = "+-*/"[i];
        wait_new = true;
        result = as_int(input->Text);
        Renderer::Invalidated() = true;
    };
    btns->Set(i, 3, btn);
}

Label eq = MakeBtn();
eq->Text = "=";
eq->Click += [&](Element, MouseEventArgs) {
    if (op == '+') input->Text = "  " + std::to_string(result + as_int(input->Text));
    else if (op == '-') input->Text = "  " + std::to_string(result - as_int(input->Text));
    else if (op == '*') input->Text = "  " + std::to_string(result * as_int(input->Text));
    else if (op == '/' && as_int(input->Text)) input->Text = "  " + std::to_string(result / as_int(input->Text));
    op = '\0';
    result = as_int(input->Text);
    wait_new = true;
    Renderer::Invalidated() = true;
};
btns->Set(3, 0, eq);

Label cl = MakeBtn();
cl->Text = "C";
cl->Click += [&](Element, MouseEventArgs) {
    input->Text = "  0";
    op = '\0';
    result = 0;
    wait_new = true;
    Renderer::Invalidated() = true;
};
btns->Set(3, 2, cl);

Grid form = MakeGrid({ 130, 0 }, { 0 });
form->BackgroundColor = Color::FromARGB(0xE6E6E6);
form->Set(0, 0, input);
form->Set(1, 0, btns);
Renderer::MainLoop(form);

} ``` 用户只需关心控件的使用,无需处理时钟、动画、渲染、事件触发等细节。EasyGraphics的控件API设计直观易用,大部分参考C# WPF风格。

文档和进一步帮助

详细文档和教程位于项目的documentation目录下,名为Quickstart.md,提供了项目的快速入门指南。

注意事项

在使用EasyGraphics库时,请确保您的系统已安装必要的依赖项,并遵循项目的使用条款和条件。

下载地址

点击下载 【提取码: 4003】【解压密码: www.makuang.net】