将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





本文转自Gnie博客园博客,原文链接:http://www.cnblogs.com/gnielee/archive/2010/07/27/wpf-flash-activex.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
49
分享
相关文章
在WPF程序中实现PropertyGrid功能
【11月更文挑战第15天】PropertyGrid 是一个用户界面组件,用于直观地查看和编辑对象属性。在 WPF 中可通过组合 Expander 和 DataGrid 实现基本功能,或使用第三方库 PropertyTools 获得更强大特性,包括属性验证和类型特定编辑器。
331 3
|
8月前
|
C#
C# WPF 将第三方DLL嵌入 exe
C# WPF 将第三方DLL嵌入 exe
149 0
|
8月前
|
C#
WPF/C#:程序关闭的三种模式
WPF/C#:程序关闭的三种模式
177 0
|
10月前
|
C#
WPF/C#:程序关闭的三种模式
WPF/C#:程序关闭的三种模式
144 3
WPF防止程序多次运行
WPF防止程序多次运行
279 0
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
在WPF程序中将控件所呈现的内容保存成图像
原文:在WPF程序中将控件所呈现的内容保存成图像 有的时候,我们需要将控件所呈现的内容保存成图像保存下来,例如:InkCanvas的手写墨迹,WebBrowser中的网页等。可能有人会说,这个不就是截图嘛,找到控件的坐标和大小,调用截图API不就可以了嘛。
1166 0
WPF程序中的弱事件模式
原文:WPF程序中的弱事件模式 在C#中,得益于强大的GC机制,使得我们开发程序变得非常简单,很多时候我们只需要管使用,而并不需要关心什么时候释放资源。但是,GC有的时并不是按照我们所期望的方式工作。 例如,我想实现一个在窗口的标题栏中实时显示当前的时间,一个比较常规的做法如下:     var...
1195 0