Adding a button to a flash movie is one of the basic requirements. Aspose.Flash provides a Button namespace which allows you to add a button into a flash movie conveniently. In order to add a button we first need to create ButtonRecord objects. ButtonRecord class represents a shape that is drawn when a button is in particular state. Shapes can be drawn for each of three button states i.e. Over, Up and Down. A shape is also used to define the active area of the button. A single ButtonRecord can be applied to more than one button states, or there could be multiple button records for any ButtonState.
In this example, we’ll simply add a button to a flash movie, and we’ll not perform any action. This will allow us to understand how to add a button to a flash movie; however, in order to understand the actions of a button and other features, we have provided some other topics. The following steps can be used to add a button:
- Create FlashContainer object and set FrameRate and FrameSize properties
- Define background color using SetBackgroundColor class
- Create a new font using DefineFont2 class
- Create a text object using DefineEditText class; this text will be displayed on button
- Define button states using ButtoState enumeration
- Define two ButtonRecord objects and add those to an ArrayList
- Add DefineButton2 to the FlashContainer object
- Place the DefineButton2 object on the display list using PlaceObject2 class
- Add ShowFrame object and save the movie
The example below will show you how to add button in the flash movie.
[C#]
//create a flash container
FlashContainer flash = new FlashContainer();
/// set frame rate
flash.FrameRate = 24.0f;
/// set frame size
flash.FrameSize = new Rect(0, 0, 4000, 4000);
/// define background
SetBackgroundColor bg = new SetBackgroundColor(new RGB(255, 200, 255, 255));
flash.Add(bg);
//define array lists for push and action objects
ArrayList pushArray = new ArrayList();
ArrayList actionArray = new ArrayList();
//define fill styles
ArrayList listShape = new ArrayList();
listShape.Add(new SolidFill(new RGB(204, 204, 204, 255)));
//define line styles
ArrayList listLine = new ArrayList();
listLine.Add(new SolidLine(20, new RGB(0, 0, 0, 255)));
Shape shape = new Shape();
//define style change record and set fill style and line style
StyleChangeRecord scr = new StyleChangeRecord(2400, 450);
scr.OverlapFillStyle = 1;
scr.LineStyle = 1;
shape.Add(scr);
//add four straight edges to draw a rectangle
shape.Add(new StraightEdgeRecord(-2400, 0));
shape.Add(new StraightEdgeRecord(0, -450));
shape.Add(new StraightEdgeRecord(2400, 0));
shape.Add(new StraightEdgeRecord(0, 450));
//create DefineShape object and apply line style and fill style
DefineShape defineShape =
new DefineShape(flash.NewIdentifier(), new Rect(-10, -10, 2410, 460), listShape, listLine, shape);
//create a new font
DefineFont2 font = new DefineFont2(flash.NewIdentifier(), "Verdana");
font.Small = false;
font.Italic = false;
font.Bold = false;
font.Language = TextLanguage.Latin;
font.Leading = 0;
font.CodeTable = new ArrayList();
font.GlyphShapeTable = new ArrayList();
flash.Add(font);
//create DefineEditText object to add text
Rect bounds = new Rect(-40, -40, 2000, 596);
DefineEditText text = new DefineEditText(flash.NewIdentifier(), bounds);
text.FontId = font.FontId;
text.WordWrap = false;
text.Multiline = true;
text.ReadOnly = true;
text.AutoSize = false;
text.NoSelect = false;
text.Bordered = false;
text.HTML = false;
text.UseOutlines = false;
text.FontHeight = 400;
text.TextColorRGB = new RGB(0, 0, 0, 255);
text.Leading = 40;
text.InitialText = "Click Now!";
//add text and DefineShape objects in the container
flash.Add(text);
flash.Add(defineShape);
//define button states
int buttonState = (int)Aspose.Flash.Button.ButtonState.Down | (int)Aspose.Flash.Button.ButtonState.Up |
(int)Aspose.Flash.Button.ButtonState.Over | (int)Aspose.Flash.Button.ButtonState.Active;
//create a button record and attach with the define shape object
ButtonRecord button1 = new ButtonRecord(
buttonState, defineShape.ShapeId, 1,
new MatrixTransform(),
new ColorTransform(1, 1, 1, 0, 0, 0));
//add text to the button record
ButtonRecord button2 = new ButtonRecord(
buttonState, text.ObjectId, 2,
new MatrixTransform(280, -42),
new ColorTransform(1, 1, 1, 0, 0, 0));
ArrayList buttons = new ArrayList();
buttons.Add(button1);
buttons.Add(button2);
//create empty action array - because we're not adding any action to the button yet
actionArray = new ArrayList();
//define button action object
ButtonAction buttonAction = new ButtonAction((int)ButtonActionType.Press, actionArray);
ArrayList buttonActions = new ArrayList();
buttonActions.Add(buttonAction);
//define button using DefineButton2
DefineButton2 startButton =
new DefineButton2(flash.NewIdentifier(), false, buttons, buttonActions);
flash.Add(startButton);
//place button on containerr using PlaceObject2
PlaceObject2 placeObject = new PlaceObject2(startButton.ButtonId, 1);
placeObject.CharacterType = PlaceType.New;
placeObject.MatrixTransform = new MatrixTransform(1190, 2580);
placeObject.Ratio = 3.051804E-05f;
flash.Add(placeObject);
//add show frame object
flash.Add(new ShowFrame());
//save output flash file
flash.Write("AddButton.swf");
[VB.NET]
'create a flash container
Dim flash As New FlashContainer()
''' set frame rate
flash.FrameRate = 24.0f
''' set frame size
flash.FrameSize = New Rect(0, 0, 4000, 4000)
''' define background
Dim bg As New SetBackgroundColor(New RGB(255, 200, 255, 255))
flash.Add(bg)
'define array lists for push and action objects
Dim pushArray As New ArrayList()
Dim actionArray As New ArrayList()
'define fill styles
Dim listShape As New ArrayList()
listShape.Add(New SolidFill(New RGB(204, 204, 204, 255)))
'define line styles
Dim listLine As New ArrayList()
listLine.Add(New SolidLine(20, New RGB(0, 0, 0, 255)))
Dim shape As New Shape()
'define style change record and set fill style and line style
Dim scr As New StyleChangeRecord(2400, 450)
scr.OverlapFillStyle = 1
scr.LineStyle = 1
shape.Add(scr)
'add four straight edges to draw a rectangle
shape.Add(New StraightEdgeRecord(-2400, 0))
shape.Add(New StraightEdgeRecord(0, -450))
shape.Add(New StraightEdgeRecord(2400, 0))
shape.Add(New StraightEdgeRecord(0, 450))
'create DefineShape object and apply line style and fill style
Dim defineShape As New DefineShape(flash.NewIdentifier(), New Rect(-10, -10, 2410, 460), listShape, listLine, shape)
'create a new font
Dim font As New DefineFont2(flash.NewIdentifier(), "Verdana")
font.Small = False
font.Italic = False
font.Bold = False
font.Language = TextLanguage.Latin
font.Leading = 0
font.CodeTable = New ArrayList()
font.GlyphShapeTable = New ArrayList()
flash.Add(font)
'create DefineEditText object to add text
Dim bounds As New Rect(-40, -40, 2000, 596)
Dim text As New DefineEditText(flash.NewIdentifier(), bounds)
text.FontId = font.FontId
text.WordWrap = False
text.Multiline = True
text.ReadOnly = True
text.AutoSize = False
text.NoSelect = False
text.Bordered = False
text.HTML = False
text.UseOutlines = False
text.FontHeight = 400
text.TextColorRGB = New RGB(0, 0, 0, 255)
text.Leading = 40
text.InitialText = "Click Now!"
'add text and DefineShape objects in the container
flash.Add(text)
flash.Add(defineShape)
'define button states
Dim buttonState As Integer = CInt(Fix(Aspose.Flash.Button.ButtonState.Down)) Or CInt(Fix(Aspose.Flash.Button.ButtonState.Up)) Or CInt(Fix(Aspose.Flash.Button.ButtonState.Over)) Or CInt(Fix(Aspose.Flash.Button.ButtonState.Active))
'create a button record and attach with the define shape object
Dim button1 As New ButtonRecord(buttonState, defineShape.ShapeId, 1, New MatrixTransform(), New ColorTransform(1, 1, 1, 0, 0, 0))
'add text to the button record
Dim button2 As New ButtonRecord(buttonState, text.ObjectId, 2, New MatrixTransform(280, -42), New ColorTransform(1, 1, 1, 0, 0, 0))
Dim buttons As New ArrayList()
buttons.Add(button1)
buttons.Add(button2)
'create empty action array - because we're not adding any action to the button yet
actionArray = New ArrayList()
'define button action object
Dim buttonAction As New ButtonAction(CInt(Fix(ButtonActionType.Press)), actionArray)
Dim buttonActions As New ArrayList()
buttonActions.Add(buttonAction)
'define button using DefineButton2
Dim startButton As New DefineButton2(flash.NewIdentifier(), False, buttons, buttonActions)
flash.Add(startButton)
'place button on containerr using PlaceObject2
Dim placeObject As New PlaceObject2(startButton.ButtonId, 1)
placeObject.CharacterType = PlaceType.New
placeObject.MatrixTransform = New MatrixTransform(1190, 2580)
placeObject.Ratio = 3.051804E-05f
flash.Add(placeObject)
'add show frame object
flash.Add(New ShowFrame())
'save output flash file
flash.Write("AddButton.swf")
The following snapshot shows you the output from the above code.