C# and Flash Player

Hi all,

today ill’ show you how to embed a flash file (.swf ) in a Wpf Window.

It’s important to rember that Wpf can’t natively support Flash, so we have to use the old WindowsForm Friends to accomplish this .

I have created a wrapper for the flashControl,cause i need some other optional function for my app, for our purpose it isn’t necessary, you could bypass this part and embed directly the fash control :

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

namespace Wins.WinStore.Totem.Controls.Presentation
{
    public partial class FlashAxControl : UserControl
    {
        public FlashAxControl()
        {
            InitializeComponent();

        }

        public new int Width
        {
            get { return axShockwaveFlash1.Width; }
            set { axShockwaveFlash1.Width = value; }
        }

        public new int Height
        {
            get { return axShockwaveFlash1.Height; }
            set { axShockwaveFlash1.Height = value; }
        }

        public void LoadMovie(string strPath)
        {
            axShockwaveFlash1.LoadMovie(0, strPath);
         }

        public void Play()
        {
            axShockwaveFlash1.Play();
        }

        public void Stop()
        {
            axShockwaveFlash1.Stop();
        }    }
}

the 0 params  passed in the LoadMovie method prevent the swf file to change proportions when stretched .

The control axShockwaveFlash1 is the “core” of the application, so to get it  add a reference to ShokWaveFlash in your Project :

 

Now it’s time for the Wpf part, so thi is the Window XAML :

   
    <Grid Name="grd_Main">
        <WindowsFormsHost Name="host" Background="White"></WindowsFormsHost>
    </Grid>

 

to embed a WindowsForm control we need WindowsFormsHost, remember that Wpf and WindowsForm show objects in a different manner, so a WindowsFormsHost control will be ever the topmost control in your Wpf Window.

And the last one,  behind code for the Window_Loaded event :

 

  private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FlashAxControl player = new FlashAxControl();
            host.Child = player;
            player.Width = (int)this.Width;
            player.Height = (int)this.Height;
            player.LoadMovie(System.Reflection.Assembly.GetExecutingAssembly().Location.Remove(System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\\")) + "\\" + _strFilePath);
            player.Play();
        }

ask if you need …

see you !!

Leave a comment