Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

试用C语言GUI库:libui #26

Open
manxisuo opened this issue Nov 22, 2018 · 0 comments
Open

试用C语言GUI库:libui #26

manxisuo opened this issue Nov 22, 2018 · 0 comments

Comments

@manxisuo
Copy link
Owner

manxisuo commented Nov 22, 2018

libui是一个简单易用的C语言GUI库。

1. 编译libui

(1) 从官网下载libui的源码

下载地址:https://github.com/andlabs/libui

(2) 根据README的说明进行构建

$ # you must be in the top-level libui directory, otherwise this won't work
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make tester         # for the test program
$ make examples       # for examples

执行cmake时报错,相关信息如下:

-- No package 'gtk+-3.0' found
CMake Error at /usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:367 (message):
A required package was not found
Call Stack (most recent call first):
/usr/share/cmake-3.5/Modules/FindPkgConfig.cmake:532 (_pkg_check_modules_internal)
unix/CMakeLists.txt:4 (pkg_check_modules)

解决方法为安装libgtk,即:

sudo apt-get install libgtk-3-dev

编译完成后,生成的共享库位于build/out目录。

libui.so
libui.so.0

2. 编写测试代码

代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ui.h>

int onClosing(uiWindow *w, void *data)
{
    uiQuit();
    return 1;
}

void onClicked(uiButton *b, void *data)
{
    char msg[32] = "Button is clicked!";
    uiLabelSetText(uiLabel(data), msg);
    puts(msg);
}

int main(void)
{
    uiInitOptions o;
    const char *err;
    uiWindow *w;
    uiGrid *g;
    uiLabel *l;
    uiButton *b;

    memset(&o, 0, sizeof(uiInitOptions));
    err = uiInit(&o);
    if (err != NULL) {
        puts(err);
        uiFreeInitError(err);
        return 1;
    }

    // 创建Window
    w = uiNewWindow("libui测试", 320, 240, 0);
    uiWindowSetMargined(w, 1);

    // 创建Grid
    g = uiNewGrid();
    uiGridSetPadded(g, 1);
    uiWindowSetChild(w, uiControl(g));

    // 创建Label
    l = uiNewLabel("I am a label.");
    uiGridAppend(g, uiControl(l),
        0, 2, 2, 1,
        1, uiAlignCenter, 1, uiAlignFill);

    // 创建Button,注册按钮点击时的回调函数
    b = uiNewButton("Click me!");
    uiButtonOnClicked(b, onClicked, l);
    uiGridAppend(g, uiControl(b),
        0, 2, 2, 1,
        1, uiAlignCenter, 1, uiAlignEnd);

    // 注册窗口关闭时的回调函数
    uiWindowOnClosing(w, onClosing, NULL);

    // 显示窗口
    uiControlShow(uiControl(w));

    // 启动主循环
    uiMain();

    return 0;
}

编译、运行:

# 注意将libui.so加入共享库搜索路径
gcc main.c -l ui -L . -I ~/dev/C/libui-master -o main
./main

效果如下:

libui测试

@manxisuo manxisuo changed the title 试用libui库 试用C语言GUI库libui Nov 22, 2018
@manxisuo manxisuo changed the title 试用C语言GUI库libui 试用C语言GUI库:libui Nov 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant