轉(zhuǎn)帖|其它|編輯:郝浩|2011-03-16 11:14:45.000|閱讀 1221 次
概述:由于WPF 本身中不支持COM 組件同時(shí)也無(wú)法加載ActiveX 控件,所以需要借助WinForm 引用ActiveX 控件將Flash 加入其中。首先創(chuàng)建一個(gè)WPF 項(xiàng)目(WpfFlash),將Flash 文件(.swf)加入到項(xiàng)目中,并將Copy to Output Directory 設(shè)置為"Copy always"。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
由于WPF 本身中不支持COM 組件同時(shí)也無(wú)法加載ActiveX 控件,所以需要借助WinForm 引用ActiveX 控件將Flash 加入其中。首先創(chuàng)建一個(gè)WPF 項(xiàng)目(WpfFlash),將Flash 文件(.swf)加入到項(xiàng)目中,并將Copy to Output Directory 設(shè)置為"Copy always"。
在工程中新增一個(gè)Windows Forms Control Library 項(xiàng)目(FlashControlLibrary),利用該控件庫(kù)加載Flash ActiveX。
在FlashControlLibrary 項(xiàng)目工具欄(Toolbox)中點(diǎn)擊鼠標(biāo)右鍵,選擇"Choose Items..."。在COM Components 標(biāo)簽中選擇"Shockwave Flash Object",點(diǎn)擊確定。
此時(shí)在工具欄中已經(jīng)可以看到剛添加的Shockwave Flash Object 控件了。將控件拖入設(shè)計(jì)窗口,調(diào)整好控件尺寸使其滿(mǎn)足Flash 的尺寸大小,對(duì)FlashControlLibrary 項(xiàng)目進(jìn)行編譯,并生成DLL 文件。
返回WpfFlash 項(xiàng)目將上面編譯的AxInterop.ShockwaveFlashObjects.dll 加入References,并添加System.Windows.Forms 和WindowsFormsIntegration,便于WinForm 程序在WPF 中交互使用。
接下來(lái)將通過(guò)兩種方式將Flash 文件加入到WPF,一種側(cè)重于使用XAML 代碼實(shí)現(xiàn),另一種則使用C#。可按各自需要選擇其一。
XAML 方法
打開(kāi)MainWindow.xaml,加入命名空間xmlns:f="clr-namespace:AxShockwaveFlashObjects;assembly=AxInterop.ShockwaveFlashObjects"。在<Grid>中加入WindowsFormsHost 用于調(diào)用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>
打開(kāi)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# 實(shí)現(xiàn)相同的效果,首先將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 方法,主要通過(guò)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;
}
}
}
效果圖
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:{GnieTech}