65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Lua 控制您的应用程序

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.21/5 (14投票s)

2005年5月6日

CPOL

2分钟阅读

viewsIcon

72308

downloadIcon

3486

将 Lua 集成到你的应用程序的方法。

*Lua 脚本(如下)用于自定义 UI 的首选项和内容(如上)。

引言

本文展示了一种将 Lua 集成到你的应用程序的简单方法。

什么是 Lua?

Lua 是一种扩展编程语言,旨在支持带有数据描述设施的通用过程编程。它还为面向对象编程、函数式编程和数据驱动编程提供了良好的支持。 Lua 旨在用作任何需要配置语言的程序的强大、轻量级配置语言。 Lua 作为库实现,用清晰的 C 编写(即 ANSI C 和 C++ 的通用子集中)。

Lua 语法示例(for 循环)

for i=1,10 do
    -- the first program in every language
    io.write("Hello world, from ",_VERSION,"!\n")
end

背景

此示例是一个 WTL 应用程序(简单的 HTML 帮助系统),它集成了 Lua 脚本来自定义首选项和内容。

它定义了这些 Lua 函数

-- # MessageBox
-----------------------------------------------------------------
-- int MessageBox(
-- string msg, |= Message to display
-- string capition |= Capition of Box
-- );
-- Return Value:
-- if 1 the user click in OK or user close the box


-- # ShowContentPainel
-----------------------------------------------------------------
-- void ShowContentPainel(
-- bool bShow |= If true, the painel start opened, if false not.
-- );

-- # SetWindowStartSize
-----------------------------------------------------------------
-- void SetWindowStartSize(
-- number w, |= Start W size of window
-- number h, |= Start H size of window
-- );
-- Remarks:
-- if this function is not called, the default size is 800 x 600

-- # SetMinWindowSize
-----------------------------------------------------------------
-- void SetMinWindowSize(
-- number w, |= Minimum W size of window
-- number h, |= Minimum H size of window
-- );

-- # SetTitle
-----------------------------------------------------------------
-- void SetTitle(
-- string title |= Text that be title of window.
-- );

-- # Navigate
-----------------------------------------------------------------
-- void Navigate(
-- string url |= Url
-- );

-- # InsertItemInPainel
-----------------------------------------------------------------
-- void InsertItemInPainel(
-- string title, |= Text displayed in tree
-- string url, |= Url
-- number icon, |= Icon of item, the possible values are: 
                        0 = BOOK, 1 = FILE, 2 = NETFILE
-- number id, |= Id of item, this has to be unique and start in 1
-- number idp |= Parent item, this is a ID 
                 of a item that is the parent or '0' for root item.
-- );
-- sample:
-- InsertItemInPainel("Trinity Systems", 
       "http://www.novaamerica.net/trinitysystems/", 0, 1, 0);
        -- ICON BOOK / ID 1 / In ROOT
-- InsertItemInPainel("Orion", 
       "http://www.novaamerica.net/trinitysystems/Orion", 2, 2, 1); 
       -- ICON NETFILE / ID 2 / In ID1 (Trinity Systems)

-- # ExpandItemInPainel
-----------------------------------------------------------------
-- void ExpandItemInPainel(
-- string id |= Id of item
-- );
-- Remarks:
-- This function need to be called after InsertItemInPainel's

...现在我将展示如何在 Lua/C++ 中创建这些函数。

代码

  1. 首先要做的是构建包含 Lua 的 DLL。 (下载 Lua DLL 演示项目 - 224 Kb
  2. 在你的项目中链接它
    //
    // For sample:
    //
    //----------------------------------------------------------
    // Library Linkage
    //----------------------------------------------------------
    //-
    #if defined (_DEBUG)
    #pragma comment( lib, "lua.lib" ) // Lua Support
    #else
    #pragma comment( lib, "lua.lib" ) // Lua Support
    #endif
    //-

    记住:你需要更改你的项目属性 -> 链接器 -> 常规 -> 附加库目录:Lua lib 目录。

  3. 添加 Lua 包含文件
    extern "C" 
    {
    #include "lua.h"
    }

    记住:你需要更改你的项目属性 -> C/C++ -> 常规 -> 附加包含目录:Lua include 目录。

    请注意,Lua lib 中的所有文件都需要保持 "c" 扩展名,因为 Lua 被编写为符合 ANSI C 标准。

  4. 现在我们需要启动 Lua VM,如下所示
    LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
                           LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
      //...
      // Lua
      //
    
      lua_State *luaVM = lua_open(); /* Open Lua */
    
      luaopen_base(luaVM );     /* opens the basic library */
      luaopen_table(luaVM );    /* opens the table library */
      luaopen_io(luaVM );       /* opens the I/O library */
      luaopen_string(luaVM );   /* opens the string lib. */
      luaopen_math(luaVM );     /* opens the math lib. */
    
      if (NULL == luaVM)
      {
        MessageBox("Error Initializing lua\n");
      }
    
      //...
      // Do things with lua code.
      // see below
      //...
    
      lua_close(luaVM); /* Close Lua */
      //
    
      // End
      //...
    }
  5. 现在我们创建一个 Lua 和 C/C++ 之间的粘合函数。

    Lua API 函数是

    lua_register(s, n, g)

    其中

    • slua_State 来注册该函数。
    • n:暴露给 Lua 的函数名称。
    • g:C/C++ 粘合函数。

    请参阅示例

    //...
    // Do things with lua code.
    lua_register( luaVM, "SetHome", l_SetHome );
    //...
    // ----------------------------------------------------
    // #Lua Functions
    // ----------------------------------------------------
     
    static int l_SetTitle( lua_State* luaVM)
    {
        const char* title = luaL_checkstring(luaVM, 1);
    
        theMainFrame->SetWindowText(title);
    
        return 0;
    }
  6. 现在我们需要加载并执行 Lua 脚本。
    //...
    // Do things with lua code.
    lua_register( luaVM, "SetHome", l_SetHome );
    //more glue functions
    lua_dofile(luaVM, "hrconf.lua");

    Lua API 函数是

    lua_dofile(s, p)

    其中

    • slua_State 来注册该函数。
    • p:Lua 脚本文件的路径。

    查看脚本.

为了完全理解 Lua API,请参阅此链接

关注点

Lua 是自由软件

历史

  • 2005年4月29日:首次发布。
© . All rights reserved.