Embed flash into the WPF Program

Source: Internet
Author: User

Since WPF does not support COM components and cannot load ActiveX controls, you need to use winform to reference ActiveX controls to add flash. First, create a WPF Project (wpfflash), add the Flash file (.swf) to the project, and set copy to output directory to "copy always ".

Add a Windows Forms control library project (flashcontrollibrary) in the project and use the control library to load Flash ActiveX.

Right-click the flashcontrollibrary project toolbar (Toolbox) and choose "choose items ...". Select "Shockwave Flash Object" in the COM components tab and click "OK.


Now you can see the newly added Shockwave Flash Object Control in the toolbar. Drag the control into the design window, adjust the control size to meet the flash size, compile the flashcontrollibrary project, and generate the DLL file.

Return to the wpfflash project and add the axinterop. shockwaveflashobjects. dll compiled above to references, and add system. Windows. Forms and windowsformsintegration to facilitate winformProgramInteractive use in WPF.


Next, we will add Flash files to WPF in two ways, one focusing on using XAML code implementation, and the other using C #. You can select one of them as needed.

XAML Method

Open mainwindow. XAML and add the namespace xmlns: F = "CLR-namespace: axshockwaveflashobjects; Assembly = axinterop. shockwaveflashobjects ". Add windowsformshost to <grid> to call the winform program and add the axshockwaveflash control to load the flash file.

 <  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  > 

Open mainwindow. XAML. CS and load the Flash file to the flashshow control.

UsingSystem;UsingSystem. windows;NamespaceWpfflash {Public partial classMainwindow:Window{PublicMainwindow () {initializecomponent ();StringFlashpath =Environment. Currentdirectory; flashpath + =@ "\ Game.swf"; Flashshow. Movie = flashpath ;}}}
C # Method

Use C # to achieve the same effect, first setCodeAdd the loaded event to the window as follows.

 <  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"/> </  Window  > 

Defines the flashloaded method. It mainly uses windowsformshost and axshockwaveflash to load the 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 ;}}}

Source code Download

Wpfflash.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.