using Common.system;
using System;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using XHWK.Model;
namespace XHWK.WKTool
{
///
/// 录屏工具栏
///
public partial class ScreenRecordingToolbarWindow : Window
{
#region 初始变量
///
/// 视频保存名称
///
string VideoSavePathName;
///
/// 是否首次录屏
///
bool IsFirstRS = true;
///
/// 是否暂停
///
bool IsSuspend = true;
///
/// 视频信息
///
Model_Video VideoInfo = null;
//声明一个 DrawingAttributes 类型的变量
DrawingAttributes drawingAttributes;
private DispatcherTimer t = null;
///
/// 计时器状态
///
private enum State
{
Start,
Pause,
End
}
///
/// 状态
///
private State _state = State.End;
///
/// 计时用
///
private TimeSpan _timeSpan = new TimeSpan(0, 0, 0, 0, 0);
#endregion
#region 初始化
///
/// 录屏工具栏
///
public ScreenRecordingToolbarWindow()
{
InitializeComponent();
}
///
/// 初始化
///
public void Initialize()
{
//创建 DrawingAttributes 类的一个实例
drawingAttributes = new DrawingAttributes();
//将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
//InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
drawingAttributes.FitToCurve = true;
//隐藏画笔工具栏
//BtnToolbarDown_Click(null, null);
gridToolbar.Visibility = Visibility.Hidden;
gridColour.Visibility = Visibility.Hidden;
gridThickness.Visibility = Visibility.Hidden;
if (t == null)
{
t = new DispatcherTimer();
t.Tick += OnTimer;
t.Interval = new TimeSpan(0, 0, 0, 1);
t.IsEnabled = true;
t.Start();
}
t.Interval = new TimeSpan(0, 0, 0, 1);
Stack();
ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
BtnRecordingScreen_Click(null, null);
}
#endregion
#region 事件
///
/// 时钟回调
///
///
///
private void OnTimer(object sender, EventArgs e)
{
switch (_state)
{
case State.Start:
{
_timeSpan += new TimeSpan(0, 0, 0, 1);
}
break;
case State.Pause:
{
}
break;
case State.End:
{
_timeSpan = new TimeSpan();
//_timeSpan = new TimeSpan(0, 23, 12, 45, 54);
}
break;
}
string time = string.Format("{0:D2}:{1:D2}",/* _timeSpan.Hours,*/ _timeSpan.Minutes, _timeSpan.Seconds);
//char[] times = time.ToCharArray();
txbTime.Text = time;
}
///
/// 开始
///
///
///
private void Stack()
{
_state = State.Start;
}
///
/// 暂停
///
private void TimeOut()
{
_state = State.Pause;
}
///
/// 结束
///
///
///
private void End()
{
_state = State.End;
}
#region 录屏
///
/// 设置录屏文件地址
///
void SetUpVideoPathName()
{
//FileToolsCommon.DeleteDirectory(APP.WKData.WkPath + "temp/");
FileToolsCommon.CreateDirectory(APP.WKData.WkPath);
FileToolsCommon.DeleteDirectory(APP.WKData.WkPath + "temp/");
VideoSavePathName = APP.WKData.WkPath + APP.WKData.WkName + "_录屏." + ((Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"))).ToString();
int num = 1;
while(FileToolsCommon.IsExistFile(VideoSavePathName))
{
VideoSavePathName = APP.WKData.WkPath + APP.WKData.WkName + "_录屏_"+ num + "." + ((Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"))).ToString();
num++;
}
}
///
/// 开始或暂停录制
///
///
///
private void BtnRecordingScreen_Click(object sender, RoutedEventArgs e)
{
if (IsSuspend)
{
Stack();
if (IsFirstRS)
{
VideoInfo = new Model_Video();
VideoInfo.VideoType = (Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"));
VideoInfo.WkType = Enum_WKVidetype.RecordingScreen;
SetUpVideoPathName();
IsFirstRS = false;
}
IsSuspend = false;
//#region 录像倒计时
//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();
//#endregion
ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
try
{
APP.FFmpeg.StartRecordingVideo(VideoSavePathName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
TimeOut();
IsSuspend = true;
ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/microLessonSystem_14.png"));
try
{
APP.FFmpeg.SuspendFFmpeg();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
///
/// 停止录像
///
///
///
private void BtnStopRecordingScreen_Click(object sender, RoutedEventArgs e)
{
IsSuspend = true;
txbTime.Text = "00:00";
End();
ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
if (gridToolbar.Visibility==Visibility.Visible)
{
gridToolbar.Visibility = Visibility.Hidden;
gridColour.Visibility = Visibility.Hidden;
gridThickness.Visibility = Visibility.Hidden;
APP.W_PracticeWindow.Hide();
}
if (APP.W_XHMicroLessonSystemWindow == null)
{
APP.W_XHMicroLessonSystemWindow = new XHMicroLessonSystemWindow();
}
APP.W_XHMicroLessonSystemWindow.Show();
if (!IsFirstRS)
{
try
{
try
{
APP.FFmpeg.StopFFmpeg(VideoSavePathName);
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
}
IsFirstRS = true;
//生成缩略图
string ThumbnailPath = FileToolsCommon.GetDirectoryName(VideoSavePathName) + "ThumbnailPath/";
FileToolsCommon.CreateDirectory(ThumbnailPath);
//缩略图存储位置
string ThumbnailPathName = ThumbnailPath + FileToolsCommon.GetIOFileNameNoExtension(VideoSavePathName) + ".JPG";
new Thread(new ThreadStart(new Action(() =>
{
while (!FileToolsCommon.IsExistFile(VideoSavePathName))
{
Thread.Sleep(100);
}
FileToolsCommon.DeleteFile(ThumbnailPathName);
//VideoInfo.VideoSize = FileToolsCommon.GetFileSizeByMB(VideoSavePathName).ToString() + " MB";
VideoInfo.RSTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
VideoInfo.VideoPath = VideoSavePathName;
VideoInfo.ThumbnailPath = ThumbnailPathName;
APP.FFmpeg.GenerateThumbnails(VideoSavePathName, ThumbnailPathName);
VideoInfo.FileGuid = Guid.NewGuid().ToString();
VideoInfo.IsUpload = false;
VideoInfo.Uploaded = 0;
VideoInfo.Savefolder = APP.UserInfo.Schoolid + "/resource";
APP.VideoList.Add(VideoInfo);
//保存数据
APP.SaveWkData();
}))).Start();
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
}
}
Hide();
}
#endregion
#region 画笔相关
///
/// 画笔工具栏关闭事件
///
///
///
private void BtnToolbarDown_Click(object sender, RoutedEventArgs e)
{
gridToolbar.Visibility = Visibility.Hidden;
gridColour.Visibility = Visibility.Hidden;
gridThickness.Visibility = Visibility.Hidden;
APP.W_PracticeWindow.Hide();
}
///
/// 画笔点击事件
///
///
///
private void BtnBrush_Click(object sender, RoutedEventArgs e)
{
string time = GetTimeStamp();
string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
string imagePath = Path.Combine(tempPath, time + ".jpg");
ImageHelper.GetScreenshot(new System.Drawing.Rectangle(0, 0, 0, 0), imagePath);
if (gridToolbar.Visibility == Visibility.Visible)
{
gridToolbar.Visibility = Visibility.Hidden;
}
else
{
gridToolbar.Visibility = Visibility.Visible;
try
{
if (APP.W_PracticeWindow == null)
{
APP.W_PracticeWindow = new PracticeWindow();
//APP.W_PracticeWindow.Topmost = true;
APP.W_PracticeWindow.Width = pwidth;
APP.W_PracticeWindow.Height = pHeight;
APP.W_PracticeWindow.Left = 0;
APP.W_PracticeWindow.Top = 0;
//practiceWin.Owner = this;
}
APP.W_PracticeWindow.Initialize(imagePath);
APP.W_PracticeWindow.Show();
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex);
}
}
//imgCanvas.Source = new BitmapImage(new Uri(imagePath));
}
///
/// 屏幕宽
///
internal double pwidth = SystemParameters.PrimaryScreenWidth;
///
/// 屏幕高
///
internal double pHeight = SystemParameters.PrimaryScreenHeight;
///
/// 获取时间戳
///
///
public string GetTimeStamp()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
///
/// 画笔粗细事件
///
///
///
private void BtnThickness_Click(object sender, RoutedEventArgs e)
{
gridThickness.Visibility = Visibility.Visible;
gridColour.Visibility = Visibility.Collapsed;
}
///
/// 画笔颜色事件
///
///
///
private void BtnColour_Click(object sender, RoutedEventArgs e)
{
gridColour.Visibility = Visibility.Visible;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 白色
///
///
///
private void BtnWhite_Click(object sender, RoutedEventArgs e)
{
//drawingAttributes.Color = Colors.White;
APP.W_PracticeWindow.White();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 红色
///
///
///
private void BtnRed_Click(object sender, RoutedEventArgs e)
{
//drawingAttributes.Color = Colors.Red;
APP.W_PracticeWindow.Red();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 黄色
///
///
///
private void BtnYellow_Click(object sender, RoutedEventArgs e)
{
//drawingAttributes.Color = Colors.Gold;
APP.W_PracticeWindow.Yellow();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 青色
///
///
///
private void BtnCyanBlue_Click(object sender, RoutedEventArgs e)
{
//drawingAttributes.Color = Colors.LimeGreen;
APP.W_PracticeWindow.CyanBlue();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 灰色
///
///
///
private void BtnGray_Click(object sender, RoutedEventArgs e)
{
//drawingAttributes.Color = Colors.Gray;
APP.W_PracticeWindow.Gray();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 蓝色
///
///
///
private void BtnBlue_Click(object sender, RoutedEventArgs e)
{
//drawingAttributes.Color = Colors.DeepSkyBlue;
APP.W_PracticeWindow.Blue();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 画笔 细
///
///
///
private void BtnFine_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.Fine();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 画笔 中
///
///
///
private void BtnIn_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.In();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 画笔粗
///
///
///
private void BtnCrude_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.Crude();
gridColour.Visibility = Visibility.Collapsed;
gridThickness.Visibility = Visibility.Collapsed;
}
///
/// 橡皮
///
///
///
private void BtnEraser_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.Eraser();
}
///
/// 🖊
///
///
///
private void BtnPen_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.Pen();
}
///
/// ⚪
///
///
///
private void BtnRound_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.Round();
}
///
/// 矩形
///
///
///
private void BtnRectangle_Click(object sender, RoutedEventArgs e)
{
APP.W_PracticeWindow.Rectangle();
}
#endregion
#endregion
}
}