在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
什么是Lua Lua例子(FOR 循环) for i=1,10 do -- the first program in every language io.write("Hello world, from ",_VERSION,"!\n") end 详细描述可以参考Lua网站http://www.lua.org/about.html 背景 -- # 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: -- ICON BOOK / ID 1 / In ROOT -- InsertItemInPainel("Trinity Systems", "http://www.novaamerica.net/trinitysystems/", 0, 1, 0); -- ICON NETFILE / ID 2 / In ID1 (Trinity Systems) -- InsertItemInPainel("Orion", "http://www.novaamerica.net/trinitysystems/Orion", 2, 2, 1); -- # ExpandItemInPainel ------------------------------------------------------------------ -- void ExpandItemInPainel( -- string id |= Id of item -- ); -- Remarks: -- This function need to be called after InsertItemInPainel's 现在我将展示如何使用Lua/C++创建这些函数。 代码 1. 首先要做的是创建含Lua的DLL // // For sample: // //--------------------------------------------- 记得:要更改项目的属性Project Property -> linker -> general -> additional library directory 3. 添加Lua包含文件: extern "C" { #include "lua.h" } 记得:要更改项目的属性 Project Property -> C/C++ -> general -> additional include directories 4. 现在我们需要按如下方式启动Lua VM LRESULT OnCreate(UINT , WPARAM , LPARAM , BOOL& ) { //... // Lua // lua_State *luaVM = lua_open(); // luaopen_base(luaVM ); luaopen_table(luaVM ); luaopen_io(luaVM ); luaopen_string(luaVM ); luaopen_math(luaVM ); if (NULL == luaVM) { MessageBox("Error Initializing lua\n"); } //... // Do things with lua code. // see below //... lua_close(luaVM); // // End //... } 5. 现在我们写Lua 和 C/C++ 结合的函数 lua_register(s, n, g) 这里: 请看下面例子: //... // 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) 这里: 为了完全理解Lua API,可以参考Lua 参考手册http://www.lua.org/manual/5.1/manual.html 趣点: |
请发表评论