Amethyst Tutorials

Back to -Tutorials -Amethyst Tutorials

How to add SWFs to .NET Applications

Drag-drop-and-code Flash and Flex objects in VB and C# programs

It is pretty easy to use Flash objects in your .NET applications, if you know how. Here I want to guide you through the basics of using C# with Flash in Amethyst.

The first thing you should do is create the Flash program you want to use. Here I’ve used Amethyst to design and code a simple game using Flex 4. The game interface has some buttons for moving around the game-world and a text area for displaying information. I’ve compiled this as normal in Amethyst...

Now I want to create the .NET application that will use this game. I right-click the Solution Explorer, and select Add/New Project...

I choose C# Windows Forms Application (which I name CSharpTestApp)...

The end result is that I now have a single solution containing two projects: 1) my Flex game and 2) a .NET C# project...

Next I need some mechanism to allow me to embed Flash objects into my .NET user interface. There are various ways of doing this but I’ll explain the simplest. In the C# designer, right-click the General group of the Toolbox and select Choose Items...

When the dialog appears, switch to the Com Components page. Find the entry for Shockwave Flash Object, check it and click OK...

You will now have a Shockwave Flash component in the General tab of the Toolbox (if you accidentally add it to a different tab, you can just drag and drop it into the General one). To add a Flash object to your .NET application, just drag the component from the Toolbox and onto the form. This will appear as a blank white box...

I rename the control to flashDisplay just by entering the name in the Properties panel...

Now I need to ‘wire up’ the Flash object to the specific SWF that I wish to display here. To do that I need to set the Movie property to the name of the SWF. I could do this using the Properties panel but I want a bit more control over what is going on so, in fact, I do it in code. Here is the full code of my C# program (which adds a bit of error handling and also resizes the SWF):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace CSharpTestApp {
  public partial class Form1 : Form {

     private bool _started;      
     private string _fileName;

     public Form1() {
        InitializeComponent();
        try {
           if( _started ) {
              throw new Exception("already started");
           }
           _fileName = Environment.CurrentDirectory + @"..\..\..\..\adventuregame\bin\Debug\wombat.swf";
           _fileName = Path.GetFullPath(_fileName);
           flashDisplay.Movie = _fileName;
           flashDisplay.Movie = Path.GetFullPath(_fileName);
           flashDisplay.Size = new System.Drawing.Size(400, 300);
           _started = true;
        } catch( Exception ex ) {
           MessageBox.Show(ex.Message);
        }
     }

  }
}

Note that my path to the SWF is a relative one and it is pretty long as it goes up a few levels (..\..\..\..\) and then down again into the folder where my SWF is built while debugging. This is convenient when developing a project but you may want to copy the SWF to a different directory for deployment.

Now I just run my C# application to verify that the SWF is indeed embedded into my Windows Form and that I can play my Flex game inside my .NET application...

While this explains the basics of embedding an SWF into a .NET application, it does not show how you might communicate between the .NET and Flash/Flex programs at runtime. For information on mixed-platform debugging, see: Debug From .NET Into ActionScript.

Bookmark and Share   Keywords:  Amethyst  tutorial
© SapphireSteel Software 2014