📄 wpsapplication.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>
/// WPS应用程序服务类
/// </summary>
public class WPSApplication
{
static private WPS.Document m_pWPSDocument = null;
static private WPS.Application m_pWPSApp = null;
static private CaptureException m_pCapEx = null;
/// <summary>
/// 构造函数
/// </summary>
public WPSApplication()
{
}
/// <summary>
/// 传递关闭异常类对象指针
/// </summary>
static public CaptureException SetCaptureException
{set{m_pCapEx = value;}}
/// <summary>
/// 创建应用程序实例
/// </summary>
/// <param name="quiet">是否显示程序主窗口</param>
/// <returns>返回启动是否成功</returns>
static public bool StartProcess(bool quiet)
{
bool _blResult = false;
Thread _pThread = new Thread(new ThreadStart(CloseExcDlg));
_pThread.Priority = ThreadPriority.Lowest;
_pThread.Start();
try
{
m_pWPSApp = new WPS.ApplicationClass();
m_pWPSApp.Visible = quiet;
_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;
Thread _pThread = new Thread(new ThreadStart(CloseExcDlg));
_pThread.Priority = ThreadPriority.Lowest;
_pThread.Start();
OpenFileTest:
try
{
m_pWPSDocument = m_pWPSApp.Documents.Open(strFilename,false,false,true,password,string.Empty,
false,string.Empty,string.Empty,0,0,true,false,0,false);
m_pWPSDocument.ActiveWindow.Selection.EndKey(WPS.WpsUnits.wpsStory,
WPS.WpsMovementType.wpsMove);
_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("TInputPasswordDlg", "密码");
if(_nHwnd != IntPtr.Zero)
APIIntegrate.CloseWindow(_nHwnd);
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
{
object _pArg = password.EncryFilePro;
m_pWPSDocument.SetPasswordEncryptionOptions(provider, name, password.Length,
ref _pArg);
_pArg = String.Empty;
m_pWPSDocument.SaveAs(targetFilename, ref _pArg, true, password.Password, false,
String.Empty, false,true, true, true, true, 0, true, true, 0, true);
m_pWPSDocument.ActiveWindow.Selection.EndKey(WPS.WpsUnits.wpsStory, WPS.WpsMovementType.wpsMove);
_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("wps"))
_pProcess.Kill();
}
catch
{}
}
/// <summary>
/// 关闭文档
/// </summary>
/// <returns>返回关闭成功与否</returns>
static public bool CloseDocument()
{
Object[] _pPars = {0, 0, false};
try
{
m_pWPSDocument.Close(ref _pPars[0], ref _pPars[1], ref _pPars[2]);
return true;
}
catch
{return false;}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -