lua

require('CLRPackage') -- 这个在包的 sample目录下面




import('System')
import('System.Windows.Forms')
import('NLua')

import('System.IO')

//这个要用 全名, 不然会报错

import('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')




System = CLRPackage("System", "System")
Forms = CLRPackage("System.Windows.Forms", "System.Windows.Forms")
Drawing = CLRPackage("System.Drawing", "System.Drawing")
NLua = CLRPackage("NLua", "NLua")
IO = CLRPackage("System.IO", "System.IO")




Form=Forms.Form
TextBox=Forms.TextBox
Label=Forms.Label
ListBox=Forms.ListBox
Button=Forms.Button
Point=Drawing.Point
Size=Drawing.Size
Lua=NLua.Lua
OpenFileDialog=Forms.OpenFileDialog
File=IO.File
StreamReader=IO.StreamReader
FileMode=IO.FileMode
ScrollBars=Forms.ScrollBars
FormBorderStyle=Forms.FormBorderStyle
FormStartPosition=Forms.FormStartPosition


function clear_click(sender,args)
  code:Clear()
end


function execute_click(sender,args)
  results.Items:Clear()
  result=lua:DoString(code.Text)
  if result then
    for i=0,result.Length-1 do
      results.Items:Add(result[i])
    end
  end
end


function load_click(sender,args)
  open_file:ShowDialog()
  file=StreamReader(open_file.FileName)
  code.Text=file:ReadToEnd()
  file:Close()
end


form = Form()
code = TextBox()
label1 = Label()
execute = Button()
clear = Button()
results = ListBox()
label2 = Label()
load = Button()
lua = Lua()
--lua:OpenBaseLib() -- steffenj: Open*Lib() functions no longer exist
open_file = OpenFileDialog()


form:SuspendLayout()


code.Location = Point(16,24)
code.Multiline = true
code.Name = "Code"
code.Size = Size(440, 128)
code.ScrollBars = ScrollBars.Vertical
code.TabIndex = 0
code.Text = ""


label1.Location = Point(16, 8)
label1.Name = "label1"
label1.Size = Size(100, 16)
label1.TabIndex = 1
label1.Text = "Lua Code:"


execute.Location = Point(96, 160)
execute.Name = "Execute"
execute.TabIndex = 2
execute.Text = "Execute"
execute.Click:Add(execute_click)


clear.Location = Point(176, 160)
clear.Name = "Clear"
clear.TabIndex = 3
clear.Text = "Clear"
clear.Click:Add(clear_click)


results.Location = Point(16, 208)
results.Name = "Results"
results.Size = Size(440, 95)
results.TabIndex = 4


label2.Location = Point(16, 192)
label2.Name = "label2"
label2.Size = Size(100, 16)
label2.TabIndex = 5
label2.Text = "Results:"


load.Location = Point(16, 160)
load.Name = "Load"
load.TabIndex = 6
load.Text = "Load..."
load.Click:Add(load_click)


open_file.DefaultExt = "lua"
open_file.Filter = "Lua Scripts|*.lua|All Files|*.*"
open_file.Title = "Pick a File"


form.AutoScaleBaseSize = Size(5, 13)
form.ClientSize = Size(472, 315)
form.Controls:Add(load)
form.Controls:Add(label2)
form.Controls:Add(results)
form.Controls:Add(clear)
form.Controls:Add(execute)
form.Controls:Add(label1)
form.Controls:Add(code)
form.Name = "MainForm"
form.Text = "LuaNet夏静"
form.FormBorderStyle = FormBorderStyle.Fixed3D
form.StartPosition = FormStartPosition.CenterScreen
form:ResumeLayout(false)


form:ShowDialog()

--------------------------------------------------------------------------------

 string script = File.ReadAllText("Form1.Lua");
            byte[] scriptUtf8 = Encoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(script));    //Default is GB2312 on Chinese OS.


            try
            {
                using (Lua lua = new Lua())
                {


                    lua.LoadCLRPackage();


                    //lua.DoString(@" import ('NLuaSample') ");


                    //lua["gValue"] = "This is a global value"; // You can set a global value.


                    var returns = lua.DoString(scriptUtf8);




                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值