Dotnet Maxscript form / by John Crawshaw

Here is a short and sweet blog post on how to create a dotnet form in 3Dsmax. There are a few out there, but I thought It would be good to share the one I use on a daily basis. The best feature of this is the fact that you can drag the UI around. This basically allows it to run like a normal windows UI but with all the nice UI features of dotnet. In the video I have shown how you can quickly adjust the height and width of the UI, and the close button will always align to the top right. 

Here is the code below. Hope it helps you out! I'm going to try and add some standard buttons and dropdown as examples over the coming weeks. Almost like a tool kit of how to get started with dotnet. As always any questions just leave a comment below and I will attempt to answer it the best I can.

John

try(dn_form_Tasks.close())catch()
    
Ccolor = dotnetclass "system.drawing.color" 
Padding = 6
HexFont = dotNetObject "System.Drawing.Font" "Verdana" 10 ((dotNetClass "System.Drawing.FontStyle").bold)
fnt_Titles = dotNetObject "System.Drawing.Font" "Myriad Pro Regular" 8 ((dotNetClass "System.Drawing.FontStyle").regular)

--Dotnet Colours
global TopbarGrey = (dotNetClass "system.drawing.color").fromArgb 73 73 73
BackGrey = (dotNetClass "system.drawing.color").fromArgb 29 29 29

--Form Settings
dn_form_Tasks = dotNetObject "MaxCustomControls.MaxForm"
fn changeBackcolor = (dn_form_Tasks.backColor = BackGrey)
dotnet.AddEventHandler dn_form_Tasks "BackColorChanged" changeBackcolor
dn_form_Tasks.AllowTransparency=true
dn_form_Tasks.opacity=1
dn_form_Tasks.width=600
dn_form_Tasks.height=200
dn_form_Tasks.FormBorderStyle=(dotNetClass "System.Windows.Forms.FormBorderStyle").none  

--'Close' Button
dn_btn_Close = dotnetObject "system.windows.forms.button"
dn_btn_Close.width=35
dn_btn_Close.height=34
dn_btn_Close.top=0  
dn_btn_Close.left=(dn_form_Tasks.width-35)
dn_btn_Close.flatStyle=(dotNetClass "System.Windows.Forms.FlatStyle").flat
dn_btn_Close.backcolor = Ccolor.lightgray
dn_btn_Close.forecolor = Ccolor.dimgray
dn_btn_Close.font = HexFont
dn_btn_Close.text = "x"
dn_btn_Close.flatappearance.bordersize = 0
dn_form_Tasks.controls.add dn_btn_Close

fn md_btn_Close dn_btn_Close evnt =
(
    dn_form_Tasks.dispose()
)
dotnet.addEventHandler dn_btn_Close "MouseUp" md_btn_Close

--Asset ID Label
lbl_AssetID = dotNetObject "label"
lbl_AssetID.text = "Create Project - Main two"
lbl_AssetID.backColor = TopbarGrey
lbl_AssetID.Width = dn_form_Tasks.width
lbl_AssetID.Height = 23
lbl_AssetID.left = 5
lbl_AssetID.top = 11
lbl_AssetID.forecolor = Ccolor.darkgray
dn_form_Tasks.controls.add lbl_AssetID

--Shotgun Image
TopBar = dotnetobject "Windows.Forms.PictureBox"
TopBar.backcolor = TopbarGrey
TopBar.width = dn_form_Tasks.width
TopBar.height = 35
TopBar.left = 0
dn_form_Tasks.controls.add TopBar

global mouseOffset = [0,0]
global dragging = 0

fn lbl_AssetIDDown lbl_AssetID evnt =
(
    mouseOffset[1] = mouse.screenPos.x - dn_form_Tasks.left
    mouseOffset[2] = mouse.screenPos.y - dn_form_Tasks.top
    global dragging=1
)
dotnet.addEventHandler lbl_AssetID "MouseDown" lbl_AssetIDDown

fn lbl_AssetIDMove lbl_AssetID evnt =
(
    if dragging==1 then
    (
    local FormPos = #()
    local NewPos = #()  
    FormPos[1] = dn_form_Tasks.left
    FormPos[2] = dn_form_Tasks.top
        
    NewPos[1] = mouse.screenpos.x - mouseOffset[1]
    NewPos[2] = mouse.screenpos.y - mouseOffset[2]
    
    dn_form_Tasks.left = NewPos[1]
    dn_form_Tasks.top = NewPos[2]
    )
)
dotnet.addEventHandler lbl_AssetID "MouseMove" lbl_AssetIDMove

fn lbl_AssetIDUp lbl_AssetID evnt =
(
    global dragging=0
)
dotnet.addEventHandler lbl_AssetID "MouseUp" lbl_AssetIDUp



--UI FINISH



--Declare and create form
dotNet.setLifeTimeControl dn_form_Tasks #dotNet
dn_form_Tasks.showModeless()
fn getScreenResolution=
(
    screen = (dotNetClass "System.Windows.Forms.Screen").PrimaryScreen.Bounds
    return #(screen.Width, screen.Height)
)
res = getScreenResolution()
ResWidth = (res[1]/2)-(dn_form_Tasks.width/2)
ResHeight = (res[2]/2)-(dn_form_Tasks.height/2)
dn_form_Tasks.top = ResHeight
dn_form_Tasks.left = ResWidth