Trên form này, bao gồm các chức năng:
- Mở slide đầu tiên
- Mở slide cuối cùng
- Di chuyển slide
- Tự động chạy slide
- Hide control trình chiếu power point
Đầu tiên, các bạn cần import thư viện Microsoft.Office.Interop.PowerPoint từ Nuget
PM> NuGet\Install-Package Microsoft.Office.Interop.PowerPoint -Version 15.0.4420.1018
Source code VB.NET
Imports System.Runtime.InteropServices Imports System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar Imports Microsoft.Office.Core Imports Microsoft.Office.Interop.PowerPoint Imports ppt = Microsoft.Office.Interop.PowerPoint Public Class Form1 'NuGet\Install-Package Microsoft.Office.Interop.PowerPoint -Version 15.0.4420.1018 <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> Private Shared Function FindWindow(ByVal ZeroOnly As IntPtr, ByVal lpWindowName As String) As IntPtr End Function <DllImport("user32.dll", SetLastError:=True)> Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr End Function <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Public Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean End Function Private presentation As ppt.Presentation Private oSlideShowView As Microsoft.Office.Interop.PowerPoint.SlideShowView Private totalSlide As Integer = 0 Private panelHeight As Integer Private hided As Boolean Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load panel_Control.Location = New System.Drawing.Point((panel_Action.Width - panel_Control.Width) \ 2, (panel_Action.Height - panel_Control.Height) \ 2) lblSperator.Text = "" lblSperator.Size = New Size(panel_Action.Width, 2) ToggleButton(False) btnShowHide.BackColor = Color.FromArgb(150, Color.Black) btnShowHide.FlatStyle = FlatStyle.Flat btnShowHide.FlatAppearance.BorderSize = 0 panelHeight = panel_Action.Height hided = False End Sub Private Sub btnBrowser_Click(sender As Object, e As EventArgs) Handles btnBrowser.Click Dim openFileDialog = New OpenFileDialog() openFileDialog.Title = "Chọn file Power Point trình chiếu" openFileDialog.Filter = "PowerPoint Files|*.ppt;*.pptx" If openFileDialog.ShowDialog() = DialogResult.OK Then totalSlide = OpenFilePowerPoint(openFileDialog.FileName, panel_Slide) SetCurrentSlide($"1/{totalSlide}") ToggleButton(True) End If End Sub Private Sub SetCurrentSlide(ByVal message As String) lbl_curentSlide.Text = message End Sub Public Function OpenFilePowerPoint(ByVal FileName As String, ByVal PanelEmbbed As Panel) As Integer 'Trả về tổng số slide Try Dim application As ppt.Application Dim screenClasshWnd As IntPtr = CType(0, IntPtr) Dim x As IntPtr = CType(0, IntPtr) application = New ppt.Application() presentation = application.Presentations.Open(FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse) Dim sst1 As ppt.SlideShowSettings = presentation.SlideShowSettings sst1.LoopUntilStopped = Microsoft.Office.Core.MsoTriState.msoCTrue Dim objSlides As ppt.Slides = presentation.Slides sst1.LoopUntilStopped = MsoTriState.msoTrue sst1.StartingSlide = 1 sst1.EndingSlide = objSlides.Count PanelEmbbed.Dock = DockStyle.Fill sst1.ShowType = ppt.PpSlideShowType.ppShowTypeKiosk Dim sw As ppt.SlideShowWindow = sst1.Run() Dim pptptr As IntPtr = CType(sw.HWND, IntPtr) SetParent(pptptr, PanelEmbbed.Handle) oSlideShowView = presentation.SlideShowWindow.View presentation.SlideShowWindow.Height = (PanelEmbbed.Height - 280) Return sst1.EndingSlide Catch ex As Exception Debug.WriteLine(ex.Message) Return 0 End Try End Function Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click oSlideShowView.First() SetCurrentSlide($"1/{totalSlide}") End Sub Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click oSlideShowView.Previous() SetCurrentSlide($"{oSlideShowView.CurrentShowPosition}/{totalSlide}") End Sub Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click oSlideShowView.Next() SetCurrentSlide($"{oSlideShowView.CurrentShowPosition}/{totalSlide}") End Sub Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click oSlideShowView.Last() SetCurrentSlide($"{oSlideShowView.CurrentShowPosition}/{totalSlide}") End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick oSlideShowView.Next() SetCurrentSlide($"{oSlideShowView.CurrentShowPosition}/{totalSlide}") End Sub Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click If btnStart.Text = "Start" Then btnStart.Text = "Pause" Timer1.Enabled = True Timer1.Start() Else btnStart.Text = "Start" Timer1.Enabled = False Timer1.Stop() End If End Sub Private Sub ToggleButton(ByVal isEnable As Boolean) btnFirst.Enabled = isEnable btnPrevious.Enabled = isEnable btnNext.Enabled = isEnable btnLast.Enabled = isEnable btnStart.Enabled = isEnable lbl_curentSlide.Enabled = isEnable End Sub Private Sub btnShowHide_Click(sender As Object, e As EventArgs) Handles btnShowHide.Click If hided Then btnShowHide.Text = "Hide" Else btnShowHide.Text = "Show" End If Timer2.Start() End Sub Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick If hided Then panel_Action.Height += 2 If panel_Action.Height >= panelHeight Then Timer2.Stop() hided = False Me.Refresh() End If Else panel_Action.Height -= 2 If panel_Action.Height <= 0 Then Timer2.Stop() hided = True Me.Refresh() End If End If End Sub End Class
Source code C#:
using Microsoft.Office.Core; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar; using Button = System.Windows.Forms.Button; using ppt = Microsoft.Office.Interop.PowerPoint; namespace EmbbedPowerPointInWinform { public partial class Form1 : Form { [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern IntPtr FindWindow(IntPtr ZeroOnly, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool SetWindowText(IntPtr hwnd, String lpString); ppt.Presentation presentation; Microsoft.Office.Interop.PowerPoint.SlideShowView oSlideShowView; public Form1() { InitializeComponent(); } public void open(string FileName) { try { ppt.Application application; IntPtr screenClasshWnd = (IntPtr)0; IntPtr x = (IntPtr)0; application = new ppt.Application(); presentation = application.Presentations.Open(FileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse); ppt.SlideShowSettings sst1 = presentation.SlideShowSettings; sst1.LoopUntilStopped = Microsoft.Office.Core.MsoTriState.msoCTrue; ppt.Slides objSlides = presentation.Slides; sst1.LoopUntilStopped = MsoTriState.msoTrue; sst1.StartingSlide = 1; sst1.EndingSlide = objSlides.Count; panel1.Dock = DockStyle.Fill; sst1.ShowType = ppt.PpSlideShowType.ppShowTypeKiosk; ppt.SlideShowWindow sw = sst1.Run(); oSlideShowView = presentation.SlideShowWindow.View; IntPtr pptptr = (IntPtr)sw.HWND; SetParent(pptptr, panel1.Handle); MessageBox.Show(sst1.EndingSlide.ToString()); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { } private void AxWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) { } private void Form1_Load(object sender, EventArgs e) { string FileName = @"D:\Documents\Downloads\2023. Quy chế xe về Tết.pptx"; open(FileName); } private void button1_Click(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { oSlideShowView.Next(); } } }
Chúc các bạn thành công với thủ thuật này.
Theo: LapTrinhVB