BLOG ĐANG TRONG THỜI GIAN PHÁT TRIỂN, MONG SỰ QUAN TÂM CỦA MỌI NGƯỜI DÀNH CHO BLOG MỚI NÀY CỦA HUNG.PRO.VN

[VB] Show Slide Microsoft Power Point in Winform

[VB.NET - C#] Show Slide Microsoft Power Point in Winform, hung qb share show slide microsoft power point
12 min read
Giao diện demo ứng dụng:

Trên form này, bao gồm các chức năng:
  1. Mở slide đầu tiên
  2. Mở slide cuối cùng
  3. Di chuyển slide
  4. Tự động chạy slide
  5. Hide control trình chiếu power point
Video demo ứng dụng:

Đầ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();
            
        }
    }

}

DOWNLOAD SOURCE CODE

Chúc các bạn thành công với thủ thuật này.
Theo: LapTrinhVB

Bạn có thể thích những bài đăng này

  • Playing games can be for fun, but hacking games is also to know your level :3 :)) Today I would like to guide you to create your own hack. For personal use :D I chose Kingdom Crus…
  • Hello everyone, with this "GET IP ADRESSBAR" algorithm, there is a lot of sharing on "INTERNET", but today Hung shares it with everyone, afraid people will forg…
  • Like the previous applications are applications that I wrote a long time ago and stored on the host :D Now looking back, should I make an application for you to test? :v You visit …
  • Hi everyone, there are usually programs that force users to run ADMINISTRATOR permission to access and update windows system information, for example, the program must run ADMINIST…
  • The ProgressBar control visually indicates the progress of a lengthy operation such as calculating a complex result, downloading a large file from the Web etc. The Maximum and Min…
  • This is an application developed by me guys ^^. Also because I have too much free time, I can't wait to find the code, who would have thought I could write this application ^^ …

12 nhận xét

  1. second ago
    có áp dụng đc công việc của c, để nhanh hơn k e? cho c xin cái demo test thử nào?
    1. second ago
      E cập nhật demo phía trên rồi c nha
  2. second ago
    không cần lắm vì nó k áp dụng với cv của c. e phát triển được chương trình trên điện thoại android nữa thì tốt quá. Sẽ có nhiều thứ để nhờ e hơn ^^
  3. second ago
    Cũng hay đó a, nhưng e hiện tại e sữ dụng điện thoại nhiều hơn máy tính nên cần app mobile phù hợp với công việc hơn a ( như c. kiểu bình luận trên, đợi a phát triển chương trình điện thoại tốt ) ^^
    1. second ago
      A cũng đang tìm hiểu học lập trình mobile e ạh, nhưng chưa biết học ntn nên a cũng hơi ngại và thời gian a hiện tại k cho phép anh có nhiều thời gian để tập trung vào việc đó nên cũng hơi chán đó e .
  4. second ago
    Hi adm, mình có một dự án không biết bạn có thể xem qua k?, hiện tại m` đang phát triển một hotel và mình muốn thuê bạn viết cho mình một chương trình máy tính, kiểm soát số tiền theo tháng, và số phòng khách hàng thuê ở theo ngày giờ, theo ngày... để mình tìm có thể thống kê được số tiền theo ngày hoặc theo tháng. (mình đã gửi thông tin cho bạn ở fb, bạn đọc qua và mong bạn phản hồi lại m` sớm nhé.) tks bạn
    1. second ago
      Hi @Ngọc Anh Hotel hiện tại mình chưa thể phát triển chương trình đó cho bạn nha, tại thời gian mình không có nhiều và bị hạn chế về code , m` sẽ giới thiệu bạn với người này để bạn hỏi nhé. Một thời gian nữa mình sẽ cố gắng học thật tốt để phát triển nhiều thứ hơn và nhận code chương trình thuê bạn nhé.
  5. second ago
    Nó đơn giản chỉ là hiển thị trên hình ảnh, như mình thêm hình ảnh vào thôi, k biết có thể làm việc gì khác nữa k nhỉ
    1. second ago
      Có bạn, nó có thể làm trang trình chiếu giới thiệu sản phẩm trên chương trình của mình phát triển đó bạn,
    2. second ago
      Nhưng thêm vào chương trình nhiều ảnh thì sẽ có một lượng ảnh tải về cùng chương trình thì sẽ gây ra dữ liệu hệ thống nhiều? có thể lưu trên database và khi mỡ chương trình lên thì nó sẽ hiển thị k bạn?
    3. second ago
      vấn đề này thì có bạn nhé, nhưng code đó m` chưa tìm hiểu và kết nối csdl đc, nếu m mà kết nối chương trình với csdl thì đa phần chương trình hiện tại do m` viết đã có bản quyền, bắt ng` dùng phải mua bản quyền r` bạn nhé
    4. second ago
      oh. hehe tks bạn nhé. m` sẽ tìm hiểu và tối ưu chương trình này lên phiên bản khác tốt nhất cho người dùng.
Hung Pro Blog @ 2025
Chia sẽ kiến thức và tư duy sáng tạo về nhiều ngôn ngữ lập trình khác nhau. Csharp, Visual Studio, Blogger, HTML, Javascript, CSS..v.v
Developed by Jago Desain