using ComeCapture;
using Common.system;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
using System.Security.RightsManagement;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Threading;
using TStudyDigitalPen.HID;
using XHWK.Model;
namespace XHWK.WKTool
{
public partial class APP : Application
{
#region 全局变量
///
/// 用户信息
///
public static Model_UserInfo UserInfo=null;
///
/// 接口返回消息
///
public static string ServerMsg=string.Empty;
///
/// 用户微课列表模型
///
public static List WKDataList = null;
///
/// 微课模型
///
public static Model_WKData WKData = null;
///
/// 微课视频列表
///
public static List VideoList = null;
///
/// 画板模型
///
public static List PageDrawList = null;
///
/// 录屏工具
///
public static FFMpeg FFmpeg = new FFMpeg();
///
/// 登陆状态 false 未登录 true已登陆
///
public static bool IsLoginType = false;
public static readonly Model_Page pageData = new Model.Model_Page();
///
/// 防止图片地址重复
///
public static long num = 9999;
///
/// 导入图片路径
///
public static string[] Paths = new string[] { };
///
/// 截图图片路径
///
public static string[] JPaths = new string[999];
public static string ImgPath = string.Empty;
///
/// 页码IP
///
public static string[] OutPut = new string[] { };
public static string CameraPosition = string.Empty;
///
/// 数据存放目录
///
public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHMicroLesson\\";
#region 点阵笔
///
/// 点阵笔
///
public static DigitalPenHID digitalPen;
///
/// 笔序号
///
public static string PenSerial;
///
/// 笔状态,是否已连接
///
public static bool PenStatus;
#endregion
#region 页面
///
/// 主页面
///
public static XHMicroLessonSystemWindow W_XHMicroLessonSystemWindow = null;
///
/// 创建微课页面
///
public static CreateAMicroLessonWindow W_CreateAMicroLessonWindow = null;
///
/// 开始录像
///
public static CountdownWindow W_CountdownWindow = null;
///
/// 录像工具栏
///
public static ScreenRecordingToolbarWindow W_ScreenRecordingToolbarWindow = null;
///
/// 登陆
///
public static LoginWindow W_LoginWindow = null;
///
/// 截图
///
public static JieTuWindow W_JieTuWindow = null;
///
/// 批注
///
public static PracticeWindow W_PracticeWindow = null;
///
/// 我的视频
///
public static FileDirectoryWindow fileDirectoryWindow = null;
///
/// 打印
///
public static PrintWindow W_PrintWindow = null;
///
/// 提示窗口
///
public static PromptWindow W_PromptWindow = null;
#endregion
#endregion
[STAThread]
private static void Main()
{
StopSameProcess();
Killffmpeg();
LatticeFileHelper.RunPrintConfig();
try
{
UserInfo = new Model_UserInfo();
WKDataList = new List();
VideoList = new List();
#region 强制以管理员方式运行 修改人:赵耀 修改时间:2020年9月7日
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
W_CreateAMicroLessonWindow = new CreateAMicroLessonWindow();
// 定义Application对象作为整个应用程序入口
Application app = new Application();
app.Run(W_CreateAMicroLessonWindow);
//Application.Run(W_CreateAMicroLessonWindow);
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Assembly.GetExecutingAssembly().Location;
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//退出
//Current.Shutdown();
Environment.Exit(0);
}
#endregion
}
catch (Exception ex)
{
string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
LogHelper.WriteErrLog(ErrMessage, ex);
}
}
#region 杀死已存在的进程
///
/// 结束已运行的程序
///
private static void StopSameProcess()
{
Process current = Process.GetCurrentProcess();
Process[] processList = Process.GetProcesses();
foreach (Process process in processList)
{
if (process.ProcessName.ToUpper() == "星火微课")
{
if (process.Id != current.Id) //忽略当前进程
{
try
{
process.Kill(); //结束进程
}
catch (Exception)
{
}
}
}
}
}
///
/// 杀死正在运行的ffmpeg
///
public static void Killffmpeg()
{
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
foreach (var KillProcess in KillProcessArray)
{
KillProcess.Kill();
}
}
#endregion
///
/// 错误处理
///
///
///
private void MyApp_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
LogHelper.WriteErrLog("未标示错误:" + e.Exception.Message, e.Exception);
MessageBox.Show("程序异常." + Environment.NewLine + e.Exception.Message);
e.Handled = true;
}
#region 数据保存和读取
///
/// 保存微课信息
///
public static void SaveWkData()
{
try
{
WKData.VideoList = VideoList;
if (WKDataList != null)
{
WKDataList.RemoveAll(x => x.WkPath == WKData.WkPath);
}
else
{
WKDataList = new List();
}
WKDataList.Add(WKData);
//string WkDateXmlStr = XmlUtilHelper.Serializer(typeof(Model_WKData), WKData);
string WkDateXmlStr = XmlUtilHelper.XmlSerialize(WKDataList);
string SavePath = FileToolsCommon.GetFileAbsolutePath("/Data/");
FileToolsCommon.CreateDirectory(SavePath);
string SaveName = SavePath + "WkDate.d";
FileToolsCommon.DeleteFile(SaveName);
FileToolsCommon.WriteText(SaveName, WkDateXmlStr);
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【微课数据保存】(SaveWkData)保存失败:" + ex.Message, ex);
}
}
///
/// 读取微课信息
///
public static void ReadWkData(string WkPath)
{
try
{
string SavePath = FileToolsCommon.GetFileAbsolutePath("/Data/");
FileToolsCommon.CreateDirectory(SavePath);
string SaveName = SavePath + "WkDate.d";
//文件若存在则读取
if (FileToolsCommon.IsExistFile(SaveName))
{
string WkDateXmlStr = FileToolsCommon.FileToString(SaveName);
WKDataList = XmlUtilHelper.DESerializer>(WkDateXmlStr);
if (WKDataList.Exists(x => x.WkPath == WkPath))
{
WKData = WKDataList.Find(x => x.WkPath == WkPath);
VideoList = new List();
VideoList = WKData.VideoList;
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【微课数据读取】(ReadWkData)读取失败:" + ex.Message, ex);
}
}
///
/// 保存画板数据
///
public static void SaveDraw()
{
try
{
if (PageDrawList == null)
{
return;
}
if (PageDrawList.Count < 1)
{
return;
}
FileToolsCommon.CreateDirectory(WKData.WkPath);
string SavePath = WKData.WkPath + "PageData.d";
FileToolsCommon.DeleteFile(SavePath);
string PageDataXmlStr = XmlUtilHelper.XmlSerialize(PageDrawList);
FileToolsCommon.WriteText(SavePath, PageDataXmlStr);
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【画板数据保存】(SaveDraw)保存失败:" + ex.Message, ex);
}
}
///
/// 读取文件
///
public static void ReadDrawData()
{
try
{
PageDrawList = new List();
string SavePath = WKData.WkPath + "PageData.d";
if (FileToolsCommon.IsExistFile(SavePath))
{
string PageDataXmlStr = FileToolsCommon.FileToString(SavePath);
PageDrawList = XmlUtilHelper.DESerializer>(PageDataXmlStr);
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【画板数据读取】(ReadDraw)读取失败:" + ex.Message, ex);
}
}
#endregion
#region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
///
/// 内存释放压缩
///
///
protected override void OnStartup(StartupEventArgs e)
{
//private LierdaCracker cracker = new LierdaCracker();
//cracker.Cracker(5000);//间隔时间
//base.OnStartup(e);
new Thread(o =>
{
while (true)
{
try
{
//FreeMemoryHelper.SetDate();
FreeMemoryHelper.ReallocateMemory();
Thread.Sleep(20000);//暂定每20秒释放一次
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【释放内存(MyApp)" + ex.Message, ex);
}
}
}).Start();
}
#endregion 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
}
}