将Flash 嵌入WPF 程序

     由于WPF 本身中不支持COM 组件同时也无法加载ActiveX 控件,所以需要借助WinForm 引用ActiveX 控件将Flash 加入其中。首先创建一个WPF 项目(WpfFlash),将Flash 文件(.swf)加入到项目中,并将Copy to Output Directory 设置为"Copy always"。

Copy

     在工程中新增一个Windows Forms Control Library 项目(FlashControlLibrary),利用该控件库加载Flash ActiveX。

New Project

Project

     在FlashControlLibrary 项目工具栏(Toolbox)中点击鼠标右键,选择"Choose Items..."。在COM Components 标签中选择"Shockwave Flash Object",点击确定。

AddCom
     此时在工具栏中已经可以看到刚添加的Shockwave Flash Object 控件了。将控件拖入设计窗口,调整好控件尺寸使其满足Flash 的尺寸大小,对FlashControlLibrary 项目进行编译,并生成DLL 文件。

Object  DLL

     返回WpfFlash 项目将上面编译的AxInterop.ShockwaveFlashObjects.dll 加入References,并添加System.Windows.Forms 和WindowsFormsIntegration,便于WinForm 程序在WPF 中交互使用。

AxInterop

AddRef
     接下来将通过两种方式将Flash 文件加入到WPF,一种侧重于使用XAML 代码实现,另一种则使用C#。可按各自需要选择其一。

XAML 方法

     打开MainWindow.xaml,加入命名空间xmlns:f="clr-namespace:AxShockwaveFlashObjects;assembly=AxInterop.ShockwaveFlashObjects"。在<Grid>中加入WindowsFormsHost 用于调用WinForm 程序,并在其中添加AxShockwaveFlash 控件加载Flash 文件。

<Window x:Class="WpfFlash.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:f="clr-namespace:AxShockwaveFlashObjects;assembly=AxInterop.ShockwaveFlashObjects"
        Title="Crab Shooter" Height="540" Width="655">
    <Grid>
        <WindowsFormsHost>
            <f:AxShockwaveFlash x:Name="flashShow"/>
        </WindowsFormsHost>
    </Grid>
</Window>

打开MainWindow.xaml.cs 将Flash 文件加载到flashShow 控件。

using System;
using System.Windows;

namespace WpfFlash
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string flashPath = Environment.CurrentDirectory;
            flashPath += @"\game.swf";
            flashShow.Movie = flashPath;
        }
    }
}

C# 方法

使用C# 实现相同的效果,首先将XAML 代码按如下方式修改,在Window 中加入Loaded 事件。

<Window x:Class="WpfFlash.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Crab Shooter" Loaded="FlashLoaded" Height="540" Width="655">
    <Grid x:Name="mainGrid"/>
</Window>

定义FlashLoaded 方法,主要通过WindowsFormsHost和 AxShockwaveFlash 完成Flash 加载操作。

using System;
using System.Windows;
using System.Windows.Forms.Integration;
using AxShockwaveFlashObjects;

namespace WpfFlash
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void FlashLoaded(object sender, RoutedEventArgs e)
        {
            WindowsFormsHost formHost = new WindowsFormsHost();

            AxShockwaveFlash axShockwaveFlash = new AxShockwaveFlash();

            formHost.Child = axShockwaveFlash;

            mainGrid.Children.Add(formHost);

            string flashPath = Environment.CurrentDirectory;
            flashPath += @"\game.swf";
            
            axShockwaveFlash.Movie = flashPath;
        }
    }
}

效果图

Game

源代码下载

WpfFlash.zip

posted @ 2010-07-27 13:12  Gnie  阅读(15312)  评论(35编辑  收藏  举报
Copyright © 2010 Gnie