using Common.system; using NReco.VideoConverter; using System; using System.Drawing; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Interop; using System.Windows.Threading; using XHWK.Model; using XHWK.WKTool.Utils; namespace XHWK.WKTool { /// /// 录屏工具栏 /// public partial class ScreenRecordingToolbarWindow { #region 初始变量 /// /// 视频保存名称 /// private string _videoSavePathName; /// /// 临时视频路径 /// private string _tempVideoPathName; /// /// 临时麦克风路径 /// private string _tempAudioPathName1; /// /// 临时扬声器路径 /// private string _tempAudioPathName2; private readonly string _temppath = App.WKData.WkPath + "_temppath/"; /// /// 视频信息 /// private Model_Video _videoInfo; //声明一个 DrawingAttributes 类型的变量 private DispatcherTimer _t; /// /// 计时器状态 /// private enum State { Start, Pause, End, Loading } /// /// 状态 /// private State _state = State.End; private KeyboardHookCommon _kHook; /// /// 🖊状态 0红色 1蓝色 10红色批注内 11蓝色批注内 /// public int flg; #endregion 初始变量 #region 初始化 /// /// 录屏工具栏 /// public ScreenRecordingToolbarWindow() { InitializeComponent(); ResizeMode = ResizeMode.NoResize; } /// /// 初始化 /// public void Initialize() { GridSrToobar.Visibility = Visibility.Visible; if (App.W_PracticeWindow == null) { App.W_PracticeWindow = new PracticeWindow { //APP.W_PracticeWindow.Topmost = true; Width = pwidth, Height = pHeight, Left = 0, Top = 0 }; //practiceWin.Owner = this; } App.W_PracticeWindow.InitTqlpPen(); App.W_PracticeWindow.Show(); new Thread ( () => { Thread.Sleep(100); Dispatcher.Invoke(() => { Topmost = true; }); } ).Start(); App.W_PracticeWindow.Hide(); flg = 0; _kHook = new KeyboardHookCommon(); _kHook.KeyDownEvent += K_hook_KeyDownEvent; //创建 DrawingAttributes 类的一个实例 //隐藏画笔工具栏 BtnRecordingScreen.Visibility = Visibility.Visible; BtnRecordingScreenPause.Visibility = Visibility.Collapsed; BtnStopRecordingScreen.IsEnabled = false; //停止录制按钮不点击 BtnPenBlue.IsEnabled = false; //蓝笔不可点击 BtnPenRed.IsEnabled = false; //红笔不可点击 BtnPenBlue.Visibility = Visibility.Visible; BtnPenRed.Visibility = Visibility.Visible; BtnPenBlue_CL.Visibility = Visibility.Collapsed; BtnPenRed_CL.Visibility = Visibility.Collapsed; TxbTime.Content = "00:00:00"; } private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyValue == (int)System.Windows.Forms.Keys.F5 && (int)Control.ModifierKeys == (int)Keys.Control) { //开始,暂停 BtnRecordingScreen_Click(null, null); } if (e.KeyValue == (int)Keys.F6 && (int)Control.ModifierKeys == (int)Keys.Control) { //结束 BtnStopRecordingScreen_Click(null, null); } } #endregion 初始化 #region 事件 private DateTime _srTime = Convert.ToDateTime("2020-01-01 00:00:00"); /// /// 时钟回调 /// /// /// private void OnTimer(object sender, EventArgs e) { if (_state == State.Start) { _srTime = _srTime.AddMilliseconds(1000); } TxbTime.Content = _srTime.ToString("HH:mm:ss"); } /// /// 结束 /// private void End() { _state = State.End; } #region 录屏 /// /// 设置录屏文件地址 /// private void SetSavePath() { string fileType = ((Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"))).ToString(); FileToolsCommon.CreateDirectory(App.WKData.WkPath); FileToolsCommon.DeleteDirectory(_temppath); FileToolsCommon.CreateDirectory(_temppath); _videoSavePathName = App.WKData.WkPath + App.WKData.WkName + "_录屏." + fileType; int num = 1; while (FileToolsCommon.IsExistFile(_videoSavePathName)) { num++; _videoSavePathName = App.WKData.WkPath + App.WKData.WkName + "_录屏_" + num + "." + fileType; } _tempVideoPathName = _temppath + "_录屏_" + num + ".avi"; _tempAudioPathName1 = _temppath + "_麦克风_" + num + "_1.mp3"; _tempAudioPathName2 = _temppath + "_扬声器_" + num + "_2.mp3"; } /// /// 是否已经按下按钮 /// private bool _isPressButton; //录制麦克风的声音 private ZAudioRecordHelper _helper1; //录制扬声器的声音 private ZAudioRecordHelper _helper2; //桌面录制 private ZVideoRecordHelper _helper0; public IntPtr winHandle; // 当前窗体指针 /// /// 开始或暂停录制 /// /// /// private async void BtnRecordingScreen_Click(object sender, RoutedEventArgs e) { #region 防止连击 if (_isPressButton) { return; } _isPressButton = true; new Thread ( () => { Thread.Sleep(500); _isPressButton = false; } ).Start(); #endregion 防止连击 if (_state == State.End) { _srTime = Convert.ToDateTime("2020-01-01 00:00:00"); TxbTime.Content = "00:00"; if (_t == null) { _t = new DispatcherTimer(); _t.Tick += OnTimer; _t.Interval = new TimeSpan ( 0, 0, 0, 0, 1000 ); _t.IsEnabled = true; _t.Start(); } else { _t.Interval = new TimeSpan ( 0, 0, 0, 0, 1000 ); } _state = State.Loading; _videoInfo = new Model_Video { VideoType = (Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType")), WkType = Enum_WKVidetype.RecordingScreen }; SetSavePath(); if (App.W_CountdownWindow == null) { App.W_CountdownWindow = new CountdownWindow(); } App.W_CountdownWindow.InitAndShow(true); BtnRecordingScreen.Visibility = Visibility.Collapsed; BtnRecordingScreenPause.Visibility = Visibility.Visible; BtnStopRecordingScreen.IsEnabled = true; //停止录制按钮可点击 BtnPenBlue.IsEnabled = true; //蓝笔可点击 BtnPenRed.IsEnabled = true; //红笔可点击 BtnPenBlue.Visibility = Visibility.Visible; BtnPenRed.Visibility = Visibility.Visible; BtnPenBlue_CL.Visibility = Visibility.Collapsed; BtnPenRed_CL.Visibility = Visibility.Collapsed; TxbTime.Visibility = Visibility.Visible; //时间显示 #region 隐藏工具栏 if (App.IsHideSRTool) { if (App.W_MinToolbar == null) { App.W_MinToolbar = new MinToolbar(); } App.W_MinToolbar.Topmost = true; double minToolTop = Top; GridSrToobar.Visibility = Visibility.Hidden; App.W_MinToolbar.Initialize(minToolTop, ActualHeight); App.W_MinToolbar.Show(); } #endregion 隐藏工具栏 try { // 视频 winHandle = new WindowInteropHelper(this).Handle; var curScreen = Screen.FromHandle(winHandle); int recordWidth = curScreen.Bounds.Width; int recordHeight = curScreen.Bounds.Height; _helper0 = new ZVideoRecordHelper ( _tempVideoPathName, recordWidth, recordHeight ); // 麦克风 _helper1 = new ZAudioRecordHelper(_tempAudioPathName1, ZAudioRecordHelper.RecordType.microphone); // 扬声器 _helper2 = new ZAudioRecordHelper(_tempAudioPathName2, ZAudioRecordHelper.RecordType.loudspeaker); await Task.Delay(3000); // App.W_CountdownWindow.Hide(); await Task.Delay(100); _state = State.Start; _helper1.StartRecordAudio(); _helper2.StartRecordAudio(); _helper0.StartRecordVideo(); _kHook.Start(); //安装键盘钩子 } catch (Exception) { ExitByErr(); } Console.WriteLine(@"TempVideoPathName:" + _tempVideoPathName); Console.WriteLine(@"TempAudioPathName1:" + _tempAudioPathName1); Console.WriteLine(@"TempAudioPathName2:" + _tempAudioPathName2); } else if (_state == State.Start) { _state = State.Pause; _helper1.PauseRecordAudio(); _helper2.PauseRecordAudio(); _helper0.PauseRecordVideo(); BtnRecordingScreen.Visibility = Visibility.Visible; BtnRecordingScreenPause.Visibility = Visibility.Collapsed; } else if (_state == State.Pause) { _state = State.Start; _helper1.ResumeRecordAudio(); _helper2.ResumeRecordAudio(); _helper0.ResumeRecordVideo(); BtnRecordingScreen.Visibility = Visibility.Collapsed; BtnRecordingScreenPause.Visibility = Visibility.Visible; } } private void ExitByErr() { BtnRecordingScreen.ToolTip = "开始"; _kHook.Stop(); TxbTime.Content = "00:00"; if (App.W_PracticeWindow != null) { if (App.W_PracticeWindow.Visibility == Visibility.Visible) { Owner = null; App.W_PracticeWindow.ReturnPractice(); } } if (App.W_XHMicroLessonSystemWindow == null) { App.W_XHMicroLessonSystemWindow = new MainWindow(); } App.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent(); App.W_XHMicroLessonSystemWindow.InitTqlpPen(); App.W_XHMicroLessonSystemWindow.Show(); End(); if (_t != null) { _t.Stop(); _t = null; } _helper0.StopRecordVideo(); _helper1.StopRecordAudio(); _helper2.StopRecordAudio(); FileToolsCommon.DeleteDirectory(_temppath); if (ClickStopRecordingScreen != null) { ClickStopRecordingScreen(); } Hide(); } /// /// 停止录像 /// /// /// private void BtnStopRecordingScreen_Click(object sender, RoutedEventArgs e) { #region 防止连击 if (_isPressButton) { return; } else { _isPressButton = true; new Thread ( () => { Thread.Sleep(500); _isPressButton = false; } ).Start(); } #endregion 防止连击 BtnRecordingScreen.ToolTip = "开始"; _kHook.Stop(); TxbTime.Content = "00:00"; if (App.W_PracticeWindow != null) { if (App.W_PracticeWindow.Visibility == Visibility.Visible) { Owner = null; App.W_PracticeWindow.ReturnPractice(); } } if (App.W_XHMicroLessonSystemWindow == null) { App.W_XHMicroLessonSystemWindow = new MainWindow(); } App.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent(); App.W_XHMicroLessonSystemWindow.InitTqlpPen(); App.W_XHMicroLessonSystemWindow.Show(); if (_state == State.Pause || _state == State.Start) { End(); _t.Stop(); _t = null; Console.WriteLine(@"停止录制"); _helper0.StopRecordVideo(); _helper1.StopRecordAudio(); _helper2.StopRecordAudio(); new Thread ( () => { FFMpegConverter ffMpeg = new FFMpegConverter(); FFMpegInput[] input = new FFMpegInput[] { new FFMpegInput(_tempVideoPathName), new FFMpegInput(_tempAudioPathName1), new FFMpegInput(_tempAudioPathName2), }; try { // 多路音频混音 ffMpeg.ConvertMedia ( input, _videoSavePathName, "mp4", new OutputSettings() { CustomOutputArgs = "-filter_complex amix=inputs=2:duration=first:dropout_transition=2" } ); //生成缩略图 string thumbnailPath = FileToolsCommon.GetDirectoryName(_videoSavePathName) + "ThumbnailPath/"; FileToolsCommon.CreateDirectory(thumbnailPath); //缩略图存储位置 string thumbnailPathName = thumbnailPath + FileToolsCommon.GetIOFileName(_videoSavePathName).Replace(".", "") + ".JPG"; _videoInfo.RSTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); _videoInfo.VideoPath = _videoSavePathName; _videoInfo.ThumbnailPath = thumbnailPathName; ZVideoRecordHelper.GetVideoPic(_tempVideoPathName, thumbnailPathName); _videoInfo.FileGuid = Guid.NewGuid().ToString(); _videoInfo.IsUpload = false; _videoInfo.Uploaded = 0; if (!App.VideoList.Exists(x => x.FileGuid == _videoInfo.FileGuid) || !App.VideoList.Exists(x => x.VideoPath == _videoInfo.VideoPath)) { App.VideoList.Add(_videoInfo); //保存数据 App.SaveWkData(); } } catch (Exception ex) { LogHelper.WriteErrLog("录屏保存" + ex.Message, ex); } FileToolsCommon.DeleteDirectory(_temppath); } ).Start(); } if (ClickStopRecordingScreen != null) { ClickStopRecordingScreen(); } Hide(); _srTime = Convert.ToDateTime("2020-01-01 00:00:00"); TxbTime.Content = "00:00"; } public void MaxToobar() { Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); float dip = graphics.DpiX; //float DIPY = graphics.DpiY; App.W_ScreenRecordingToolbarWindow.Left = PrimaryScreen.WorkingArea.Width / (dip / 96) - App.W_ScreenRecordingToolbarWindow.Width; GridSrToobar.Visibility = Visibility.Visible; App.W_ScreenRecordingToolbarWindow.Topmost = true; } #endregion 录屏 #region 画笔相关 /// /// 笔调用批注 /// public void PenPractice() { if (flg != 11 && flg != 10) { #region 防止连击 if (_isPressButton) { return; } else { _isPressButton = true; new Thread ( () => { Thread.Sleep(500); _isPressButton = false; } ).Start(); } #endregion 防止连击 GetTimeStamp(); try { Dispatcher.Invoke ( () => { if (App.W_PracticeWindow == null) { App.W_PracticeWindow = new PracticeWindow { Width = pwidth, Height = pHeight, Left = 0, Top = 0 }; } App.W_PracticeWindow.Initialize(); // imagePath); flg = 11; App.W_PracticeWindow.Blue(); App.W_PracticeWindow.Show(); App.W_PracticeWindow.Topmost = true; App.W_PracticeWindow.Focus(); } ); new Thread ( () => { Dispatcher.Invoke(() => { App.W_PracticeWindow.Topmost = false; }); Thread.Sleep(500); Dispatcher.Invoke(() => { Topmost = true; }); } ).Start(); } catch (Exception ex) { LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex); } } } /// /// 画笔点击事件 /// /// /// private void BtnBrush_Click(object sender, RoutedEventArgs e) { if (BtnPenBlue.Visibility != Visibility.Visible) { return; } #region 防止连击 if (_isPressButton) { return; } else { _isPressButton = true; new Thread ( () => { Thread.Sleep(500); _isPressButton = false; } ).Start(); } #endregion 防止连击 BtnPenBlue.Visibility = Visibility.Collapsed; BtnPenRed.Visibility = Visibility.Visible; BtnPenBlue_CL.Visibility = Visibility.Visible; BtnPenRed_CL.Visibility = Visibility.Collapsed; GetTimeStamp(); try { if (flg == 11) { //Dispatcher.Invoke(() => //{ // borOne.Background = new SolidColorBrush(Colors.DodgerBlue); // borTwo.Background = new SolidColorBrush(Colors.DodgerBlue); //}); flg = 1; //this.Owner = null; App.W_PracticeWindow.ReturnPractice(); } else if (flg == 10) { //Dispatcher.Invoke(() => //{ // borOne.Background = new SolidColorBrush(Colors.LightSkyBlue); // borTwo.Background = new SolidColorBrush(Colors.DodgerBlue); //}); App.W_PracticeWindow.Blue(); flg = 11; } else { //new Thread(new ThreadStart(new Action(() => //{ //Dispatcher.Invoke(() => //{ // borOne.Background = new SolidColorBrush(Colors.LightSkyBlue); // borTwo.Background = new SolidColorBrush(Colors.DodgerBlue); //}); //}))).Start(); if (App.W_PracticeWindow == null) { App.W_PracticeWindow = new PracticeWindow { //APP.W_PracticeWindow.Topmost = true; Width = pwidth, Height = pHeight, Left = 0, Top = 0 }; //practiceWin.Owner = this; } App.W_PracticeWindow.InitTqlpPen(); App.W_PracticeWindow.Initialize(); // imagePath); flg = 11; App.W_PracticeWindow.Blue(); App.W_PracticeWindow.Show(); new Thread ( () => { Thread.Sleep(100); Dispatcher.Invoke ( () => { //Owner = APP.W_PracticeWindow; Topmost = true; } ); } ).Start(); } } 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(); } #endregion 画笔相关 #endregion 事件 /// /// 停止录屏 /// public delegate void StopRecordingScreen(); /// /// 停止录屏 /// public event StopRecordingScreen ClickStopRecordingScreen; /// /// 红笔 /// /// /// private void BtnBlackPenTwo_Click(object sender, RoutedEventArgs e) { if (BtnPenRed.Visibility != Visibility.Visible) { return; } #region 防止连击 if (_isPressButton) { return; } else { _isPressButton = true; new Thread ( () => { Thread.Sleep(500); _isPressButton = false; } ).Start(); } #endregion 防止连击 BtnPenBlue.Visibility = Visibility.Visible; BtnPenRed.Visibility = Visibility.Collapsed; BtnPenBlue_CL.Visibility = Visibility.Collapsed; BtnPenRed_CL.Visibility = Visibility.Visible; string time = GetTimeStamp(); #region 录屏批注取消画笔 string imagePath = Path.Combine(_temppath, time + ".jpg"); ImageHelper.GetScreenshot ( new Rectangle ( 0, 0, 0, 0 ), imagePath ); #endregion 录屏批注取消画笔 try { if (flg == 10) { flg = 0; App.W_PracticeWindow.ReturnPractice(); } else if (flg == 11) { flg = 10; App.W_PracticeWindow.Red(); } else { if (App.W_PracticeWindow == null) { App.W_PracticeWindow = new PracticeWindow { Width = pwidth, Height = pHeight, Left = 0, Top = 0 }; } App.W_PracticeWindow.InitTqlpPen(); App.W_PracticeWindow.Initialize(); //imagePath); flg = 10; App.W_PracticeWindow.Red(); App.W_PracticeWindow.Show(); new Thread ( () => { Thread.Sleep(100); Dispatcher.Invoke ( () => { //Owner = APP.W_PracticeWindow; Topmost = true; } ); } ).Start(); } } catch (Exception ex) { LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex); } } /// /// 返回主界面 /// /// /// private void BtnReturn_Click(object sender, RoutedEventArgs e) { if (_state == State.Pause || _state == State.Start) { MessageBoxResult br = MessageWindow.Show ( "退出将结束并保存录制,是否继续?", "提示", MessageBoxButton.OKCancel ); if (br == MessageBoxResult.Cancel) { return; } BtnStopRecordingScreen_Click(null, null); } App.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent(); //APP.W_XHMicroLessonSystemWindow.InitPen(); App.W_XHMicroLessonSystemWindow.InitTqlpPen(); App.W_XHMicroLessonSystemWindow.Show(); new Thread ( () => { Thread.Sleep(700); Dispatcher.Invoke ( () => { if (App.W_MinToolbar != null) { App.W_MinToolbar.Hide(); } } ); } ).Start(); Hide(); } /// /// 修改笔状态 /// public void ModifyState() { BtnPenBlue.Visibility = Visibility.Visible; BtnPenRed.Visibility = Visibility.Visible; BtnPenBlue_CL.Visibility = Visibility.Collapsed; BtnPenRed_CL.Visibility = Visibility.Collapsed; } private void gridToobarTwo_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) { //if (BtnRecordingScreen.ToolTip.ToString().Equals("暂停") && (gridToobar.Visibility == Visibility.Visible || BorderBlack.Visibility == Visibility.Visible)) if (BtnRecordingScreenPause.Visibility == Visibility.Visible) { if (App.IsHideSRTool) { if (App.W_MinToolbar == null) { App.W_MinToolbar = new MinToolbar(); } App.W_MinToolbar.Topmost = true; double minToolTop = Top; new Thread ( () => { Thread.Sleep(500); Dispatcher.Invoke ( () => { MouseEventCommon.GetCursorPos(out MouseEventCommon.POINT pointRecord); if (pointRecord.X >= this.Left && pointRecord.X < this.Left + this.ActualWidth && pointRecord.Y >= this.Top && pointRecord.Y < this.Top + this.ActualHeight) { //LblMessage.Content = "在区域内:("+ pointRecord.X+","+pointRecord.Y+") ("+ Left + ","+Top+")"; } else { //LblMessage.Content = "不在区域内:(" + pointRecord.X + "," + pointRecord.Y + ") (" + Left + "," + Top + ")"; App.W_MinToolbar.Initialize(minToolTop, ActualHeight); GridSrToobar.Visibility = Visibility.Hidden; App.W_MinToolbar.Show(); } } ); } ).Start(); } } } } }