WPF内嵌Flash并交互[翻译]

  代码下载

  内嵌Macromedia Flash PlayerActivex控件小菜一碟,可获得以下益处:

  • 为WPF程序增加功能、图表、动画
  • 扩展Flash程序的功能
  • 使用现有的flash资源

在内嵌之前,需要考虑的有:

  •      Macromedia官方不支持Flash ActiveX控件内嵌进windows程序
  •   需要用户安装Flash ActiveX控件,如果要分发Flash ActiveX控件,请Adobe公司授权
  •       使用ActiveX技术,只运行在Windows操作系统上
  •   使用 6.0r79或以上的版本,就版本不支持。

在WPF中使用Flash ActiveX控件最便捷的方式是建立自己的封装了Flash播放器的WPF控件。由于WPF不直接支持封装(Wrapper),我们借用WindowsFormsHost。按照下面四步即可。

第一步、创建一个WPF用户控件库

我们终极目标是拥有一个WPF用户控件来播放Flash文件(swf/flv),因此,我们首先创建一个用户控件来封装它。以后我们即可直接使用该用户控件。

  1、创建一个新用户控件项目(我的命名为MyBlogUserContorls)

  2、添加一个WPF用户控件

      3、在WPF用户控件中添加Uri属性和Loaded事件

               

1           public Uri Source{get;set;}
2 
3       private void FlashPlayer_Loaded(object sender,RoutedEventArgs e)
4 
5       {
6 
7       }
8 

 

 

第二步、创建一个Windows Froms 用户控件

  WPF不直接支持ActiveX控件,而Windows Forms支持。我们使用Crossbow技术(http://msdn.microsoft.com/en-us/cc527394.aspx)来在WPF中使用Flash播放器。将Flash ActiveX控件加入工具箱,并封装进Windows Forms用户控件。

      1、(.net 标签)添加WindowsFormsIntegration应用

  2、添加加一个Windows Forms用户控件。

  3、在设计模式打开用户控件、确保工具箱可见

  4,右键点击工具箱、选择“Choose Items”选项

      5、当“Choose Toolbox Items”对话框打开,在“COM 组件”选项卡选中 Shockwave Flash Object选项,点击确定。如果未找到 ,确保你的计算机安装Flash ActiveX控件

      6、在工具箱中将出现Shockwave Flash Object工具

      7、拖动Shockwave Flash Object到你的WindowsForms用户控件,调整大小,并且根据需要命名(比如:axShockwaveFlash)

      8、进入Windows Forms 用户控件代码窗口,添加Flash播放器方法。

  

 1  public partial class WFFFlashPlayer : UserControl
 2     {
 3         public WFFFlashPlayer()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         public new int Width //孤陋寡闻,第一次见这么用
 9         {
10             get { return axShockwaveFlash.Width; }
11             set { axShockwaveFlash.Width = value; }
12         }
13 
14         public new int Height
15         {
16             get { return axShockwaveFlash.Height; }
17             set { axShockwaveFlash.Height = value; }
18         }
19 
20         public void LoadMovie(string strPath)
21         {
22             axShockwaveFlash.LoadMovie(0, strPath);
23         }
24 
25         public void Play()
26         {
27             axShockwaveFlash.Play();
28         }
29 
30         public void Stop()
31         {
32             axShockwaveFlash.Stop();
33         }
34     }

 

 

 

完成此步,将获得Windows Forms用户控件,下面将Windows Forms用户空间加入WPF用户控件

 

第三步、在WPF中封装Windows Forms用户控件

1、打开WPF用户控件XAML文件

2、给Grid命名

3、进入WPF用户空间代码视图

4、完善Loaded事件使其托管Windows Forms 的Flash Player,加载题材并播放

 

 1   public partial class FlashPlayer : UserControl
 2     {
 3         public FlashPlayer()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         public Uri Source { getset; }     
 9 
10         private void FlashPlayer_Loaded(object sender, RoutedEventArgs e)
11         {
12             WindowsFormsHost host = new WindowsFormsHost();
13             WFFlashPlayer player = new WFFlashPlayer();
14                         
15             //the Windows Forms Host hosts the Flash Player
16             host.Child = player;
17 
18             //the WPF Grid hosts the Windows Forms Host
19             FlashPlayerGrid.Children.Add(host);
20 
21             //set size
22             player.Width = (int)Width;
23             player.Height = (int)Height;
24 
25             //load & play the movie
26             player.LoadMovie(Source.AbsoluteUri);
27             player.Play();
28         }
29     }

 

 

第四步、使用WPF Flash控件

在新WPF程序中使用我们的控件

1、创建一个新的WPF程序

2、添加我们刚刚生成的用户空间库引用

3、打开XAML文件

4、加入命名空间

5、将WPF Flash Player控件加入你自己窗口,添加Source和调整大小

 

……

 

一切都OK了,祝好运

 

posted @ 2010-08-19 20:44  程序员阿呆  阅读(3697)  评论(15编辑  收藏  举报