MaxScript学习笔记目录
大家好,我是阿赵,之前介绍过MaxScript的各种UI控件的用法,这次介绍一些稍微复杂一点的用法。
一、例子介绍
这次要做的例子,有一部分是使用了dotNet的UI控件,一部分是MaxScript自带的UI用法,可以综合着一起看看。使用了donNet的UI控件,可以做出更复杂的UI展示。先看看例子的截图,下面有每个部分的详细说明,最后有完整的代码
二、详细说明
1、ListView
1.代码
rollout ListViewTestWin "ListViewTestWin" width:247 height:282
(
dotNetControl 'myList' "System.Windows.Forms.ListView" pos:[10,10] width:217 height:200 align:#left
on ListViewTestWin open do
(
--设置ListView的属性
myList.Fullrowselect = true
myList.GridLines = true
myList.View = myList.View.Details
--添加标题
myList.Columns.Add("Name")
myList.Columns.Add("Description")
--模拟数据
local items = #()
for i in 1 to 100 do
(
local tempVal = "Item "+(i as string)
append items tempVal
)
--把数据填充到ListView
for i = 1 to items.count do
(
row = dotNetObject "System.Windows.Forms.ListViewItem"
row.Text = items[i]
row.SubItems.Add("This is item " + i as string)
myList.Items.Add(row)
)
)
on myList mouseDown arg do
(
--arg.Clicks判断点击的次数,2就是双击,1是单击
if arg.Clicks == 2 do
(
--通过点击的位置判断点到的具体格子
hit=(myList.HitTest (dotNetObject "System.Drawing.Point" arg.x arg.y))
print hit.item.subItems.item[0].text
print hit.item.subItems.item[1].text
)
)
)
2.说明
(1)这个例子使用了dotNet的ListView组件,所以需要定义控件类型为System.Windows.Forms.ListView
(2)初始化操作,在on open事件里面,给控件填充数据
(3)填充数据部分,和C#本身并没有什么区别
(4)在获取点击数据的地方,由于HitTest 方法需要的传入参数是Point类型的,所以需要定义一下dotNetObject “System.Drawing.Point”
(5)mouseDown 事件会传入参数,可以判断点击的次数
2、ListBox
1.代码
rollout ListBoxTestWin "ListBoxTestWin" width:247 height:282
(
dotNetControl 'myList' "System.Windows.Forms.ListBox" pos:[10,10] width:217 height:200 align:#left
on ListBoxTestWin open do
(
--模拟数据
local items = #()
for i in 1 to 100 do
(
local tempVal = "Item "+(i as string)
append items tempVal
)
--填充数据
myList.Items.clear()
for i in 1 to 100 do
(
myList.Items.Add(items[i])
)
)
on myList mouseDown arg do
(
--获取当前选择的格子内容
print (myList.SelectedItem as string)
)
)
2.说明
(1)这个例子使用了dotNet的ListBox组件,所以要定义控件类型为System.Windows.Forms.ListBox
(2)初始化操作,在on open事件里面,给控件填充数据
(3)数据填充和获取部分都可以参考C#
3、Form窗体
1.代码
fn ShowFormFun =
(
-- 加载.NET Framework集成器
dotNet.loadAssembly "System.Windows.Forms"
dotNet.loadAssembly "System.Data"
-- 新建.NET窗体类
local form = dotNetObject "System.Windows.Forms.Form"
form.ClientSize = dotNetObject "System.Drawing.Size" 300 200 -- 定义窗体大小
form.text = "Test Window"
-- 添加.DataGridView控件
local myDataGrid = dotNetObject "System.Windows.Forms.DataGridView"
myDataGrid.Dock = myDataGrid.Dock.Fill -- 填满整个窗口
form.Controls.Add myDataGrid -- 将数据表格添加到窗体中
local myDataTable = dotNetObject "System.Data.DataTable"
local myDataColumn1 = myDataTable.Columns.Add "姓名"
myDataColumn1.DataType = dotNetClass "System.String"
local myDataColumn2 = myDataTable.Columns.Add "年龄"
myDataColumn2.DataType = dotNetClass "System.Int32"
myDataTable.Rows.Add #("张三",18)
myDataTable.Rows.Add #("李四",25)
myDataGrid.DataSource = myDataTable -- 绑定数据源
-- 显示窗体
form.ShowDialog()
)
rollout DotNetFormTestWin "DotNetFormTestWin" width:378 height:80
(
button 'btn1' "showForm" pos:[78,21] width:209 height:37 align:#left
on btn1 pressed do
(
ShowFormFun()
)
)
2.说明
(1)这里使用了2个dotNet的内容,第一个是窗体Form,第二个是DataGridView
(2)Form是不能嵌入MaxScript的rollout 的,它是一个独立的窗体,而且在打开的时候,会强制获取最高层的焦点。所以在显示Form的时候,原来的窗体是不能操作的。
(3)由于我们要直接的调用C#的代码,所以使用dotNet.loadAssembly来加载dotNet的库
(4)在调用C#方法的时候,都需要注意传入的数据的类型是什么,比如form.ClientSize 需要传入的类型是Size,所以要定义类型dotNetObject “System.Drawing.Size”
(5)由于Form并不是嵌入到rollout的,所以它需要单独的打开命令
form.ShowDialog()
(6)DataGridView作为数据表显示,通过DataTable赋值给DataSource,进行数据的绑定
4、自定义窗体
1.代码
rollout CustomPanelTestWin "CustomPanel" width:378 height:80
(
button 'btn1' "showWin" pos:[78,21] width:209 height:37 align:#left
on btn1 pressed do
(
createDialog CustomPanelWin
)
)
rollout CustomPanelWin "CustomPanel" width:378 height:221
(
button 'btn1' "closeWin" pos:[85,142] width:209 height:37 align:#left
label 'lbl1' "This is a Custom win" pos:[17,12] width:166 height:23 align:#left
colorPicker 'cp1' "ColorPicker" pos:[33,38] width:307 height:30 align:#left
slider 'sld1' "Slider" pos:[32,83] width:304 height:44 align:#left
on btn1 pressed do
(
DestroyDialog CustomPanelWin
)
)
2.说明
(1)这个是maxscript自己的rollout 实现的
(2)当点击按钮的时候,通过createDialog方法动态打开指定的窗体
(3)需要关闭窗体的时候,通过DestroyDialog方法关闭
5、折叠栏
1.代码
local theFloater = newRolloutFloater "Test" 400 400
addRollout ListViewTestWin theFloater rolledup:false
addRollout ListBoxTestWin theFloater rolledup:true
addRollout DotNetFormTestWin theFloater rolledup:true
addRollout CustomPanelTestWin theFloater rolledup:true
2.说明
(1)这个是MaxScript本身的功能,创建一个newRolloutFloater浮动窗体
(2)通过addRollout方法,把已经做好的rollout 插入到浮动窗体里面
(3)rolledup控制是否折叠的值,当为false时,内容展开,当为true时,内容折叠
三、完整代码
(
local CustomPanelWin
rollout ListViewTestWin "ListViewTestWin" width:247 height:282
(
dotNetControl 'myList' "System.Windows.Forms.ListView" pos:[10,10] width:217 height:200 align:#left
on ListViewTestWin open do
(
--设置ListView的属性
myList.Fullrowselect = true
myList.GridLines = true
myList.View = myList.View.Details
--添加标题
myList.Columns.Add("Name")
myList.Columns.Add("Description")
--模拟数据
local items = #()
for i in 1 to 100 do
(
local tempVal = "Item "+(i as string)
append items tempVal
)
--把数据填充到ListView
for i = 1 to items.count do
(
row = dotNetObject "System.Windows.Forms.ListViewItem"
row.Text = items[i]
row.SubItems.Add("This is item " + i as string)
myList.Items.Add(row)
)
)
on myList mouseDown arg do
(
--arg.Clicks判断点击的次数,2就是双击,1是单击
if arg.Clicks == 2 do
(
--通过点击的位置判断点到的具体格子
hit=(myList.HitTest (dotNetObject "System.Drawing.Point" arg.x arg.y))
print hit.item.subItems.item[0].text
print hit.item.subItems.item[1].text
)
)
)
rollout ListBoxTestWin "ListBoxTestWin" width:247 height:282
(
dotNetControl 'myList' "System.Windows.Forms.ListBox" pos:[10,10] width:217 height:200 align:#left
on ListBoxTestWin open do
(
--模拟数据
local items = #()
for i in 1 to 100 do
(
local tempVal = "Item "+(i as string)
append items tempVal
)
--填充数据
myList.Items.clear()
for i in 1 to 100 do
(
myList.Items.Add(items[i])
)
)
on myList mouseDown arg do
(
--获取当前选择的格子内容
print (myList.SelectedItem as string)
)
)
fn ShowFormFun =
(
-- 加载.NET Framework集成器
dotNet.loadAssembly "System.Windows.Forms"
dotNet.loadAssembly "System.Data"
-- 新建.NET窗体类
local form = dotNetObject "System.Windows.Forms.Form"
form.ClientSize = dotNetObject "System.Drawing.Size" 300 200 -- 定义窗体大小
form.text = "Test Window"
-- 添加.DataGridView控件
local myDataGrid = dotNetObject "System.Windows.Forms.DataGridView"
myDataGrid.Dock = myDataGrid.Dock.Fill -- 填满整个窗口
form.Controls.Add myDataGrid -- 将数据表格添加到窗体中
local myDataTable = dotNetObject "System.Data.DataTable"
local myDataColumn1 = myDataTable.Columns.Add "姓名"
myDataColumn1.DataType = dotNetClass "System.String"
local myDataColumn2 = myDataTable.Columns.Add "年龄"
myDataColumn2.DataType = dotNetClass "System.Int32"
myDataTable.Rows.Add #("张三",18)
myDataTable.Rows.Add #("李四",25)
myDataGrid.DataSource = myDataTable -- 绑定数据源
-- 显示窗体
form.ShowDialog()
)
rollout DotNetFormTestWin "DotNetFormTestWin" width:378 height:80
(
button 'btn1' "showForm" pos:[78,21] width:209 height:37 align:#left
on btn1 pressed do
(
ShowFormFun()
)
)
rollout CustomPanelTestWin "CustomPanel" width:378 height:80
(
button 'btn1' "showWin" pos:[78,21] width:209 height:37 align:#left
on btn1 pressed do
(
createDialog CustomPanelWin
)
)
rollout CustomPanelWin "CustomPanel" width:378 height:221
(
button 'btn1' "closeWin" pos:[85,142] width:209 height:37 align:#left
label 'lbl1' "This is a Custom win" pos:[17,12] width:166 height:23 align:#left
colorPicker 'cp1' "ColorPicker" pos:[33,38] width:307 height:30 align:#left
slider 'sld1' "Slider" pos:[32,83] width:304 height:44 align:#left
on btn1 pressed do
(
DestroyDialog CustomPanelWin
)
)
local theFloater = newRolloutFloater "Test" 400 400
addRollout ListViewTestWin theFloater rolledup:false
addRollout ListBoxTestWin theFloater rolledup:true
addRollout DotNetFormTestWin theFloater rolledup:true
addRollout CustomPanelTestWin theFloater rolledup:true
)