加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Program.cs 13.38 KB
一键复制 编辑 原始数据 按行查看 历史
谢进伟 提交于 2019-11-15 21:44 . 初始化
//Copyright (c) 2012 Stefan Moebius (mail@stefanmoebius.de)
//x86 Taget for VS Express described on http://msdn.microsoft.com/en-gb/vstudio/aa718685.aspx. So it should be ok:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using SystemEx.FileIO.Logging;
using System.Net;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.ComponentModel;
namespace xpsviewer
{
static class Program
{
private static LocalXpsViewer localXpsViewer;
private static LoadingWait waitFrm;
private static string fileToLoad = "";
private static string title = "";
private static bool debug = false;
private static bool isProtected = true;
private static bool allowPrint = false;
private static bool allowCopy = false;
private static bool allowSearch = true;
private static bool allowPrevNext = true;
private static bool allowQuitBtn = true;
private static long pipesize = 0;
private static string execProgram = "";
private static string execParam = "";
private static bool toggle = false;
private static Thread thrdWait = null;
private static volatile bool thrdWaitRunning = true;
private static bool isViewerOnlineFile = false;
static private void WaitWindow()
{
try
{
waitFrm = new LoadingWait();
waitFrm.Show();
while (thrdWaitRunning)
{
Application.DoEvents();
Thread.Sleep(1);
}
waitFrm.Close();
}
catch (System.Exception ex)
{
LogFile.Error("Error while running wait thread:\n", ex);
}
}
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
bool showDWMError = false;
bool stopLoading = false;
Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
InitParameters();
fileToLoad = "http://127.0.0.1:8888//wtcps/playOnlineCourseware?netUrl=http://static.rybbaby.com/20191012102133_1b3433d51cbb42b68f3ea919d0b17df7.xps";
if (fileToLoad.StartsWith("http"))
{
fileToLoad = downloadRemoteFile(fileToLoad);
}
try
{
if (fileToLoad == "")
{
Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
dialog.CheckFileExists = true;
dialog.Filter = Lang.translate("XPS documents|*.xps|All Files|*.*", "XPS Dokumente|*.xps|Alle Dateien|*.*", "XPS papiers|*.xps|Tous les fichiers|*.*");
dialog.RestoreDirectory = true;
dialog.AddExtension = true;
dialog.FilterIndex = 0;
dialog.Multiselect = false;
dialog.Title = Lang.translate("Choose an XPS file...", "Wählen Sie eine XPS-Datei aus...", "Choisissez un fichier XPS...");
dialog.ShowDialog();
if (File.Exists(dialog.FileName))
fileToLoad = dialog.FileName;
else
stopLoading = true;
}
}
catch (System.Exception ex)
{
LogFile.Error("Error while opening open file dialog:\n", ex);
}
if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 2) //Windows 8
{
LogFile.Debug("Win 8 detected");
if (Program.IsProtected)
{
if (WndProtect.IsDWMEnabled() == false)
{
LogFile.Debug("Win 8 + no DWM + Protected....");
//Stop running the program under windows 8 without DWM -> no protection
stopLoading = true;
showDWMError = true;
}
}
}
if (stopLoading == false)
{
try
{
thrdWait = new Thread(new ThreadStart(WaitWindow));
thrdWait.SetApartmentState(ApartmentState.STA);
thrdWait.Start();
}
catch (System.Exception ex)
{
LogFile.Error("Error while starting wait thread:\n", ex);
}
localXpsViewer = new LocalXpsViewer();
bool run = localXpsViewer.LoadFileSync();
thrdWaitRunning = false;
try
{
if (title != "")
{
localXpsViewer.Text = title;
}
else
{
if (fileToLoad != "")
localXpsViewer.Text = System.IO.Path.GetFileName(fileToLoad);
}
if (toggle)
localXpsViewer.SetFullscreenFlag();
}
catch (System.Exception ex)
{
LogFile.Error("Error while modifiying main form:\n", ex);
}
if (run)
{
Application.Run(localXpsViewer);
clearTemFile();
}
else
MessageBox.Show(Lang.translate("The document cannot be opened!", "Das Dokument konnte nicht geöffnet werden!", "Le document ne peut pas être ouvert!"), "XPS Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else
{
if (showDWMError)
{
LogFile.Error("Cannot continue, Win8 need DWM!");
MessageBox.Show(Lang.translate("This document cannot be shown when the Desktop Window Manager (DWM) is not active. To continue please activate it.", "Dieses Dokument kann nicht angezeigt werden, wenn der Desktop Window Manager (DWM) nicht aktiv ist. Um fortzufahren aktivieren Sie diesen bitte.", "Ce programme ne pas fonctionner lorsque le Desktop Window Manager (DWM) n'est pas actif. Pour continuer s'il vous plaît activer le Desktop Window Manager."), "XPS Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
}
}
private static string downloadRemoteFile(string url)
{
string filePath = "";
try
{
isViewerOnlineFile = true;
string fileName = System.Guid.NewGuid().ToString();
filePath = Path.GetTempPath() + fileName;
WebClient webclient = new WebClient();
webclient.DownloadFile(url, filePath);
}
catch (Exception ex)
{
MessageBox.Show("加载远程文件失败!", "XPS Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
return filePath;
}
public static void InitParameters()
{
string[] args = Environment.GetCommandLineArgs();
try
{
int i = 1; // First parameter is the executable path!
while (i < args.Length)
{
string arg = args[i].ToUpper();
if (arg.Equals("/FULLSCREEN"))
toggle = true;
else if (arg.Equals("/DEBUG"))
{
LogFile.OpenLog("", "xpsviewer", true, LogLevel.DEBUG);
debug = true;
}
else if (arg.Equals("/PROTECT"))
isProtected = true;
else if (arg.Equals("/NOPRINT"))
allowPrint = false;
else if (arg.Equals("/NOCOPY"))
allowCopy = false;
else if (arg.Equals("/NOSEARCH"))
allowSearch = false;
else if (arg.Equals("/NONAVIGATION"))
allowPrevNext = false;
else if (arg.Equals("/QUITBTN"))
allowQuitBtn = true;
else if (arg.Equals("/EXEC"))
{
if (i < args.Length - 1)
{
execProgram = args[i + 1];
i++;
}
}
else if (arg.Equals("/EXECPARAM"))
{
if (i < args.Length - 1)
{
execParam = args[i + 1];
i++;
}
}
else if (arg.Equals("/SIZE"))
{
if (i < args.Length - 1)
{
try
{
pipesize = Convert.ToInt64(args[i + 1]);
}
catch (System.Exception ex)
{
LogFile.Error("exception wile converting size form string to int:", ex);
}
i++;
}
}
else if (arg.Equals("/TITLE"))
{
if (i < args.Length - 1)
{
title = args[i + 1];
i++;
}
}
else
{
if (fileToLoad.Equals(""))
{
fileToLoad = args[i];
}
}
i++;
}
}
catch (System.Exception ex)
{
LogFile.Error("Error while parsing parameters:\n", ex);
}
}
public static void Show()
{
try
{
if (localXpsViewer != null)
localXpsViewer.Show();
}
catch (System.Exception ex)
{
LogFile.Error("Error in Show():\n", ex);
}
}
public static void Hide()
{
try
{
if (localXpsViewer != null)
localXpsViewer.Hide();
}
catch (System.Exception ex)
{
LogFile.Error("Error in Hide():\n", ex);
}
}
public static void Quit()
{
try
{
if (localXpsViewer != null)
{
localXpsViewer.Close();
}
}
catch (System.Exception ex)
{
LogFile.Error("Error in Quit():\n", ex);
}
}
public static void ToggleFullscreen()
{
try
{
if (localXpsViewer != null)
localXpsViewer.ToggleFullscreen();
}
catch (System.Exception ex)
{
LogFile.Error("Error in ToggleFullscreen():\n", ex);
}
}
public static void CloseWaitWindow()
{
thrdWaitRunning = false;
}
public static void clearTemFile()
{
if (isViewerOnlineFile)
{
try
{
File.Delete(fileToLoad);
}
catch (Exception ex)
{
LogFile.Error("Error in clearTemFile():\n", ex);
}
}
}
public static bool IsFullscreen
{
get { return localXpsViewer.IsFullscreen; }
}
public static string FileToLoad
{
get { return fileToLoad; }
}
public static bool Debug
{
get { return debug; }
}
public static bool IsProtected
{
get { return isProtected; }
}
public static bool AllowPrint
{
get { return allowPrint; }
}
public static bool AllowCopy
{
get { return allowCopy; }
}
public static bool AllowSearch
{
get { return allowSearch; }
}
public static bool AllowPrevNext
{
get { return allowPrevNext; }
}
public static bool AllowQuitBtn
{
get { return allowQuitBtn; }
}
public static long PipeSize
{
get { return pipesize; }
}
public static string ExecParam
{
get { return execParam; }
}
public static string ExecProgram
{
get { return execProgram; }
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化