The dotNet
Struct provides additional MAXScript methods for managing dotNet objects, classes, assemblies,
event handlers, and others.
Available in
3ds Max 9 and higher.
Assembly Loading:
<dotNetObject>dotNet.loadAssembly <assembly> [returnPassFail:bool]
This method loads the specified assembly. The assembly can be specified as an assembly
name or the filename of the dll containing the assembly.
The method first attempts to load an assembly from the application directory or from
the global assembly cache using the assembly name. If that fails, a dll with the assembly
name is searched in the directory containing the primary .net assemblies.
If
returnPassFail is false (the default), the assembly is found and successfully loaded, a dotNetObject
value wrapping the .net assembly value is returned, otherwise a value of undefined
is returned. If
returnPassFail is true, this method returns true if the assembly is loaded, false if not.
FOR EXAMPLE
|
dotnet.loadAssembly "system.xml"
dotnet.loadAssembly "system.xml.dll"
dotnet.loadAssembly "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\system.xml.dll"
dotnet.loadAssembly "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\system.xml"
|
NOTE:
You can do the same by using the existing dotNet classes:
FOR EXAMPLE
|
assembly = dotNetClass "System.Reflection.Assembly" r = assembly.loadfrom "F:\\FlashTrackBar\\bin\\Debug\\FlashTrackBar.dll"
|
Icon Loading:
dotNet.LoadIcon <iconName> iconSize:<point2> enabled:<bool> on:<bool> applyUIScaling:<bool>
Returns a
System::Drawing::Icon if the icon is found, undefined if not. Available in
in 3ds Max 2017 and higher.
The
iconName is the path to an icon file, or previously loaded icon resource.
The
iconSize is the size of the icon at 100% DPI scaling. The default is [24,24] if not specified.
The
enabled parameter specifies which version (enabled or disabled) of the icon to load.
The
on parameter specifies which version (on or off) of the icon to load.
The
applyUIScaling specifies whether to apply scaling to the icon on High DPI displays. The default
is true.
FOR EXAMPLE
|
hForm = dotNetObject "System.Windows.Forms.Form"
dotNetIcon = dotnet.loadIcon("PolyTools\TransformTools\PB_CW")
hForm.Icon = dotNetIcon --set the title bar icon of the form
hForm.show()
--Result: an empty form with a clockwise rotation arrow as icon
|
Event Handlers:
dotNet.addEventHandler <dotNetControl> <EventNameString> <function>
This method adds an event handler to the dotNetControl passed as the first argument.
The second argument specifies the event name as string. The list of all available
events for a given dotNetControl can be acquired by calling showEvents() on the dotNetControl
value.
The third argument is the custom MAXScript function to be called when the event is
triggered.
If the custom MAXScript function called by the handler expects two arguments, the
first argument must contain the sender and the second the eventArg.
If the event handler function specifies only one argument, then only the eventArg
is passed.
If the event handler function does not specify any arguments, none are passed.
See the example further on this page for details.
NOTE:You can register multiple functions with differing number of arguments for the same
event using
dotNet.addEventHandler() .
NOTE:You can only add functions that are registered in the global scope as event handlers.
If you need to register a function that is hidden from the global scop (for example,
contained in a struct), you need to define a global function to point to the hidden
function.
Example
|
struct dotNetStruct
(
myForm = dotNetObject "system.windows.forms.form",
myButton = dotNetObject "system.windows.forms.button"
)
ui = dotNetStruct()
ui.myForm.width = 100
ui.myForm.height = 100
ui.myButton.text = "Say Something"
ui.myForm.controls.add ui.myButton
-- A function "hidden" in a global struct
struct functionStruct
(
fn printSomething a b =
(
print "something"
)
)
myFunctionStruct = functionStruct()
--- Create a new function in global scope to point to our "hidden" function
fn theFunctionPrint a b = myFunctionStruct.printSomething a b
DotNet.AddEventHandler ui.mybutton "click" theFunctionPrint
ui.myForm.show()
|
dotNet.removeAllEventHandlers <dotNetControl>
Removes all event handlers from the specified dotNetControl.
dotNet.removeEventHandler <dotNetControl> <EventNameString> <function>
Removes the specified event handler function from the given dotNetControl and event.
The arguments list is the same as the
dotNet.addEventHandler() .
The callback function is specified as the third argument because there can be multiple
functions registered with the same event (see note above).
To remove all functions registered with a given event, see the following method:
dotNet.removeEventHandlers <dotNetControl> <EventNameString>
Removes all functions registered with the specified event of the dotNetControl.
EXAMPLE
|
(
fn buttonPressed1 a1 a2 =
(
print "1. Button Pressed - Two Arguments"
print a1
print a2
)
fn buttonPressed2 a1 =
(
print "2. Button Pressed - One Argument"
print a1
)
fn buttonPressed3 =
(
print "3. Button Pressed - No Arguments"
)
fn mouseEnter4 =
(
print "4. Mouse Entered The Button!"
)
fn mouseLeave5 =
(
print "5. Mouse Left The Button!"
)
local mButton = dotNetObject "System.windows.forms.button"
mButton.text = "Press Me"
mButton.size = dotNetObject "System.Drawing.Size" 100 100
mButton.location = dotNetObject "System.Drawing.Point" 10 10
local hForm = dotNetObject "System.Windows.Forms.Form"
hForm.size = dotNetObject "System.Drawing.Size" 120 140
hForm.controls.add mButton
dotNet.addEventHandler mButton "click" buttonPressed1
dotNet.addEventHandler mButton "click" buttonPressed2
dotNet.addEventHandler mButton "click" buttonPressed3
dotNet.addEventHandler mButton "MouseEnter" mouseEnter4
dotNet.addEventHandler mButton "MouseLeave" mouseLeave5
hForm.show()
ok
)
|
If you evaluate the above script, move the mouse over the button, click it, move the
mouse away from the button, and then back in and out, the Listener output appears
as follows:
|
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"5. Mouse Left The Button!"
"4. Mouse Entered The Button!"
"5. Mouse Left The Button!"
|
Adding the following line to the first (buttonPressed1) function causes the first
on Click event handler to be removed after the first click on the button. Clicking
a second time prints only the second and third functions to the Listener.
Within the function, the a1 argument contains the "sender", or the dotNetControl whose
event was triggered.
|
dotNet.removeEventHandlers a1 "click" buttonPressed1
|
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"5. Mouse Left The Button!"
--additional clicks result in no output from function 1
--but all others work
|
Adding the following line to the first (buttonPressed1) function causes all on Click
event handlers to be removed after the first click on the button.
Clicking a second time does not print anything to the Listener, but when the mouse
moves in and out, the other two functions are printed.
|
dotNet.removeEventHandlers a1 "click"
|
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
"5. Mouse Left The Button!"
"4. Mouse Entered The Button!"
"5. Mouse Left The Button!"
--additional clicks result in no output
--but moving the mouse prints to the Listener
|
Adding the following line to the first (buttonPressed1) function causes all event
handlers to be removed after the first click on the button including the on Click,
on MouseEnter, and on MouseLeave events.
|
dotNet.removeAllEventHandlers a1
|
OK
"4. Mouse Entered The Button!"
"1. Button Pressed - Two Arguments"
dotNetObject:System.Windows.Forms.Button
dotNetObject:System.Windows.Forms.MouseEventArgs
"2. Button Pressed - One Argument"
dotNetObject:System.Windows.Forms.MouseEventArgs
"3. Button Pressed - No Arguments"
--additional clicks result in no output
|
Additional Inspector Functions:
dotNet.showConstructors [<dotNetClass> | <dotNetObject> | <dotNetControl>] [to:<stream>]
Displays the constructors exposed by the specified DotNetClass, DotNetObject, or DotNetControl.
If
to is not specified, the output goes to Listerner, otherwise the output goes to the
specified stream.
dotNet.getType<type_string>
Returns a dotNetObject value that wraps the System:Type specified by the type string.
Enums:
dotnet.combineEnums {<dotNetObject> | <number>}+
Combines System::Enum-derived objects that are to be treated as bit fields (these
have a FlagsAttribute). The System::Enum-derived objects must all be of the same type.
The return value is of that type. If a number is specified, at least one System::Enum-derived
object must also be specified (to provide the type).
dotnet.CompareEnums{<dotNetObject> | <number>} {<dotNetObject> | <number>}
Performs bit-wise comparison of the two value. Returns true if at least one corresponding
bit is set in each value.
EXAMPLE
|
BindingFlags= dotNetClass "System.Reflection.BindingFlags"
--> dotNetClass:System.Reflection.BindingFlags
BindingFlags.Static.value__
--> 8
BindingFlags.Public.value__
--> 16
BindingFlags.FlattenHierarchy.value__
--> 64
res = dotNet.combineEnums BindingFlags.Static BindingFlags.Public 2
--> dotNetObject:System.Reflection.BindingFlags
res.value__
--> 26
dotNet.compareEnums res 1
--> false
dotNet.compareEnums res BindingFlags.Static
--> true
dotNet.compareEnums res BindingFlags.Public
--> true
dotNet.compareEnums res BindingFlags.FlattenHierarchy
--> false
|