Introduction
In this article, we will see how we can send data from a Flash movie to the hosting C# application. I'll start by mentioning the requirements of creating this application.
Requirements
You will need the Macromedia Flash plug-in installed in your system, and you will also need Macromedia Flash software if you are required to create the Flash movie. Now we will move to the steps for creating the application.
Inside Macromedia Flash
Sending data to and from a Macromedia Flash movie to any hosting application will require a call to the FSCommand
function. The FSCommand
function takes two optional parameters which are the command
and the parameters
.
In this sample, I created three buttons and three circles inside the Macromedia Flash movie and converted them to symbols (will not dig in to details of creating the Flash movie).
In the on (press)
event of all these six movie clip symbols, I inserted this code which differs according to the object. For example, I inserted the following code in the on(press)
action script event of the blue button:
//
on (press) {
fscommand("Button","Blue");
}
//
and for the green circle, I used this code:
//
on (press) {
fscommand("Circule","Green");
}
//
However, you can use the FSCommand
at any event inside the Flash movie, or even in the frame actions. To sum it up, you can use the FSCommand
where you can write an action script :).
Inside VS.NET
As mentioned before, you should have the Macromedia Flash Player (the OCX files registered in your system). So, now create a new C# Windows Forms project, and right click on the Toolbox while you are in the design view of the main form. Choose:
Choose Add/Remove Items, and you will get a window. Choose the COM Components tab and then check Shockwave Flash Object:
Now you will find the Shockwave Flash object added to your tool box. Drag and drop it in your form and resize it to match your Flash movie size. To choose the Flash movie, set the property Movie
, and to choose whether to have it embedded inside your application or to reload it externally each time you run the application, set the property EmbedMovie
. I prefer that you choose to set it to true
, because not embedding it will require that you preserve the original path of the movie all the time (you may just put it next to the exe).
Now we are a step ahead to our destination. We now need to collect the data sent from the Flash movie. Select the Flash movie inside the form, and then in the property pane choose the events. You will find the event FSCommand
, double click on it to create the needed event handler.
When the Flash movie sends its data via the FSCommand
function, the C# program collects this data inside the AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent
object which is defined in the event handler parameter e
. So, to collect the sent command
from the FSCommand
Flash function, you will just need to use the e.command
, and for the parameters
, use e.args
. However, before we write anything inside this function, drag a Label
to the form and resize it to an appropriate size and name it lbl_Result
. Now, moving back to the event handler of the FSCommand
event, we want to tell the C# form which button or circle was clicked. So, we will need to add this code:
lbl_Result.Text="The "+e.args.ToString()+" "+e.command.ToString()+" was clicked";
and finally you will get the information that you clicked on the blue circle, for example:
You can use this simple interaction for more complex data transfer. I hope it was useful.;)
Using the code
There is almost no code in this project :D, it's simple and small. You will just need to follow the above step by step explanation.
Agile Evangelist, Change Embracer and Success Facilitator. Project Manager and IT professional with 8+ years of leadership experience - http://www.hossamaldin.com