📄 powerpointcontrol.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
namespace UtilControl
{
public class PowerpointControl
{
private static PowerPoint.Application _application;
private PowerPoint.Presentations presentionSet;
private PowerPoint.Presentation presention;
private int intSilde;
private string FileName;
bool IsSlideShow;
bool IsMax;
private static PowerPoint.Application App
{
get
{
/*
if (_application == null)
{
_application = new PowerPoint.ApplicationClass();
}*/
return (_application);
}
set
{
_application = value;
}
}
private static PowerPoint.SlideShowView ssView
{
get
{
PowerPoint.SlideShowView view = null;
try
{
if (App != null)
{
PowerPoint.Presentation p = App.ActivePresentation;
if (p != null)
{
PowerPoint.SlideShowWindow w = p.SlideShowWindow;
if (w != null)
{
view = w.View;
}
}
}
}
catch
{
}
return (view);
}
set
{
}
}
public PowerpointControl()
{
App = null;
FileName = "";
IsSlideShow = false;
IsMax = false;
}
public void OpenFile(string strPath)
{
IsMax = false;
CloseFile();
ClosePPT();
App = new PowerPoint.ApplicationClass();
presentionSet = App.Presentations;
App.Visible = MsoTriState.msoCTrue;
try
{
presention = presentionSet.Open(strPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoCTrue);
FileName = strPath.Substring(strPath.LastIndexOf('\\') + 1);
App.WindowState = PowerPoint.PpWindowState.ppWindowMaximized;
}
catch
{
}
}
public void CloseFile()
{
try
{
presention.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(presention);
presention = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(presentionSet);
presentionSet = null;
App.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(App);
App = null;
IsSlideShow = false;
}
catch
{
}
}
public void ClosePPT()
{
CloseFile();
IsSlideShow = false;
IsMax = false;
int hwnd = UtilControl.Win32.FindWindow("PP11FrameClass", "Microsoft PowerPoint");
if (hwnd != 0)
{
UtilControl.Win32.SendMessage(hwnd, (int)UtilControl.Win32.WM_SYSCOMMAND, 0x0000F060, 0x022A03B8);
}
}
public void Maximize()
{
try
{
if (IsSlideShow == false)
{
int hwnd = GetHandle();
if (hwnd != 0)
{
UtilControl.Win32.SendMessage(hwnd, (int)Win32.WM_SYSCOMMAND, (int)Win32.SC.MAXIMIZE, 0);
IsMax = true;
}
}
}
catch
{
}
}
public void Minimize()
{
try
{
if (IsSlideShow == false)
{
int hwnd = GetHandle();
if (hwnd != 0)
{
UtilControl.Win32.SendMessage(hwnd, (int)Win32.WM_SYSCOMMAND, (int)Win32.SC.MINIMIZE, 0);
IsMax = false;
}
}
}
catch
{
}
}
public void StartSlideShow()
{
this.StopSlideShow();
try
{
IsSlideShow = true;
App.Activate();
this.Maximize();
//App.WindowState = PowerPoint.PpWindowState.ppWindowMaximized;
App.ActivePresentation.SlideShowSettings.Run();
ssView.GotoSlide(intSilde, MsoTriState.msoFalse);
}
catch
{
}
}
public void StopSlideShow()
{
try
{
if (IsSlideShow == true)
{
IsSlideShow = false;
intSilde = ssView.CurrentShowPosition;
ssView.Exit();
this.Maximize();
//App.WindowState = PowerPoint.PpWindowState.ppWindowMinimized;
}
}
catch
{
}
}
public void CapSilde(string strFileName)
{
presention.SaveAs(strFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoFalse);
}
public int SildeNum()
{
return ssView.CurrentShowPosition;
}
public void NextSlide()
{
Maximize();
try
{
if (ssView != null)
{
SetActivate();
ssView.Next();
}
}
catch
{
}
}
public void PrevSlide()
{
Maximize();
try
{
if (ssView != null)
{
SetActivate();
ssView.Previous();
}
}
catch
{
}
}
public void FirstSlide()
{
Maximize();
try
{
if (ssView != null)
{
SetActivate();
ssView.First();
}
}
catch
{
}
}
public void LastSlide()
{
Maximize();
try
{
if (ssView != null)
{
SetActivate();
ssView.Last();
}
}
catch
{
}
}
private int GetHandle()
{
int hwnd = 0;
string strWindow="";
if (FileName.Equals(""))
{
strWindow = "Microsoft PowerPoint";
return UtilControl.Win32.FindWindow("PP11FrameClass", strWindow);
}
else
{
strWindow = "PowerPoint Slide Show - " + "[" + FileName + "]";
hwnd = UtilControl.Win32.FindWindow("screenClass", strWindow);
if (hwnd == 0)
{
strWindow = "Microsoft PowerPoint - ";
strWindow += "[" + FileName + "]";
return UtilControl.Win32.FindWindow("PP11FrameClass", strWindow);
}
else
{
return hwnd;
}
}
}
public void ToggleMaxMin()
{
if (IsMax == false)
{
this.Maximize();
}
else
{
this.Minimize();
}
}
private void SetActivate()
{
int hwnd = GetHandle();
if (hwnd != Win32.GetForegroundWindow())
{
Win32.SetForegroundWindow(hwnd);
}
}
public void Swap()
{
this.StopSlideShow();
this.Minimize();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -