How to play a PPT In the wpf window ., Wpf window playback ppt
Some time ago, I received a requirement (about the content): embed the PPT playback window into our system and use our own system to control the playback of the PPT, on the PPT page, you can manually record the handwritten content.
At the beginning, I was forced to use WPF as a color pen. Later, I found some information on the Internet and finally moved out. Record it here.
Reference: http://www.jb51.net/article/63266.htm (is a c # operation PPT example, very useful)
Let's talk about my own ideas:
1. As shown in the example of the link, opening the PPT is okay.
2. Use WIN32 API to embed the handle of the PPT playback window into my own WPF window.
3. A transparent canvas is built on the outside of the PPT window. (The implementation will not be detailed here. The idea is to overwrite a transparent window in the PPT window .)
Let's talk about my own implementation:
1. Create a Window to hold the PPT playback Window. Name: PptPlayerView.
1 <Window x:Class="Ezy.Blackboard.Views.PptPlayerView" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:Ezy.Blackboard.Views" 7 mc:Ignorable="d" 8 Title="PptPlayerView" Height="300" Width="300" 9 WindowStyle="None" 10 ResizeMode="NoResize" 11 WindowState="Maximized">12 <Grid x:Name="Panel">13 14 </Grid>15 </Window>
2. After the Window is created, open the PPT and open some methods to control the PPT when the Window is opened.
2.1. First add two references to define some attributes:
1 using Microsoft.Office.Core;2 using ppt = Microsoft.Office.Interop.PowerPoint;
1 public ppt.Presentation ObjPrs { get; private set; }2 3 public ppt.SlideShowView OSlideShowView { get; private set; }4 5 public ppt.Application ObjApp { get; private set; }
2.2. constructor:
1 public PptPlayerView () 2 {3 InitializeComponent (); 4 // prevent the continuous opening of multiple PPT programs. 5 if (ObjApp! = Null) {return;} 6 ObjApp = new ppt. Application (); 7}
2. 3. Some PPT operations:
1 /// <summary> 2 /// open PPT 3 /// </summary> 4 /// <param name = "url"> </param> 5 // /<returns> </returns> 6. public ppt. presentation OpenPpt (string url) 7 {8 var objPresSet = ObjApp. presentations; 9 var objPrs = objPresSet. open (url, MsoTriState. msoTrue, MsoTriState. msoTrue, MsoTriState. msoFalse); 10 return objPrs; 11}
42 // <summary> 43 // Next page 44 /// </summary> 45 // <returns> </returns> 46 public int Next () 47 {48 OSlideShowView. next (); 49 var index = OSlideShowView. slide. slideIndex-1; 50 return index; 51} 52 53 // <summary> 54 // Previous Page 55 // </summary> 56 // <returns> </returns> 57 public int Previous () 58 {59 OSlideShowView. previous (); 60 var index = OSlideShowView. slide. slideIndex-1; 61 return index; 62} 63 64 // <summary> 65 // jump to the settings page 66 // </summary> 67 // <param name = "index"> </param> 68 // <returns> </returns> 69 public int GoToSlide (int index) 70 {71 OSlideShowView. gotoSlide (index); 72 return index; 73}
3. Play the PPT and embed it into the PptPlayerView.
3. 1. Reference WIN 32 API first. (The SetParent API is mainly used)
[DllImport("user32.dll")]private static extern IntPtr SetParent(IntPtr childIntPtr, IntPtr parentIntPtr);
. How to play a PPT:
1 /// <summary> 2 // play ppt 3 /// </summary> 4 /// <param name = "objPrs"> </param> 5 public void playPpt (ppt. presentation objPrs) 6 {7 ObjPrs = objPrs; 8 // enter playback mode 9 var objSlides = objPrs. slides; 10 var objSss = objPrs. slideShowSettings; 11 objSss. loopUntilStopped = MsoTriState. msoCTrue; 12 objSss. startingSlide = 1; 13 objSss. endingSlide = objSlides. count; 14 objSss. showType = ppt. ppSlideShowType. ppShowTypeKiosk; 15 var sw = objSss. run (); 16 17 OSlideShowView = objPrs. slideShowWindow. view; 18 var wn = (IntPtr) sw. HWND; 19 20 // embedded Form 21 var fromVisual = (HwndSource) PresentationSource. fromVisual (Panel); 22 if (fromVisual = null) 23 {24 return; 25} 26 var parentHwnd = fromVisual. handle; 27 SetParent (wn, parentHwnd); 28}
3.3. Don't forget to close the PPT when the window is closed.
1 private void PptPlayWindow_OnLoaded(object sender, RoutedEventArgs e) 2 { 3 Closed += OnClosed; 4 } 5 6 private void OnClosed(object sender, EventArgs eventArgs) 7 { 8 try 9 {10 ObjPrs.Close();11 ObjApp.Quit();12 }13 catch14 {15 16 }17 }
Success!
Hope you can use it as needed ~~~~ The first time I wrote a blog, I hope the big guys can make a lot of corrections.