星火微课系统客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ScreenRecordingToolbarWindow.xaml.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. using Common.system;
  2. using System;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using System.Windows.Ink;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Threading;
  12. using XHWK.Model;
  13. namespace XHWK.WKTool
  14. {
  15. /// <summary>
  16. /// 录屏工具栏
  17. /// </summary>
  18. public partial class ScreenRecordingToolbarWindow : Window
  19. {
  20. #region 初始变量
  21. /// <summary>
  22. /// 视频保存名称
  23. /// </summary>
  24. string VideoSavePathName;
  25. /// <summary>
  26. /// 是否首次录屏
  27. /// </summary>
  28. bool IsFirstRS = true;
  29. /// <summary>
  30. /// 是否暂停
  31. /// </summary>
  32. bool IsSuspend = true;
  33. /// <summary>
  34. /// 视频信息
  35. /// </summary>
  36. Model_Video VideoInfo = null;
  37. //声明一个 DrawingAttributes 类型的变量
  38. DrawingAttributes drawingAttributes;
  39. private DispatcherTimer t = null;
  40. /// <summary>
  41. /// 计时器状态
  42. /// </summary>
  43. private enum State
  44. {
  45. Start,
  46. Pause,
  47. End
  48. }
  49. /// <summary>
  50. /// 状态
  51. /// </summary>
  52. private State _state = State.End;
  53. /// <summary>
  54. /// 计时用
  55. /// </summary>
  56. private TimeSpan _timeSpan = new TimeSpan(0, 0, 0, 0, 0);
  57. KeyboardHookCommon k_hook;
  58. #endregion
  59. #region 初始化
  60. /// <summary>
  61. /// 录屏工具栏
  62. /// </summary>
  63. public ScreenRecordingToolbarWindow()
  64. {
  65. InitializeComponent();
  66. }
  67. /// <summary>
  68. /// 初始化
  69. /// </summary>
  70. public void Initialize()
  71. {
  72. k_hook = new KeyboardHookCommon();
  73. k_hook.KeyDownEvent += K_hook_KeyDownEvent;
  74. //创建 DrawingAttributes 类的一个实例
  75. drawingAttributes = new DrawingAttributes();
  76. //将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
  77. //InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
  78. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  79. drawingAttributes.FitToCurve = true;
  80. //隐藏画笔工具栏
  81. //BtnToolbarDown_Click(null, null);
  82. gridToolbar.Visibility = Visibility.Hidden;
  83. gridColour.Visibility = Visibility.Hidden;
  84. gridThickness.Visibility = Visibility.Hidden;
  85. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/microLessonSystem_14.png"));
  86. if (t == null)
  87. {
  88. t = new DispatcherTimer();
  89. t.Tick += OnTimer;
  90. t.Interval = new TimeSpan(0, 0, 0, 1);
  91. t.IsEnabled = true;
  92. t.Start();
  93. }
  94. t.Interval = new TimeSpan(0, 0, 0, 1);
  95. //Stack();
  96. //ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  97. //BtnRecordingScreen_Click(null, null);
  98. }
  99. private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e)
  100. {
  101. if (e.KeyValue == (int)System.Windows.Forms.Keys.F5 && (int)Control.ModifierKeys == (int)Keys.Control)
  102. {
  103. //开始,暂停
  104. BtnRecordingScreen_Click(null, null);
  105. }
  106. if (e.KeyValue == (int)Keys.S && (int)Control.ModifierKeys == (int)Keys.Control)
  107. {
  108. //结束
  109. BtnStopRecordingScreen_Click(null, null);
  110. }
  111. }
  112. #endregion
  113. #region 事件
  114. /// <summary>
  115. /// 时钟回调
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. private void OnTimer(object sender, EventArgs e)
  120. {
  121. switch (_state)
  122. {
  123. case State.Start:
  124. {
  125. _timeSpan += new TimeSpan(0, 0, 0, 1);
  126. }
  127. break;
  128. case State.Pause:
  129. {
  130. }
  131. break;
  132. case State.End:
  133. {
  134. _timeSpan = new TimeSpan();
  135. //_timeSpan = new TimeSpan(0, 23, 12, 45, 54);
  136. }
  137. break;
  138. }
  139. string time = string.Format("{0:D2}:{1:D2}",/* _timeSpan.Hours,*/ _timeSpan.Minutes, _timeSpan.Seconds);
  140. //char[] times = time.ToCharArray();
  141. txbTime.Text = time;
  142. }
  143. /// <summary>
  144. /// 开始
  145. /// </summary>
  146. /// <param name="sender"></param>
  147. /// <param name="e"></param>
  148. private void Stack()
  149. {
  150. _state = State.Start;
  151. }
  152. /// <summary>
  153. /// 暂停
  154. /// </summary>
  155. private void TimeOut()
  156. {
  157. _state = State.Pause;
  158. }
  159. /// <summary>
  160. /// 结束
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void End()
  165. {
  166. _state = State.End;
  167. }
  168. #region 录屏
  169. /// <summary>
  170. /// 设置录屏文件地址
  171. /// </summary>
  172. void SetUpVideoPathName()
  173. {
  174. //FileToolsCommon.DeleteDirectory(APP.WKData.WkPath + "temp/");
  175. FileToolsCommon.CreateDirectory(APP.WKData.WkPath);
  176. FileToolsCommon.DeleteDirectory(APP.WKData.WkPath + "tempsr/");
  177. VideoSavePathName = APP.WKData.WkPath + APP.WKData.WkName + "_录屏." + ((Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"))).ToString();
  178. int num = 1;
  179. while(FileToolsCommon.IsExistFile(VideoSavePathName))
  180. {
  181. VideoSavePathName = APP.WKData.WkPath + APP.WKData.WkName + "_录屏_"+ num + "." + ((Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"))).ToString();
  182. num++;
  183. }
  184. }
  185. /// <summary>
  186. /// 开始或暂停录制
  187. /// </summary>
  188. /// <param name="sender"></param>
  189. /// <param name="e"></param>
  190. private void BtnRecordingScreen_Click(object sender, RoutedEventArgs e)
  191. {
  192. if (IsSuspend)
  193. {
  194. Stack();
  195. if (IsFirstRS)
  196. {
  197. VideoInfo = new Model_Video();
  198. VideoInfo.VideoType = (Enum_VideoType)int.Parse(FileToolsCommon.GetConfigValue("VideoType"));
  199. VideoInfo.WkType = Enum_WKVidetype.RecordingScreen;
  200. SetUpVideoPathName();
  201. IsFirstRS = false;
  202. k_hook.Start();//安装键盘钩子
  203. }
  204. IsSuspend = false;
  205. //#region 录像倒计时
  206. //if (APP.W_CountdownWindow == null)
  207. //{
  208. // APP.W_CountdownWindow = new CountdownWindow();
  209. // //APP.W_CountdownWindow.Topmost = true;
  210. //}
  211. //else
  212. //{
  213. // APP.W_CountdownWindow.Initialize();
  214. // //APP.W_CountdownWindow.Topmost = true;
  215. //}
  216. //APP.W_CountdownWindow.Show();
  217. //#endregion
  218. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  219. try
  220. {
  221. new Thread(new ThreadStart(new Action(() =>
  222. {
  223. APP.FFmpeg.StartRecordingVideo(VideoSavePathName);
  224. }))).Start();
  225. }
  226. catch (Exception ex)
  227. {
  228. System.Windows.MessageBox.Show(ex.Message);
  229. }
  230. }
  231. else
  232. {
  233. TimeOut();
  234. IsSuspend = true;
  235. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/microLessonSystem_14.png"));
  236. #region 2秒内不可点击
  237. new Thread(new ThreadStart(new Action(() =>
  238. {
  239. Dispatcher.Invoke(() =>
  240. {
  241. BtnRecordingScreen.IsEnabled = false;
  242. BtnStopRecordingScreen.IsEnabled = false;
  243. });
  244. Thread.Sleep(2000);
  245. Dispatcher.Invoke(() =>
  246. {
  247. BtnRecordingScreen.IsEnabled = true;
  248. BtnStopRecordingScreen.IsEnabled = true;
  249. });
  250. }))).Start();
  251. #endregion
  252. try
  253. {
  254. APP.FFmpeg.SuspendFFmpeg();
  255. }
  256. catch (Exception ex)
  257. {
  258. System.Windows.MessageBox.Show(ex.Message);
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// 停止录像
  264. /// </summary>
  265. /// <param name="sender"></param>
  266. /// <param name="e"></param>
  267. private void BtnStopRecordingScreen_Click(object sender, RoutedEventArgs e)
  268. {
  269. k_hook.Stop();
  270. IsSuspend = true;
  271. txbTime.Text = "00:00";
  272. End();
  273. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  274. if (gridToolbar.Visibility==Visibility.Visible)
  275. {
  276. gridToolbar.Visibility = Visibility.Hidden;
  277. gridColour.Visibility = Visibility.Hidden;
  278. gridThickness.Visibility = Visibility.Hidden;
  279. APP.W_PracticeWindow.Hide();
  280. }
  281. if (APP.W_XHMicroLessonSystemWindow == null)
  282. {
  283. APP.W_XHMicroLessonSystemWindow = new XHMicroLessonSystemWindow();
  284. }
  285. APP.W_XHMicroLessonSystemWindow.Show();
  286. if (!IsFirstRS)
  287. {
  288. try
  289. {
  290. try
  291. {
  292. APP.FFmpeg.StopFFmpeg(VideoSavePathName);
  293. }
  294. catch (Exception ex)
  295. {
  296. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  297. }
  298. IsFirstRS = true;
  299. //生成缩略图
  300. string ThumbnailPath = FileToolsCommon.GetDirectoryName(VideoSavePathName) + "ThumbnailPath/";
  301. FileToolsCommon.CreateDirectory(ThumbnailPath);
  302. //缩略图存储位置
  303. string ThumbnailPathName = ThumbnailPath + FileToolsCommon.GetIOFileNameNoExtension(VideoSavePathName) + ".JPG";
  304. new Thread(new ThreadStart(new Action(() =>
  305. {
  306. while (!FileToolsCommon.IsExistFile(VideoSavePathName))
  307. {
  308. Thread.Sleep(100);
  309. }
  310. FileToolsCommon.DeleteFile(ThumbnailPathName);
  311. //VideoInfo.VideoSize = FileToolsCommon.GetFileSizeByMB(VideoSavePathName).ToString() + " MB";
  312. VideoInfo.RSTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  313. VideoInfo.VideoPath = VideoSavePathName;
  314. VideoInfo.ThumbnailPath = ThumbnailPathName;
  315. APP.FFmpeg.GenerateThumbnails(VideoSavePathName, ThumbnailPathName);
  316. VideoInfo.FileGuid = Guid.NewGuid().ToString();
  317. VideoInfo.IsUpload = false;
  318. VideoInfo.Uploaded = 0;
  319. VideoInfo.Savefolder = APP.UserInfo.Schoolid + "/resource";
  320. APP.VideoList.Add(VideoInfo);
  321. //保存数据
  322. APP.SaveWkData();
  323. }))).Start();
  324. Click_stopRecordingScreen();
  325. }
  326. catch (Exception ex)
  327. {
  328. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  329. }
  330. }
  331. Hide();
  332. }
  333. #endregion
  334. #region 画笔相关
  335. /// <summary>
  336. /// 画笔工具栏关闭事件
  337. /// </summary>
  338. /// <param name="sender"></param>
  339. /// <param name="e"></param>
  340. private void BtnToolbarDown_Click(object sender, RoutedEventArgs e)
  341. {
  342. gridToolbar.Visibility = Visibility.Hidden;
  343. gridColour.Visibility = Visibility.Hidden;
  344. gridThickness.Visibility = Visibility.Hidden;
  345. APP.W_PracticeWindow.Hide();
  346. }
  347. /// <summary>
  348. /// 画笔点击事件
  349. /// </summary>
  350. /// <param name="sender"></param>
  351. /// <param name="e"></param>
  352. private void BtnBrush_Click(object sender, RoutedEventArgs e)
  353. {
  354. string time = GetTimeStamp();
  355. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  356. if (!Directory.Exists(tempPath))
  357. {
  358. Directory.CreateDirectory(tempPath);
  359. }
  360. string imagePath = Path.Combine(tempPath, time + ".jpg");
  361. //ImageHelper.GetScreenshot(new System.Drawing.Rectangle(0, 0, 0, 0), imagePath);
  362. if (gridToolbar.Visibility == Visibility.Visible)
  363. {
  364. gridToolbar.Visibility = Visibility.Hidden;
  365. }
  366. else
  367. {
  368. gridToolbar.Visibility = Visibility.Visible;
  369. try
  370. {
  371. if (APP.W_PracticeWindow == null)
  372. {
  373. APP.W_PracticeWindow = new PracticeWindow();
  374. //APP.W_PracticeWindow.Topmost = true;
  375. APP.W_PracticeWindow.Width = pwidth;
  376. APP.W_PracticeWindow.Height = pHeight;
  377. APP.W_PracticeWindow.Left = 0;
  378. APP.W_PracticeWindow.Top = 0;
  379. //practiceWin.Owner = this;
  380. }
  381. APP.W_PracticeWindow.Initialize(imagePath);
  382. APP.W_PracticeWindow.Show();
  383. }
  384. catch (Exception ex)
  385. {
  386. LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex);
  387. }
  388. }
  389. //imgCanvas.Source = new BitmapImage(new Uri(imagePath));
  390. }
  391. /// <summary>
  392. /// 屏幕宽
  393. /// </summary>
  394. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  395. /// <summary>
  396. /// 屏幕高
  397. /// </summary>
  398. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  399. /// <summary>
  400. /// 获取时间戳
  401. /// </summary>
  402. /// <returns></returns>
  403. public string GetTimeStamp()
  404. {
  405. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  406. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  407. }
  408. /// <summary>
  409. /// 画笔粗细事件
  410. /// </summary>
  411. /// <param name="sender"></param>
  412. /// <param name="e"></param>
  413. private void BtnThickness_Click(object sender, RoutedEventArgs e)
  414. {
  415. gridThickness.Visibility = Visibility.Visible;
  416. gridColour.Visibility = Visibility.Collapsed;
  417. }
  418. /// <summary>
  419. /// 画笔颜色事件
  420. /// </summary>
  421. /// <param name="sender"></param>
  422. /// <param name="e"></param>
  423. private void BtnColour_Click(object sender, RoutedEventArgs e)
  424. {
  425. gridColour.Visibility = Visibility.Visible;
  426. gridThickness.Visibility = Visibility.Collapsed;
  427. }
  428. /// <summary>
  429. /// 白色
  430. /// </summary>
  431. /// <param name="sender"></param>
  432. /// <param name="e"></param>
  433. private void BtnWhite_Click(object sender, RoutedEventArgs e)
  434. {
  435. //drawingAttributes.Color = Colors.White;
  436. APP.W_PracticeWindow.White();
  437. gridColour.Visibility = Visibility.Collapsed;
  438. gridThickness.Visibility = Visibility.Collapsed;
  439. }
  440. /// <summary>
  441. /// 红色
  442. /// </summary>
  443. /// <param name="sender"></param>
  444. /// <param name="e"></param>
  445. private void BtnRed_Click(object sender, RoutedEventArgs e)
  446. {
  447. //drawingAttributes.Color = Colors.Red;
  448. APP.W_PracticeWindow.Red();
  449. gridColour.Visibility = Visibility.Collapsed;
  450. gridThickness.Visibility = Visibility.Collapsed;
  451. }
  452. /// <summary>
  453. /// 黄色
  454. /// </summary>
  455. /// <param name="sender"></param>
  456. /// <param name="e"></param>
  457. private void BtnYellow_Click(object sender, RoutedEventArgs e)
  458. {
  459. //drawingAttributes.Color = Colors.Gold;
  460. APP.W_PracticeWindow.Yellow();
  461. gridColour.Visibility = Visibility.Collapsed;
  462. gridThickness.Visibility = Visibility.Collapsed;
  463. }
  464. /// <summary>
  465. /// 青色
  466. /// </summary>
  467. /// <param name="sender"></param>
  468. /// <param name="e"></param>
  469. private void BtnCyanBlue_Click(object sender, RoutedEventArgs e)
  470. {
  471. //drawingAttributes.Color = Colors.LimeGreen;
  472. APP.W_PracticeWindow.CyanBlue();
  473. gridColour.Visibility = Visibility.Collapsed;
  474. gridThickness.Visibility = Visibility.Collapsed;
  475. }
  476. /// <summary>
  477. /// 灰色
  478. /// </summary>
  479. /// <param name="sender"></param>
  480. /// <param name="e"></param>
  481. private void BtnGray_Click(object sender, RoutedEventArgs e)
  482. {
  483. //drawingAttributes.Color = Colors.Gray;
  484. APP.W_PracticeWindow.Gray();
  485. gridColour.Visibility = Visibility.Collapsed;
  486. gridThickness.Visibility = Visibility.Collapsed;
  487. }
  488. /// <summary>
  489. /// 蓝色
  490. /// </summary>
  491. /// <param name="sender"></param>
  492. /// <param name="e"></param>
  493. private void BtnBlue_Click(object sender, RoutedEventArgs e)
  494. {
  495. //drawingAttributes.Color = Colors.DeepSkyBlue;
  496. APP.W_PracticeWindow.Blue();
  497. gridColour.Visibility = Visibility.Collapsed;
  498. gridThickness.Visibility = Visibility.Collapsed;
  499. }
  500. /// <summary>
  501. /// 画笔 细
  502. /// </summary>
  503. /// <param name="sender"></param>
  504. /// <param name="e"></param>
  505. private void BtnFine_Click(object sender, RoutedEventArgs e)
  506. {
  507. APP.W_PracticeWindow.Fine();
  508. gridColour.Visibility = Visibility.Collapsed;
  509. gridThickness.Visibility = Visibility.Collapsed;
  510. }
  511. /// <summary>
  512. /// 画笔 中
  513. /// </summary>
  514. /// <param name="sender"></param>
  515. /// <param name="e"></param>
  516. private void BtnIn_Click(object sender, RoutedEventArgs e)
  517. {
  518. APP.W_PracticeWindow.In();
  519. gridColour.Visibility = Visibility.Collapsed;
  520. gridThickness.Visibility = Visibility.Collapsed;
  521. }
  522. /// <summary>
  523. /// 画笔粗
  524. /// </summary>
  525. /// <param name="sender"></param>
  526. /// <param name="e"></param>
  527. private void BtnCrude_Click(object sender, RoutedEventArgs e)
  528. {
  529. APP.W_PracticeWindow.Crude();
  530. gridColour.Visibility = Visibility.Collapsed;
  531. gridThickness.Visibility = Visibility.Collapsed;
  532. }
  533. /// <summary>
  534. /// 橡皮
  535. /// </summary>
  536. /// <param name="sender"></param>
  537. /// <param name="e"></param>
  538. private void BtnEraser_Click(object sender, RoutedEventArgs e)
  539. {
  540. APP.W_PracticeWindow.Eraser();
  541. }
  542. /// <summary>
  543. /// 🖊
  544. /// </summary>
  545. /// <param name="sender"></param>
  546. /// <param name="e"></param>
  547. private void BtnPen_Click(object sender, RoutedEventArgs e)
  548. {
  549. APP.W_PracticeWindow.Pen();
  550. }
  551. /// <summary>
  552. /// ⚪
  553. /// </summary>
  554. /// <param name="sender"></param>
  555. /// <param name="e"></param>
  556. private void BtnRound_Click(object sender, RoutedEventArgs e)
  557. {
  558. APP.W_PracticeWindow.Round();
  559. }
  560. /// <summary>
  561. /// 矩形
  562. /// </summary>
  563. /// <param name="sender"></param>
  564. /// <param name="e"></param>
  565. private void BtnRectangle_Click(object sender, RoutedEventArgs e)
  566. {
  567. APP.W_PracticeWindow.Rectangle();
  568. }
  569. #endregion
  570. #endregion
  571. /// <summary>
  572. /// 停止录屏
  573. /// </summary>
  574. public delegate void StopRecordingScreen();
  575. /// <summary>
  576. /// 停止录屏
  577. /// </summary>
  578. public event StopRecordingScreen Click_stopRecordingScreen;
  579. /// <summary>
  580. /// 黑笔
  581. /// </summary>
  582. /// <param name="sender"></param>
  583. /// <param name="e"></param>
  584. private void BtnBlackPen_Click(object sender, RoutedEventArgs e)
  585. {
  586. }
  587. /// <summary>
  588. /// 移动工具栏
  589. /// </summary>
  590. /// <param name="sender"></param>
  591. /// <param name="e"></param>
  592. private void Grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  593. {
  594. DragMove();
  595. }
  596. }
  597. }