将Flash 嵌入WPF 程序
? ? ?由于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="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"/> </ 定义FlashLoaded 方法,主要通过WindowsFormsHost和 AxShockwaveFlash 完成Flash 加载操作。using System; using System.Windows; using System.Windows.Forms.Integration; using AxShockwaveFlashObjects; namespace WpfFlash { public partial class 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 = @"game.swf"; axShockwaveFlash.Movie = flashPath; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – Paperclip的content_type根据应用程序无效
- postgresql – 创建一个数组/切片以在Golang中存储数据库查
- [Swift]LeetCode1180. 统计只含单一字母的子串 | Count Su
- C# – 如何调用此技术?
- ajax+div=iframe应用
- ruby – 将元素注入Hash.new([])时<<和=有什么区别?
- c# – 如何使用类作为属性
- ruby-on-rails – Rails 4:没有路由匹配 – 缺少id密钥的关
- SQLite 解决:Could not load file or assembly 'Syste
- 这是C#泛型错误吗?