摘要: 由于WPF 本身中不支持COM 组件同时也无法加载ActiveX 控件,所以需要借助WinForm 引用ActiveX 控件将Flash 加入其中。首先创建一个WPF 项目(WpfFlash),将Flash 文件(.swf)加入到项目中,并将Copy to Output Directory 设置为"Copy always"。
由于WPF 本身中不支持COM 组件同时也无法加载ActiveX 控件,所以需要借助WinForm 引用ActiveX 控件将Flash 加入其中。首先创建一个WPF 项目(WpfFlash),将Flash 文件(.swf)加入到项目中,并将Copy to Output Directory 设置为"Copy always"。
在工程中新增一个Windows Forms Control Library 项目(FlashControlLibrary),利用该控件库加载Flash ActiveX。
在FlashControlLibrary 项目工具栏(Toolbox)中点击鼠标右键,选择"Choose Items..."。在COM Components 标签中选择"Shockwave Flash Object",点击确定。
此时在工具栏中已经可以看到刚添加的Shockwave Flash Object 控件了。将控件拖入设计窗口,调整好控件尺寸使其满足Flash 的尺寸大小,对FlashControlLibrary 项目进行编译,并生成DLL 文件。
返回WpfFlash 项目将上面编译的AxInterop.ShockwaveFlashObjects.dll 加入References,并添加System.Windows.Forms 和WindowsFormsIntegration,便于WinForm 程序在WPF 中交互使用。
接下来将通过两种方式将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="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="//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="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="//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; } } }
效果图
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标常用软件Flash频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号