📄 pptapplication.cs
字号:
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using v6SmokeTest;
using v6SmokeTest.PasswordTest.PlugIn.Global;
namespace v6SmokeTest.PasswordTest.PlugIn
{
/// <summary>
/// PPT应用程序服务类
/// </summary>
public class PPTApplication
{
static private PowerPoint.Presentation m_pPPTPres = null;
static private PowerPoint.Application m_pPPTApp = null;
static private string m_strPassword = String.Empty;
static private CaptureException m_pCapEx = null;
/// <summary>
/// 构造函数
/// </summary>
public PPTApplication()
{
}
/// <summary>
/// 传递关闭异常类对象指针
/// </summary>
static public CaptureException SetCaptureException
{set{m_pCapEx = value;}}
/// <summary>
/// 创建应用程序实例
/// </summary>
/// <param name="showWin">是否显示程序主窗口</param>
/// <returns>返回启动是否成功</returns>
static public bool StartProcess(bool showWin)
{
bool _blResult = false;
Thread _pThread = new Thread(new ThreadStart(CloseExcDlg));
_pThread.Priority = ThreadPriority.Lowest;
_pThread.Start();
try
{
m_pPPTApp = new PowerPoint.ApplicationClass();
if(showWin)
m_pPPTApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
else
m_pPPTApp.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
_blResult = true;
}
catch
{_blResult = false;}
try
{
_pThread.Abort();
_pThread = null;
}
catch{}
return _blResult;
}
/// <summary>
/// 打开文件
/// </summary>
/// <param name="strFilename">打开的文件名</param>
/// <param name="password">打开文件时的密码</param>
/// <returns>返回打开成功与否</returns>
static public bool OpenFile(string strFilename, string password)
{
int _nTryTimes = 0;
bool _blResult = false;
// 因为PPT的API不支持直接传密码,所以只好从界面上输入了[4/27/2005]
m_strPassword = password;
Thread _pThread = new Thread(new ThreadStart(CloseExcDlg));
_pThread.Priority = ThreadPriority.Lowest;
_pThread.Start();
OpenFileTest:
try
{
m_pPPTPres = m_pPPTApp.Presentations.Open(strFilename,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue);
_blResult = true;
}
catch
{
if(_nTryTimes < 5)
{
APIIntegrate.SleepSomeSecond("1000");
_nTryTimes ++;
goto OpenFileTest;
}
else
_blResult = false;
}
try
{
_pThread.Abort();
_pThread = null;
}
catch{}
return _blResult;
}
/// <summary>
/// 处理输入密码对话框等异常对话框线程
/// </summary>
static private void CloseExcDlg()
{
while(1 == 1)
{
IntPtr _nHwnd = APIIntegrate.FindWindow("#32770", "密码");
if(_nHwnd != IntPtr.Zero)
{
APIIntegrate.ActiveWindow(_nHwnd);
APIIntegrate.PasteString(m_strPassword);
APIIntegrate.PressKeys("Enter");
}
APIIntegrate.SleepSomeSecond("500");
m_pCapEx.CloseExcepWin("PasswordTest", String.Empty);
APIIntegrate.SleepSomeSecond("500");
}
}
/// <summary>
/// 转存文件
/// </summary>
/// <param name="targetFilename">转存为的文件名</param>
/// <param name="provider">加密类型的提供者</param>
/// <param name="name">加密类型名</param>
/// <param name="password">密码信息</param>
/// <returns>返回转存是否成功</returns>
static public bool SaveAs(string targetFilename, string provider, string name, PasswordInf password)
{
bool _blResult = false;
int _nTryTimes = 0;
Thread _pThread = new Thread(new ThreadStart(CloseExcDlg));
_pThread.Priority = ThreadPriority.Lowest;
_pThread.Start();
SaveAsFileTest:
try
{
m_pPPTPres.SetPasswordEncryptionOptions(provider, name, password.Length,
password.EncryFilePro);
m_pPPTPres.Password = password.Password;
m_pPPTPres.SaveAs(targetFilename,
PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,
Microsoft.Office.Core.MsoTriState.msoFalse);
_blResult = true;
}
catch
{
if(_nTryTimes < 5)
{
APIIntegrate.SleepSomeSecond("1000");
_nTryTimes ++;
goto SaveAsFileTest;
}
else
_blResult = false;
}
try
{
_pThread.Abort();
_pThread = null;
}
catch{}
if(_blResult && File.Exists(targetFilename))
return true;
else
return false;
}
/// <summary>
/// 杀掉进程
/// </summary>
static public void DestroyProcess()
{
try
{
foreach(Process _pProcess in Process.GetProcessesByName("POWERPNT"))
_pProcess.Kill();
}
catch
{}
}
/// <summary>
/// 关闭文档
/// </summary>
/// <returns>返回关闭成功与否</returns>
static public bool CloseDocument()
{
try
{
m_pPPTPres.Close();
return true;
}
catch
{return false;}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -