How to play flash in Windows Forms application using C#

Download Source Code

Introduction

In this article we will see how to play a flash (.swf) file in Windows Forms application

Description

We have generally two methods for playing a flash animation in a Windows Forms application:

  1. Using COM component, Shockwave Flash Object
  2. Playing flash inside a WebBrowser control

Method 1:

Step 1:

Create a new Windows Forms Application

Step 2:

Right click anywhere on the Toolbox and select Choose Items

ChooseItems

Step 3:

Check Shockwave Flash Object under COM Components tab of the Choose Toolbox Items window and click OK

Shockwave Flash Object

Step 4:

Drag Shockwave Flash Object on the form and add two Buttons, to Play and Stop flash

ShockwaveFlashObject

Two references are added to the project after adding Shockwave Flash Object, AxShockwaveFlashObjects and ShockwaveFlashObjects. These dlls are required to play flash

Step 5:

Write following to start and stop the flash

public partial class Form1 : Form
{
     public Form1()
     {
          InitializeComponent();
     }

     private void btnStop_Click(object sender, EventArgs e)
     {
          axShockwaveFlash1.Stop();
     }

     private void btnPlay_Click(object sender, EventArgs e)
     {
          axShockwaveFlash1.Movie = @"C:UsersDEEPAKDesktopMyAnimation.swf";
          axShockwaveFlash1.Play();
     }
}

Movie property of ShockwaveFlash Object is used to set the flash file to play and Play method is called to play it. Stop method is called to stop the flash but unfortunately it pauses the flash instead of stopping it. However, we can use follwing code to play and stop the flash by adding ShockwaveFlash Object dynamically

public partial class Form1 : Form
{
     AxShockwaveFlashObjects.AxShockwaveFlash axShockwaveFlash1;

     public Form1()
     {
          InitializeComponent();
     }

     private void btnStop_Click(object sender, EventArgs e)
     {
          axShockwaveFlash1.Dispose();
     }

     private void btnPlay_Click(object sender, EventArgs e)
     {
          axShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();
          axShockwaveFlash1.Dock = DockStyle.Fill;
          panel1.Controls.Add(axShockwaveFlash1);
          axShockwaveFlash1.Movie = @"C:UsersDEEPAKDesktopMyAnimation.swf";
          axShockwaveFlash1.Play();
     }
}

Here, we have declared a ShockwaveFlash Object under class declaration. In the Click event of Play button first we initialize the ShockwaveFlash Object then we set its Dock property to DockStyle.Fill to fill it in the Panel object which we have added to the form to hold the ShockwaveFlash Object. Then the ShockwaveFlash Object is added to the Panel using its Controls.Add method. Finally we set Movie property to the flash file and call Play method to play the flash. To stop the flash we just dispose ShockwaveFlash Object.

Note: To use AxShockwaveFlashObjects.AxShockwaveFlash class you must set reference to AxShockwaveFlashObjects and ShockwaveFlashObjects dlls. You can add it manually from web or you can follow above Step 2 to 4. You can remove the dragged ShockwaveFlash Object after these references are added to your project.

Method 2:

Step 1:

Create a new Windows Forms Application

Step 2:

Add two buttons to the form for playing and stopping the flash

Step 3:

Write following to play and stop the flash using a WebBrowser control

public partial class Form2 : From
{
     WebBrowser webBrowser1;
     public Form2()
     {
          InitializeComponent();
     }

     private void btnPlay_Click(object sender, EventArgs e)
     {
          webBrowser1 = new WebBrowser();
          webBrowser1.Size = new System.Drawing.Size(578, 446);
          webBrowser1.Dock = System.Windows.Forms.DockStyle.Top;
          this.Controls.Add(webBrowser1);
          webBrowser1.Navigate(@"C:UsersDEEPAKDesktopMyAnimation.swf");
     }

     private void btnStop_Click(object sender, EventArgs e)
     {
          webBrowser1.Dispose();
     }
}

Here, first we declare a WebBrowser control and then inside Play button it is initialized and its Size and Dock property is set. Then it is added to the form dynamically using Controls.Add method. Finally Navigate method of the WebBrowser control is called to open the flash file. Navigate method takes path as parameter.

Step 4:

WebBrowser control inherits Internet Explorer. So flash will play inside the WebBrowser control if it is playing in IE. Thus, please ensure that Flash plug-in for IE is installed in your system. It can be downloaded from here http://download.macromedia.com/pub/flashplayer/updaters/11/flashplayer_11_ax_debug.exe

2 thoughts on “How to play flash in Windows Forms application using C#

Leave a Reply

Your email address will not be published. Required fields are marked *