using Aspose.Words;
using Aspose.Words.Saving;
using Common.system;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace XHWK.WKTool
{
///
/// 主页面
///
public partial class XHMicroLessonSystemWindow : Window
{
#region 初始值
///
/// 文件目录窗口
///
//private CountdownWindow FileDirectoryWindows = null;
private FolderBrowserDialog Ofd;
private DialogResult Result;
public readonly BlackboardNew myblackboard;
private System.Windows.Forms.DialogResult result;
private System.Windows.Forms.OpenFileDialog ofd;
#endregion
#region 初始化
///
/// 主页面
///
public XHMicroLessonSystemWindow()
{
InitializeComponent();
myblackboard = new BlackboardNew(blackboard_canvas);
APP.pageData.pagenum = 1;
APP.pageData.currpage = 1;
DataContext = APP.pageData;
Initialize();
}
///
/// 初始化
///
public void Initialize()
{
//创建 DrawingAttributes 类的一个实例
drawingAttributes = new DrawingAttributes();
//将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
//InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
drawingAttributes.FitToCurve = true;
wfhCamera.Visibility = Visibility.Hidden;
}
#endregion
#region 事件
///
/// 窗体移动
///
///
///
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
///
/// 摄像头打开事件
///
///
///
private void RbnOpen_Click(object sender, RoutedEventArgs e)
{
//UCCamera win = new UCCamera();
////win.Topmost = true;
//win.ShowDialog();
wfhCamera.Visibility = Visibility.Visible;
CameraHelper.IsDisplay = true;
CameraHelper.SourcePlayer = player;
CameraHelper.UpdateCameraDevices();
if (CameraHelper.CameraDevices.Count > 0)
{
CameraHelper.SetCameraDevice(0);
}
}
///
/// 摄像头关闭事件
///
///
///
private void RbnTurnOff_Click(object sender, RoutedEventArgs e)
{
wfhCamera.Visibility = Visibility.Hidden;
CameraHelper.CloseDevice();
}
#endregion
///
/// 录屏事件
///
///
///
private void BtnScreenRecording_Click(object sender, RoutedEventArgs e)
{
if(APP.IsLoginType==false)
{
Login();
return;
}
if (APP.W_CountdownWindow == null)
{
APP.W_CountdownWindow = new CountdownWindow();
//APP.W_CountdownWindow.Topmost = true;
}
else
{
APP.W_CountdownWindow.Initialize();
APP.W_CountdownWindow.Topmost = true;
}
APP.W_CountdownWindow.Show();
if (APP.W_ScreenRecordingToolbarWindow == null)
{
APP.W_ScreenRecordingToolbarWindow = new ScreenRecordingToolbarWindow
{
Topmost = true
};
APP.W_ScreenRecordingToolbarWindow.Initialize();
}
else
{
APP.W_ScreenRecordingToolbarWindow.Initialize();
}
//显示在右下角
//APP.W_ScreenRecordingToolbarWindow.Left= PrimaryScreen.DESKTOP.Width - APP.W_ScreenRecordingToolbarWindow.Width - 10;
//APP.W_ScreenRecordingToolbarWindow.Top= PrimaryScreen.DESKTOP.Height - APP.W_ScreenRecordingToolbarWindow.Height - 60;
this.Hide();
Hide();
}
///
/// 上传事件
///
///
///
private void BtnUpload_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
FileDirectoryWindow fileDirectoryWindow = new FileDirectoryWindow();
fileDirectoryWindow.Show();
}
///
/// 关闭事件
///
///
///
private void BtnDown_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
///
/// 设置 保存事件
///
///
///
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
gridMain.Visibility = Visibility.Visible;
gridSetUp.Visibility = Visibility.Collapsed;
}
///
/// 设置事件
///
///
///
private void BtnSetUp_Click(object sender, RoutedEventArgs e)
{
gridMain.Visibility = Visibility.Collapsed;
gridSetUp.Visibility = Visibility.Visible;
}
///
/// 设置 浏览事件
///
///
///
private void BtnBrowse_Click(object sender, RoutedEventArgs e)
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Ofd = new System.Windows.Forms.FolderBrowserDialog();
Result = Ofd.ShowDialog();
if (Result == System.Windows.Forms.DialogResult.OK)
{
if (Ofd.SelectedPath != "")
{
txbStoragePath.Text = Ofd.SelectedPath;
//string ApplicationData = ZConfig.dataPath + "fileStorageAddress.txt";
//string temp = ofd.SelectedPath;
//System.IO.File.WriteAllText(ApplicationData, temp, Encoding.Default);
}
}
}
///
/// 画笔颜色事件 白色
///
///
///
private void BtnWhite_Click(object sender, RoutedEventArgs e)
{
//创建 DrawingAttributes 类的一个实例
drawingAttributes = new DrawingAttributes();
//将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
//InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
//设置 DrawingAttributes 的 Color 属性设置颜色
drawingAttributes.Color = Colors.White;
}
//声明一个 DrawingAttributes 类型的变量
DrawingAttributes drawingAttributes;
///
/// 画笔颜色事件 红色
///
///
///
private void BtnRed_Click(object sender, RoutedEventArgs e)
{
//设置 DrawingAttributes 的 Color 属性设置颜色
drawingAttributes.Color = Colors.Red;
}
///
/// 画笔颜色事件 灰色
///
///
///
private void BtnGray_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Color = Colors.Gray;
}
///
/// 画笔颜色事件 青色
///
///
///
private void BtnCyanBlue_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Color = Colors.LimeGreen;
}
///
/// 画笔颜色事件 黄色
///
///
///
private void BtnYellow_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Color = Colors.Gold;
}
///
/// 画笔颜色事件 蓝色
///
///
///
private void BtnBlue_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Color = Colors.DeepSkyBlue;
}
///
/// 画笔粗细 细
///
///
///
private void RbnFine_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Width = 1;
drawingAttributes.Height = 1;
}
///
/// 画笔粗细 中
///
///
///
private void RbnIn_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Width = 3;
drawingAttributes.Height = 3;
}
///
/// 画笔粗细 粗
///
///
///
private void RbnCrude_Click(object sender, RoutedEventArgs e)
{
drawingAttributes.Width = 5;
drawingAttributes.Height = 5;
}
///
/// 登陆
///
///
///
private void BtnLoginType_Click(object sender, RoutedEventArgs e)
{
Login();
}
///
/// 登陆
///
private void Login()
{
if (APP.W_LoginWindow == null)
{
APP.W_LoginWindow = new LoginWindow();
}
else
{
APP.W_LoginWindow.Initialize();
}
APP.W_LoginWindow.ShowDialog();
if (APP.IsLoginType)
{
txbLoginType.Text = APP.UserInfo.Username;
}
else
{
txbLoginType.Text = "未登录";
}
}
///
/// 截图事件
///
///
///
private void BtnScreenshot_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
}
///
/// 导入
///
///
///
private void BtnImport_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
try
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
ofd = new System.Windows.Forms.OpenFileDialog
{
Filter = "文档|*.docx;*.doc",
InitialDirectory = desktopPath,
Multiselect = false,
AddExtension = true,
DereferenceLinks = true
};
new Thread(
o =>
{
Thread.Sleep(400);
Dispatcher.Invoke(new Action(() =>
{
openDialog();
}
));
})
{
IsBackground = true
}.Start();
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【导入(BtnImport_Click)" + ex.Message, ex);
}
}
private void openDialog()
{
result = ofd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
if (ofd.FileName != "")
{
string filepath = ofd.FileName;
string path = AppDomain.CurrentDomain.BaseDirectory+ "Temp\\";
APP.Paths = ConvertWordToImage(filepath, path, "", 0, 0, null, 0).ToArray();
if(!string.IsNullOrWhiteSpace(txbCurrpage.Text)&& Convert.ToInt32(txbCurrpage.Text)< APP.Paths.Length)
{
imgCanvas.Source = new BitmapImage(new Uri(APP.Paths[Convert.ToInt32(txbCurrpage.Text) - 1]));
}
else
{
imgCanvas.Source = null;
}
}
}
}
///
/// 录制窗口内容
///
///
///
private void BtnRecord_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
}
///
/// 停止录制窗口内容
///
///
///
private void BtnStop_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
}
///
/// 增加
///
///
///
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
}
///
/// 打印
///
///
///
private void BtnPrint_Click(object sender, RoutedEventArgs e)
{
}
///
/// 我的
///
///
///
private void BtnMyMine_Click(object sender, RoutedEventArgs e)
{
if (APP.IsLoginType == false)
{
Login();
return;
}
}
///
/// 上一页
///
///
///
private void last_button_Click(object sender, RoutedEventArgs e)
{
if (APP.pageData.currpage > 1)
{
APP.pageData.currpage -= 1;
myblackboard.changepage(APP.pageData.currpage - 1);
if(APP.Paths.Length>0)
{
if (!string.IsNullOrWhiteSpace(txbCurrpage.Text) && Convert.ToInt32(txbCurrpage.Text) < APP.Paths.Length&& Convert.ToInt32(txbCurrpage.Text)>1)
{
imgCanvas.Source = new BitmapImage(new Uri(APP.Paths[Convert.ToInt32(txbCurrpage.Text) - 2]));
}
else
{
imgCanvas.Source = null;
}
}
}
}
///
/// 下一页
///
///
///
private void next_btn_Click(object sender, RoutedEventArgs e)
{
if (APP.pageData.currpage < APP.pageData.pagenum)
{
APP.pageData.currpage += 1;
myblackboard.changepage(APP.pageData.currpage - 1);
if (APP.Paths.Length > 0)
{
if (!string.IsNullOrWhiteSpace(txbCurrpage.Text) && Convert.ToInt32(txbCurrpage.Text)+1 < APP.Paths.Length)
{
imgCanvas.Source = new BitmapImage(new Uri(APP.Paths[Convert.ToInt32(txbCurrpage.Text) ]));
}
else
{
imgCanvas.Source = null;
}
}
}
}
///
/// 添加
///
///
///
private void add_button_Click(object sender, RoutedEventArgs e)
{
APP.pageData.pagenum += 1;
APP.pageData.currpage = APP.pageData.pagenum;
myblackboard.changepage(APP.pageData.currpage - 1);
}
///
/// 将Word文档转换为图片的方法(该方法基于第三方DLL),你可以像这样调用该方法: ConvertPDF2Image("F:\\PdfFile.doc", "F:\\",
/// "ImageFile", 1, 20, ImageFormat.Png, 256);
///
/// Word文件路径
/// 图片输出路径,如果为空,默认值为Word所在路径
/// 图片的名字,不需要带扩展名,如果为空,默认值为Word的名称
/// 从PDF文档的第几页开始转换,如果为0,默认值为1
/// 从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数
/// 设置所需图片格式,如果为null,默认格式为PNG
/// 设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024
public List ConvertWordToImage(string wordInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, float resolution)
{
// 返回的图片绝对路径集合
List images = new List();
try
{
// open word file
Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
// validate parameter
if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); }
if (imageOutputPath.Trim().Length == 0) { imageOutputPath = Path.GetDirectoryName(wordInputPath); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (imageName.Trim().Length == 0) { imageName = Path.GetFileNameWithoutExtension(wordInputPath); }
if (startPageNum <= 0) { startPageNum = 1; }
if (endPageNum > doc.PageCount || endPageNum <= 0) { endPageNum = doc.PageCount; }
if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (imageFormat == null) { imageFormat = ImageFormat.Png; }
if (resolution <= 0) { resolution = 128; }
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(GetSaveFormat(imageFormat))
{
Resolution = resolution
};
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
imageSaveOptions.PageIndex = i - 1;
doc.Save(Path.Combine(imageOutputPath, imageName) + "_" + APP.num.ToString() + "." + imageFormat.ToString(), imageSaveOptions);
images.Add(Path.Combine(imageOutputPath, imageName) + "_" + APP.num.ToString() + "." + imageFormat.ToString());
APP.num++;
}
imageSaveOptions = null;
doc = null;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("文档已打开,请关闭后重试!");
LogHelper.WriteErrLog("【课堂考试(ExamWindow)】错误日志:" + ex.Message, ex);
}
return images;
}
private static SaveFormat GetSaveFormat(ImageFormat imageFormat)
{
SaveFormat sf;// = SaveFormat.Unknown;
if (imageFormat.Equals(ImageFormat.Png))
{
sf = SaveFormat.Png;
}
else if (imageFormat.Equals(ImageFormat.Jpeg))
{
sf = SaveFormat.Jpeg;
}
else if (imageFormat.Equals(ImageFormat.Tiff))
{
sf = SaveFormat.Tiff;
}
else if (imageFormat.Equals(ImageFormat.Bmp))
{
sf = SaveFormat.Bmp;
}
else
{
sf = SaveFormat.Unknown;
}
return sf;
}
}
}