- 为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事件
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1 public Uri Source{get;set;}
private void FlashPlayer_Loaded(object sender,RoutedEventArgs e)
{
}
第二步、创建一个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播放器方法。
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 public partial class WFFFlashPlayer : UserControl
{
public WFFFlashPlayer()
{
InitializeComponent();
}
public new int Width //孤陋寡闻,第一次见这么用
{
get { return axShockwaveFlash.Width; }
set { axShockwaveFlash.Width = value; }
}
public new int Height
{
get { return axShockwaveFlash.Height; }
set { axShockwaveFlash.Height = value; }
}
public void LoadMovie(string strPath)
{
axShockwaveFlash.LoadMovie(0, strPath);
}
public void Play()
{
axShockwaveFlash.Play();
}
public void Stop()
{
axShockwaveFlash.Stop();
}
}
完成此步,将获得Windows Forms用户控件,下面将Windows Forms用户空间加入WPF用户控件
第三步、在WPF中封装Windows Forms用户控件
1、打开WPF用户控件XAML文件
2、给Grid命名
3、进入WPF用户空间代码视图
4、完善Loaded事件使其托管Windows Forms 的Flash Player,加载题材并播放
•Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 public partial class FlashPlayer : UserControl
{
public FlashPlayer()
{
InitializeComponent();
}
public Uri Source { get; set; }
private void FlashPlayer_Loaded(object sender, RoutedEventArgs e)
{
WindowsFormsHost host = new WindowsFormsHost();
WFFlashPlayer player = new WFFlashPlayer();
//the Windows Forms Host hosts the Flash Player
host.Child = player;
//the WPF Grid hosts the Windows Forms Host
FlashPlayerGrid.Children.Add(host);
//set size
player.Width = (int)Width;
player.Height = (int)Height;
//load & play the movie
player.LoadMovie(Source.AbsoluteUri);
player.Play();
}
}
第四步、使用WPF Flash控件
在新WPF程序中使用我们的控件
1、创建一个新的WPF程序
2、添加我们刚刚生成的用户空间库引用
3、打开XAML文件
4、加入命名空间
5、将WPF Flash Player控件加入你自己窗口,添加Source和调整大小
……
一切都OK了,祝好运