阿赵的MaxScript学习笔记分享十六《MaxScript和WinForm交互》

MaxScript学习笔记目录

大家好,我是阿赵。

之前有网友给我留言说MaxScript和DotNet的WinForm界面交互有问题。我回头看了一下,发现之前的MaxScript文章是漏了这部分没有说明,这里补充一下。这里的内容主要是用C#写一个WinForm界面,然后导出给MaxScript使用和交互。

一、 WinForm部分

1、建立WinForm界面

这里我简单的建一个C#的WinForm项目,然后简单的做一个界面:

这个界面的名字就叫做Form1,各位也可以根据自己情况起名字。不过这个名字是需要记下来的,因为在MaxScript里面调用,需要这个名字。

在Form1里面有一个TextBox和2个Button。然后在里面写入一些简单的代码:

bash 复制代码
using System;
using System.Windows.Forms;

namespace DotnetUITest
{
    public partial class Form1 : Form
    {
        private int num = 0;
        public Form1()
        {
            InitializeComponent();
        }
//获取当前的num值
        public int GetCurrentNum()
        {
            return num;
        }
//获取第二个按钮,用于给MaxScript注册点击事件
        public Button GetButton2()
        {
            return button2;
        }

        //按钮1本地点击功能测试
        private void button1_Click(object sender, EventArgs e)
        {
            num++;
            textBox1.Text = num.ToString();
        }
    }
}

2、 导出dll

做好界面和代码之后,要把这个界面导出成dll给MaxScript使用。

先找到项目属性:

然后把输出类型给出类库:

最后在生成里面选择生成解决方案:

这里要记住自己项目的项目名称,比如我这个项目的名称是:DotnetUITest

然后在生成的地方可以看到了这个项目的dll文件。

二、 MaxScript部分

1、 显示WinForm

先写以下代码,用于显示WinForm

bash 复制代码
fn main =(

 dotnet.loadAssembly ("D:/DotnetUITest.dll")
   
	global Form1 = dotnetObject "DotnetUITest.Form1"
	Form1.Show()
)

main()

说明:

我是简单的把dll文件放在了d盘,所以通过:

bash 复制代码
dotnet.loadAssembly ("D:/DotnetUITest.dll")

就可以把这个指定的dll加载进来。

然后需要加载Form1,需要通过一个全局变量把Form1保存起来:

bash 复制代码
global Form1 = dotnetObject "DotnetUITest.Form1"

最后把Form1显示出来

bash 复制代码
Form1.Show()

写好代码之后,按Ctrl+E运行脚本,会看到WinForm的界面显示出来了:

为了测试一下里面的功能是否正常,之前给button1写了个点击处理方法,每点一下,TextBox里面的数字会加一:

到这里,界面显示没问题。

2、 找到界面里面的按钮并注册点击事件

接着写代码:

bash 复制代码
fn main =(

 dotnet.loadAssembly ("D:/DotnetUITest.dll")
   
	global Form1 = dotnetObject "DotnetUITest.Form1"
	Form1.Show()
)

main()

fn clickBtnHandle sender args = 
( 
	local content = "click from winform"
	messageBox content
	print content
)
				
fn AddBtnHandleFn = 
( 
	local button = Form1.GetButton2()

	dotNet.addEventHandler button "Click" clickBtnHandle 
)

AddBtnHandleFn()

这里做了2个事情:
1. 写一个点击按钮的回调处理方法

bash 复制代码
fn clickBtnHandle sender args = 
( 
	local content = "click from winform"
	messageBox content
	print content
)

如果按钮被点击,这里会弹窗和打印"click from winform",证明按钮被点击了。
2. 注册点击事件

bash 复制代码
fn AddBtnHandleFn = 
( 
	local button = Form1.GetButton2()

	dotNet.addEventHandler button "Click" clickBtnHandle 
)

由于Form1我留了个方法叫做GetButton2,会把button2返回,所以这里调用就可以获得button2了

然后通过

bash 复制代码
dotNet.addEventHandler button "Click" clickBtnHandle 

把这个按钮的点击事件注册为clickBtnHandle

这个时候再运行脚本,点击button2:

会出现弹窗:

并且在MaxScript Listener里面出现打印内容:

3、 修改原有的按钮内容:

继续修改代码,给button2修改一下text:

运行脚本,会看到button2的文本被改变了:

4、 新增按钮

接下来不获取button2,而是直接往Form1里面新增一个按钮:

bash 复制代码
fn main =(

 dotnet.loadAssembly ("D:/DotnetUITest.dll")
   
	global Form1 = dotnetObject "DotnetUITest.Form1"
	Form1.Show()
)

main()

fn clickBtnHandle sender args = 
( 
	local content = "click from winform"
	messageBox content
	print content
)
				
fn AddBtnHandleFn = 
( 
	--local button = Form1.GetButton2()
	local button = dotNetObject "System.Windows.Forms.Button"
	Form1.controls.add button
	button.text = "new Button"
	button.Location.x = 10
	button.Location.y = 50
	dotNet.addEventHandler button "Click" clickBtnHandle 
)

AddBtnHandleFn()

这里主要改变的代码是:

local button = dotNetObject "System.Windows.Forms.Button"

Form1.controls.add button

button.text = "new Button"

button.Location.x = 10

button.Location.y = 50

新增了一个按钮,并且添加到Form1的controls里面,然后修改它的文本和位置,这时候运行脚本,会看到:

5、 从WinForm方法获取值

其实之前获取button2的时候,我们就是通过调用GetButton2方法来返回了一个按钮对象。

bash 复制代码
fn main =(

 dotnet.loadAssembly ("D:/DotnetUITest.dll")
   
	global Form1 = dotnetObject "DotnetUITest.Form1"
	Form1.Show()
)

main()

fn clickBtnHandle1 sender args = 
( 
	local content = "click from winform"
	messageBox content
	print content
)
	
fn clickBtnHandle2 sender args = 
( 
	local num = Form1.GetCurrentNum()
	print num
)

fn AddBtnHandleFn = 
( 
	local button2 = Form1.GetButton2()
	dotNet.addEventHandler button2 "Click" clickBtnHandle1
	
	local button3 = dotNetObject "System.Windows.Forms.Button"
	Form1.controls.add button3
	button3.text = "new Button"
	button3.Location.x = 10
	button3.Location.y = 50
	dotNet.addEventHandler button3 "Click" clickBtnHandle2 
)

AddBtnHandleFn()

这里稍微再重复一下,首先把button2和新增的按钮都注册了不同的点击方法,然后新按钮的点击方法,从Form1里面调用GetCurrentNum方法获得一个数值,并且打印出来:

过程就不再重复,直接运行,可以看到,点button1,num的值会变,然后点新增的按钮,可以把当前的值打印出来:

相关推荐
知识点集锦3 小时前
代发考试战报:2月5号最近考过的思科和华为考试战报
网络·学习·安全·华为·云计算
大地之灯7 小时前
深度学习每周学习总结R6(RNN实现阿尔茨海默病诊断)
rnn·深度学习·学习
不会敲代码的狗7 小时前
Acwing-基础算法课笔记之基础算法(差分)
笔记·算法
LuckyLay8 小时前
Golang学习笔记_29——抽象工厂模式
笔记·学习·golang·抽象工厂模式
嗷嗷哦润橘_10 小时前
Deepseek系列从v3到R易背面经版
开发语言·人工智能·笔记·deepseek
霜雪殇璃10 小时前
(定时器,绘制事件,qt简单服务器的搭建)2025.2.11
运维·服务器·qt·学习
漂亮_大男孩10 小时前
深度学习|表示学习|Instance Normalization 全面总结|26
人工智能·深度学习·神经网络·学习·cnn
S190111 小时前
Django学习笔记(第一天:Django基本知识简介与启动)
笔记·学习·django
DXX——12 小时前
嵌入式基础学习|C语言篇——结构体(含进阶)二
c语言·开发语言·学习
feifeikon12 小时前
NLP&深度学习 DAY7:平滑、语境学习、Scaling Law、大模型的发展、LLM的构建流程
深度学习·学习·自然语言处理