try(destroyDialog testR)catch() rollout testR "Test" ( dotNetControl testButton "system.windows.forms.button" height:50 ) createDialog testR
FlatStyle is property that will allow you to select from one of four options to affect the look of a button. Use "showProperties theButton.flatStyle" and it will print out the options as shown below. .Standard is the default, .Flat will draw the button with just an out line around it, .popup will set it so that it is flat until the mouse moves over the button when it will display as 3D and .system will look the same as the default Max script buttons.
, read-only, static .Popup : , read-only, static .Standard : , read-only, static .System : , read-only, static
To set the flat style there are two options. First you can create the flatStyle object using dotNetClass and then setting the desired style and passing that to the .flatStyle property. I have commented that method out for a simpler version where you use the .flatStyle property that it self returns a dotNetObject and do the same by setting the style that you would use to use. Either way works and from test I don't see one being any faster then the other but the second is less code.
TextAlign will allow the position of the text property to be set and has several properties. Note that it doesn't appear to work when the system option is used. You can set this property using the dotNetClass method or the same method that we used to set the flatStyle.
BackColor is a property that you will find in most dotNetControls and needs the dotNetObject system.drawing.color to be created and a color passed in. Here I'm using one of the preset colours, lightBlue but you can also use the fromARGB method and set your own RGB colors values.
on testR open do ( --Use -- testButton.flatStyle=(dotNetClass "System.Windows.Forms.FlatStyle").flat --Or testButton.flatStyle=testButton.flatStyle.flat testButton.text="Test Button" testButton.TextAlign= (dotNetClass "System.Drawing.ContentAlignment").BottomRight testButton.backColor=testButton.backColor.lightBlue )
The first and second event handlers are using both the senderArg and the arg arguments for the mouseDown and mouseUp. SenderArg just holds a reference to the button itself, you could replace senderArg in the event handler expression use testButton and get the same result. The reason we're using the senderArg is it would be easier to cut and paste the event handlers for other buttons as we would only have to change testButton to the name of the new button.
In this case we are testing to see which mouse button has been pressed on the button. We use the arg argument to do this. If you use "showProperties arg" you can get the seven properties that can be accessed.
, read-only .Clicks : , read-only .Delta : , read-only .Location : , read-only .X : , read-only .Y : , read-only .Empty : , read-only, static
We will use the .button property that returns a "system.windows.forms.mouseButtons" object, in the listener use showProperties (dotnetclass "System.Windows.Forms.MouseButtons") to get the properties of the object. Depending on which button is pressed, the mouseButtons object will return on of the six properties. We can then use that to test what button has been pressed.
, read-only, static .Middle : , read-only, static .None : , read-only, static .Right : , read-only, static .XButton1 : , read-only, static .XButton2 : , read-only, static
Once the button has been tested we can use the backColor property and set the color as we did to initialize the button in the on open handler.
on testButton mouseDown senderArg arg do ( if arg.button==arg.button.left then ( senderArg.backColor=senderArg.backColor.red ) ) on testButton mouseUp senderArg arg do ( if arg.button==arg.button.left then ( senderArg.backColor=senderArg.backColor.green ) )
The next two event handlers will set the colour as the mouse moves into and out of the area of the button.
on testButton mouseEnter senderArg arg do ( senderArg.backColor=senderArg.backColor.green ) on testButton mouseLeave senderArg arg do ( senderArg.backColor=senderArg.backColor.lightBlue )
"just an extra bit while we are on the subject of buttons - when using the flat styling property, you don't have to use the mouseover and mousedown handlers to change button color states, there is a flatappearance property that can do this for you"
Using the code below means you don't have to use the event handlers at all to get the colours to change.
try(destroyDialog testR)catch() rollout testR "Test" ( local Ccolor = dotnetclass "system.drawing.color" dotNetControl testButton "system.windows.forms.button" height:50 on testR open do ( testButton.flatStyle=testButton.flatStyle.flat testButton.text="Test Button" testButton.TextAlign= (dotNetClass "System.Drawing.ContentAlignment").BottomRight testButton.backColor=testButton.backColor.lightBlue testButton.flatappearance.mouseoverbackcolor = Ccolor.green testButton.flatappearance.mousedownbackcolor = Ccolor.red testButton.flatappearance.bordercolor = Ccolor.yellow ) ) createDialog test
We can set the font using the "System.Drawing.Font" object. Either add a function that can be called when the rollout opens or just add it to the on open event handler as we have above. This method can be applied any where that the font can be changed in a dotNetControl.
testButton.font = dotNetObject "System.Drawing.Font" "Times New Roman" 24
In this example not only is the font type being changed but also a font style is being created and applied to the button. For this the "System.Drawing.FontStyle" class is needed to be able to create the styles that will be used. Because we are adding two different styles the dotNet.combineenums command is used to create a single style object that uses both styles.
dnFontStyle = dotNetClass "System.Drawing.FontStyle" myFontStyle = dotnet.combineenums dnFontStyle.bold dnFontStyle.italic testButton.font = dotNetObject "System.Drawing.Font" "Times New Roman" 24 myFontStyle