前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习LUA,使用IUP进行GUI程序设计

学习LUA,使用IUP进行GUI程序设计

作者头像
周星星9527
发布2021-12-16 15:12:24
1.8K0
发布2021-12-16 15:12:24
举报

原文:

代码语言:javascript
复制
http://www.cppblog.com/visualfc/archive/2008/12/29/70722.html

在LUA中进行GUI程序设计,可以选择的GUI库一般有wxLua和IupLua。wxLua具备典型的面向对象风格,功能相对强大。而IUP的LUA绑定则非常简洁易用。本文主要介绍IupLua。 IUPLUA目前稳定版本为2.7.1,最新版本为3.0beta1。 IUP项目主页为:

代码语言:javascript
复制
http://sourceforge.net/projects/iup

在IupLua程序设计中,主要使用表来设计应用程序界面,表的键值则为GUI部件的属性,通过回调函数完成窗口消息的获取。下面给出一个完整的例子。

代码语言:javascript
复制
require "iuplua"

text_location = iup.text{expand="HORIZONTAL", id="text_location"}
btn_browse = iup.button{title="Browse", rastersize="x22",id="btn_browse"}
dlg = iup.dialog
{
    iup.vbox
    {
        iup.label{title="Location:"},
        iup.hbox
        {
            text_location,
            btn_browse
            ; margin="0x0"
        },
        iup.label{title="Text:"},
        iup.multiline{expand="YES"},
    }
    ;title="iuplua sample", size="200x100", margin="10x10"
}

function btn_browse:action()
    local dlg = iup.filedlg{dialogtype="DIR"}
    dlg:popup()
    if dlg.status == "0" then
        text_location.value = dlg.value
    end
end

dlg:show()

iup.MainLoop()

上面的例子通过hbox和vbox进行界面的自动调整。程序很简单,如下图所示:

其中22行的fuctnion btn_browse:action即为回调函数,相比wxLua的Bind方法,这种用法更为简洁。 另外我们也可以看到在此程序中我们需要对text_location和btn_browse进行声明,而后在dialog的box中进行引用,我们也可以使用类似HTML控制中的ID值来进行设计而无需事先声明。如下:

代码语言:javascript
复制
require "iuplua"
require "iupid"

dlg = iup.dialog
{
    iup.vbox
    {
        iup.label{title="Location:", id="ok"},
        iup.hbox
       {
            iup.text{expand="HORIZONTAL", id="text_location"},
            iup.button{title="Browse", rastersize="x22", id="btn_browse"}
            ; margin="0x0"
        },
        iup.label{title="Text:"},
        iup.multiline{expand="YES"},
    }
    ;title="iuplua sample", size="200x100", margin="10x10"
}

function btn_browse:action()
    local dlg = iup.filedlg{dialogtype="DIR"}
    dlg:popup()
    if dlg.status == "0" then
        text_location.value = dlg.value
    end
end

dlg:show()

iup.MainLoop()

如上所示,使用id值的方式GUI设计代码显得更为一致。那么如何做到这一点呢,在LUA中实现起来很简单,使用upvalue就可以做到。

代码语言:javascript
复制
function iup_id(func)
    return function(o)
        if o.id ~= nil then
            _G[o.id] = func(o)
            return _G[o.id]
        else
            return func(o)
        end
    end
end

然后对所需部件进行声明。

代码语言:javascript
复制
--standard
iup.button = iup_id(iup.button)
iup.text = iup_id(iup.text)

这样就可以使用id的方式来直接引用GUI部件了,另外需要注意的是各个GUI部件要取不同的id值。 下面给出了我写的iupcd.lua的完整源代码以供参考。

代码语言:javascript
复制
require "iuplua"
require "iupluacontrols"

function iup_id(func)
    return function(o)
        if o.id ~= nil then
            _G[o.id] = func(o)
            return _G[o.id]
        elseif o.ID ~= nil then
            _G[o.ID] = func(o)
            return _G[o.ID]
        else
            return func(o)
        end
    end
end

--standard
iup.button = iup_id(iup.button)
iup.canvas = iup_id(iup.canvas)
iup.frame = iup_id(iup.frame)
iup.multiline = iup_id(iup.multiline)
iup.progressbar = iup_id(iup.progressbar)
iup.spin = iup_id(iup.spin)
iup.tabs = iup_id(iup.tabs)
iup.val = iup_id(iup.val)
iup.toggle = iup_id(iup.toggle)
iup.radio = iup_id(iup.radio)
iup.text = iup_id(iup.text)
iup.list = iup_id(iup.list)
iup.label = iup_id(iup.label)
--dialog
iup.dialog = iup_id(iup.dialog)
iup.filedlg = iup_id(iup.filedlg)
iup.messagedlg = iup_id(iup.messagedlg)
iup.colordlg = iup_id(iup.colordlg)
iup.fontdlg = iup_id(iup.fontdlg)
iup.alarm = iup_id(iup.alarm)
iup.getfile = iup_id(iup.getfile)
iup.gettext = iup_id(iup.gettext)
iup.listdialog = iup_id(iup.listdialog)
iup.message = iup_id(iup.message)
iup.scanf = iup_id(iup.scanf)
iup.getcolor = iup_id(iup.getcolor)
iup.getparam = iup_id(iup.getparam)
--layout
iup.fill = iup_id(iup.fill)
iup.vbox = iup_id(iup.vbox)
iup.hbox = iup_id(iup.hbox)
iup.zbox = iup_id(iup.zbox)
iup.cbox = iup_id(iup.cbox)
iup.sbox = iup_id(iup.cbox)
--additional
iup.cells = iup_id(iup.cells)
iup.colorbar = iup_id(iup.colorbar)
iup.colorbrowser = iup_id(iup.colorbrowser)
iup.dial = iup_id(iup.dial)
iup.gauge = iup_id(iup.gauge)
iup.tabs = iup_id(iup.tabs)
iup.matrix = iup_id(iup.matrix)
iup.tree = iup_id(iup.tree)
iup.glcanvas = iup_id(iup.glcanvas)
iup.pplot = iup_id(iup.pplot)
iup.olecontrol = iup_id(iup.olecontrol)
iup.speech = iup_id(iup.speech)
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 传输过程数值模拟学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档