Play Flash animation in WPF

Posted by dragongamer on Thu, 16 Dec 2021 08:21:15 +0100

Some teachers asked: can WPF play Flash animation? sure. However, due to Microsoft's "selfishness", WPF itself does not directly support the playback of Flash animation, so it needs to do some "design". The following is an example (embedded WPF window playback).

1. First, build the project in VS2010. After opening VS2010, select "file" - "new" - "project", and select "WPF application", as shown in the following figure. The project name is wpffflashplayer.

Play Flash animation in WPF

2. Select the project solution name, right-click and select Add - new project to open the following window. Select "Windows Forms Control Library", set the name as Windows Forms flashcontrol library, and click "OK".

Play Flash animation in WPF

 3,Select in the toolbox Shockwave Flash Object,If it does not appear in the toolbox, just right-click in the toolbox and select "selection item", figure 1 will appear-99,Select one of them“ Shockwave Flash Object"Then click OK. In this case, the tool box will be added Shockwave Flash Object Control and drag it into the design window.

Play Flash animation in WPF

Figure 1-99

4. Start executing the program and automatically generate the following in the "Bin\Debug" folder of the project "WindowsFormsFlashControlLibrary":

AxInterop.ShockwaveFlashObjects.dll and interop ShockwaveFlashObjects. dll

These two files are the interfaces for the application of Flash Player control in WPF under Windows system. With these two files, others do not need to repeat the above design, and can be used below.

5. Then create a WPF project in Expression Blend 4, such as wpffflashplayer.

First, add references to the following 2 files in the "references" of the project (you can find this file in the folder mentioned above):

AxInterop.ShockwaveFlashObjects.dll

Interop.ShockwaveFlashObjects.dll

And in xaml.cs add namespace before file:

using AxShockwaveFlashObjects;

using ShockwaveFlashObjects;

At this time, the project can use the Flash Player class AxShockwaveFlash to create a player object. The display of this object in the WPF window also needs the help of the interface control WindowsFormsHost. Therefore, the following files need to be added to the "reference" of the project:

System.Windows.Forms.dll(C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319)

WindowsFormsIntegration.dll(C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\WPF)

The addition of the first reference will ensure the normal application of AxShockwaveFlash. When the last reference is added, the WindowsFormsHost control will appear in the "assets" column.

To play a video, you can select from the current disk, you need to use the open file dialog box, and the timer is used in the later program, so you need to add a namespace reference:

using Microsoft.Win32;//for OpenFileDialog

The above is the preparation for the design, which has been completed. The interface design is carried out below.

Find the WindowsFormsHost control in the "assets" column, drag it into the MainWindow window, name it wfh, adjust its size and play the Flash animation appropriately. Put in a button "select SWF file":

//Select SWF file (refer to "select WMV file" above for program annotation)

private void button_Click(object sender, System.Windows.RoutedEventArgs e)

    {

       OpenFileDialog openfile = new OpenFileDialog();

       openfile.Filter = "choice SWF file|*.swf";

       openfile.Title = "choice SWF file";

       if (openfile.ShowDialog() == true)

       {

           if (openfile.FileName != "")

           {

               FlashPlayer.Movie=openfile.FileName;                  

           }

       }

   }

//Play the specified Flash animation when Windows starts

private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)

    {

       // FlashPlayer as a child of wfh

       wfh.Child = FlashPlayer;

       //Get animation file path location, system Environment. The current directory points to the Debug file for the project

       string swff=System.Environment.CurrentDirectory+@"\shufa.swf";

       //Play file as player

       FlashPlayer.Movie=swff;

       //The window title displays the version of Flash Player

       this.Window.Title="WPF Embedded in Flash Player: "+FlashPlayer.ProductVersion;

    }

Topics: C# Windows WPF