How to Play Flash in Windows Forms Application Using C#

Introduction

In this article we will see how to play a flash (.swf) file in a 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".

play-flash-in-Windows-Forms1.jpg

Step 3

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

play-flash-in-Windows-Forms2.jpg

Step 4

Drag the Shockwave Flash Object onto the form and add two buttons to "Play" and "Stop" the flash.

play-flash-in-Windows-Forms3.jpg

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

Step 5

Write the 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:\Users\DEEPAK\Desktop\MyAnimation.swf";

        axShockwaveFlash1.Play();

    }

}

The "Movie" property of the ShockwaveFlash Object sets the flash file to play and the "Play" method is called to play it. The "Stop" method is called to stop the flash but unfortunately it pauses the flash instead of stopping it. However, we can use the follwing code to play and stop the flash by adding a 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:\Users\DEEPAK\Desktop\MyAnimation.swf";

        axShockwaveFlash1.Play();

    }

}

Here, we have declared a ShockwaveFlash Object under the class declaration. In the Click event of the Play button, first we initialize the ShockwaveFlash Object, then we set its Dock property to DockStyle.Fill to fill it in the Panel object that 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 the Movie property to the flash file and call the Play method to play the flash. To stop the flash we just dispose of the ShockwaveFlash Object.

Note: To use the AxShockwaveFlashObjects.AxShockwaveFlash class you must set a reference to the AxShockwaveFlashObjects and ShockwaveFlashObjects DLLs. You can add it manually from the web or you can use Steps 2 to 4 above. 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

Use the following to play and stop the flash using a WebBrowser control:

public partial class Form2 : Form

{
    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:\Users\DEEPAK\Desktop\MyAnimation.swf");

    }

    private void btnStop_Click(object sender, EventArgs e)

    {
        webBrowser1.Dispose();

    }

}

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

Step 4

The WebBrowser control inherits from Internet Explorer. So Flash will play inside the WebBrowser control if it is playing in IE. Thus, please ensure that the 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

Up Next
    Ebook Download
    View all
    Learn
    View all