星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ScreenRecordingToolbarWindow.xaml.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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/Toobar26.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. txbTime.Visibility = Visibility.Visible;
  220. try
  221. {
  222. new Thread(new ThreadStart(new Action(() =>
  223. {
  224. APP.FFmpeg.StartRecordingVideo(VideoSavePathName);
  225. }))).Start();
  226. }
  227. catch (Exception ex)
  228. {
  229. System.Windows.MessageBox.Show(ex.Message);
  230. }
  231. }
  232. else
  233. {
  234. TimeOut();
  235. IsSuspend = true;
  236. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/microLessonSystem_14.png"));
  237. #region 2秒内不可点击
  238. new Thread(new ThreadStart(new Action(() =>
  239. {
  240. Dispatcher.Invoke(() =>
  241. {
  242. BtnRecordingScreen.IsEnabled = false;
  243. BtnStopRecordingScreen.IsEnabled = false;
  244. });
  245. Thread.Sleep(2000);
  246. Dispatcher.Invoke(() =>
  247. {
  248. BtnRecordingScreen.IsEnabled = true;
  249. BtnStopRecordingScreen.IsEnabled = true;
  250. });
  251. }))).Start();
  252. #endregion
  253. try
  254. {
  255. APP.FFmpeg.SuspendFFmpeg();
  256. }
  257. catch (Exception ex)
  258. {
  259. System.Windows.MessageBox.Show(ex.Message);
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// 停止录像
  265. /// </summary>
  266. /// <param name="sender"></param>
  267. /// <param name="e"></param>
  268. private void BtnStopRecordingScreen_Click(object sender, RoutedEventArgs e)
  269. {
  270. k_hook.Stop();
  271. IsSuspend = true;
  272. txbTime.Text = "00:00";
  273. End();
  274. ImgRecordingScreen.Source = new BitmapImage(new Uri("pack://application:,,,/Images/Toobar25.png"));
  275. if (gridToolbar.Visibility==Visibility.Visible)
  276. {
  277. gridToolbar.Visibility = Visibility.Hidden;
  278. gridColour.Visibility = Visibility.Hidden;
  279. gridThickness.Visibility = Visibility.Hidden;
  280. APP.W_PracticeWindow.Hide();
  281. }
  282. if (APP.W_XHMicroLessonSystemWindow == null)
  283. {
  284. APP.W_XHMicroLessonSystemWindow = new XHMicroLessonSystemWindow();
  285. }
  286. APP.W_XHMicroLessonSystemWindow.Show();
  287. if (!IsFirstRS)
  288. {
  289. try
  290. {
  291. try
  292. {
  293. APP.FFmpeg.StopFFmpeg(VideoSavePathName);
  294. }
  295. catch (Exception ex)
  296. {
  297. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  298. }
  299. IsFirstRS = true;
  300. //生成缩略图
  301. string ThumbnailPath = FileToolsCommon.GetDirectoryName(VideoSavePathName) + "ThumbnailPath/";
  302. FileToolsCommon.CreateDirectory(ThumbnailPath);
  303. //缩略图存储位置
  304. string ThumbnailPathName = ThumbnailPath + FileToolsCommon.GetIOFileNameNoExtension(VideoSavePathName) + ".JPG";
  305. new Thread(new ThreadStart(new Action(() =>
  306. {
  307. while (!FileToolsCommon.IsExistFile(VideoSavePathName))
  308. {
  309. Thread.Sleep(100);
  310. }
  311. FileToolsCommon.DeleteFile(ThumbnailPathName);
  312. //VideoInfo.VideoSize = FileToolsCommon.GetFileSizeByMB(VideoSavePathName).ToString() + " MB";
  313. VideoInfo.RSTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  314. VideoInfo.VideoPath = VideoSavePathName;
  315. VideoInfo.ThumbnailPath = ThumbnailPathName;
  316. APP.FFmpeg.GenerateThumbnails(VideoSavePathName, ThumbnailPathName);
  317. VideoInfo.FileGuid = Guid.NewGuid().ToString();
  318. VideoInfo.IsUpload = false;
  319. VideoInfo.Uploaded = 0;
  320. VideoInfo.Savefolder = APP.UserInfo.Schoolid + "/resource";
  321. APP.VideoList.Add(VideoInfo);
  322. //保存数据
  323. APP.SaveWkData();
  324. }))).Start();
  325. Click_stopRecordingScreen();
  326. }
  327. catch (Exception ex)
  328. {
  329. LogHelper.WriteErrLog("【停止录屏】(BtnStopRecordingScreen_Click)" + ex.Message, ex);
  330. }
  331. }
  332. Hide();
  333. }
  334. #endregion
  335. #region 画笔相关
  336. /// <summary>
  337. /// 画笔工具栏关闭事件
  338. /// </summary>
  339. /// <param name="sender"></param>
  340. /// <param name="e"></param>
  341. private void BtnToolbarDown_Click(object sender, RoutedEventArgs e)
  342. {
  343. gridToolbar.Visibility = Visibility.Hidden;
  344. gridColour.Visibility = Visibility.Hidden;
  345. gridThickness.Visibility = Visibility.Hidden;
  346. APP.W_PracticeWindow.Hide();
  347. }
  348. /// <summary>
  349. /// 画笔点击事件
  350. /// </summary>
  351. /// <param name="sender"></param>
  352. /// <param name="e"></param>
  353. private void BtnBrush_Click(object sender, RoutedEventArgs e)
  354. {
  355. string time = GetTimeStamp();
  356. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  357. if (!Directory.Exists(tempPath))
  358. {
  359. Directory.CreateDirectory(tempPath);
  360. }
  361. string imagePath = Path.Combine(tempPath, time + ".jpg");
  362. //ImageHelper.GetScreenshot(new System.Drawing.Rectangle(0, 0, 0, 0), imagePath);
  363. if (gridToolbar.Visibility == Visibility.Visible)
  364. {
  365. gridToolbar.Visibility = Visibility.Hidden;
  366. }
  367. else
  368. {
  369. gridToolbar.Visibility = Visibility.Visible;
  370. try
  371. {
  372. if (APP.W_PracticeWindow == null)
  373. {
  374. APP.W_PracticeWindow = new PracticeWindow();
  375. //APP.W_PracticeWindow.Topmost = true;
  376. APP.W_PracticeWindow.Width = pwidth;
  377. APP.W_PracticeWindow.Height = pHeight;
  378. APP.W_PracticeWindow.Left = 0;
  379. APP.W_PracticeWindow.Top = 0;
  380. //practiceWin.Owner = this;
  381. }
  382. APP.W_PracticeWindow.Initialize(imagePath);
  383. APP.W_PracticeWindow.Show();
  384. }
  385. catch (Exception ex)
  386. {
  387. LogHelper.WriteErrLog("【批注(PracticeWindow)" + ex.Message, ex);
  388. }
  389. }
  390. //imgCanvas.Source = new BitmapImage(new Uri(imagePath));
  391. }
  392. /// <summary>
  393. /// 屏幕宽
  394. /// </summary>
  395. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  396. /// <summary>
  397. /// 屏幕高
  398. /// </summary>
  399. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  400. /// <summary>
  401. /// 获取时间戳
  402. /// </summary>
  403. /// <returns></returns>
  404. public string GetTimeStamp()
  405. {
  406. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  407. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  408. }
  409. /// <summary>
  410. /// 画笔粗细事件
  411. /// </summary>
  412. /// <param name="sender"></param>
  413. /// <param name="e"></param>
  414. private void BtnThickness_Click(object sender, RoutedEventArgs e)
  415. {
  416. gridThickness.Visibility = Visibility.Visible;
  417. gridColour.Visibility = Visibility.Collapsed;
  418. }
  419. /// <summary>
  420. /// 画笔颜色事件
  421. /// </summary>
  422. /// <param name="sender"></param>
  423. /// <param name="e"></param>
  424. private void BtnColour_Click(object sender, RoutedEventArgs e)
  425. {
  426. gridColour.Visibility = Visibility.Visible;
  427. gridThickness.Visibility = Visibility.Collapsed;
  428. }
  429. /// <summary>
  430. /// 白色
  431. /// </summary>
  432. /// <param name="sender"></param>
  433. /// <param name="e"></param>
  434. private void BtnWhite_Click(object sender, RoutedEventArgs e)
  435. {
  436. //drawingAttributes.Color = Colors.White;
  437. APP.W_PracticeWindow.White();
  438. gridColour.Visibility = Visibility.Collapsed;
  439. gridThickness.Visibility = Visibility.Collapsed;
  440. }
  441. /// <summary>
  442. /// 红色
  443. /// </summary>
  444. /// <param name="sender"></param>
  445. /// <param name="e"></param>
  446. private void BtnRed_Click(object sender, RoutedEventArgs e)
  447. {
  448. //drawingAttributes.Color = Colors.Red;
  449. APP.W_PracticeWindow.Red();
  450. gridColour.Visibility = Visibility.Collapsed;
  451. gridThickness.Visibility = Visibility.Collapsed;
  452. }
  453. /// <summary>
  454. /// 黄色
  455. /// </summary>
  456. /// <param name="sender"></param>
  457. /// <param name="e"></param>
  458. private void BtnYellow_Click(object sender, RoutedEventArgs e)
  459. {
  460. //drawingAttributes.Color = Colors.Gold;
  461. APP.W_PracticeWindow.Yellow();
  462. gridColour.Visibility = Visibility.Collapsed;
  463. gridThickness.Visibility = Visibility.Collapsed;
  464. }
  465. /// <summary>
  466. /// 青色
  467. /// </summary>
  468. /// <param name="sender"></param>
  469. /// <param name="e"></param>
  470. private void BtnCyanBlue_Click(object sender, RoutedEventArgs e)
  471. {
  472. //drawingAttributes.Color = Colors.LimeGreen;
  473. APP.W_PracticeWindow.CyanBlue();
  474. gridColour.Visibility = Visibility.Collapsed;
  475. gridThickness.Visibility = Visibility.Collapsed;
  476. }
  477. /// <summary>
  478. /// 灰色
  479. /// </summary>
  480. /// <param name="sender"></param>
  481. /// <param name="e"></param>
  482. private void BtnGray_Click(object sender, RoutedEventArgs e)
  483. {
  484. //drawingAttributes.Color = Colors.Gray;
  485. APP.W_PracticeWindow.Gray();
  486. gridColour.Visibility = Visibility.Collapsed;
  487. gridThickness.Visibility = Visibility.Collapsed;
  488. }
  489. /// <summary>
  490. /// 蓝色
  491. /// </summary>
  492. /// <param name="sender"></param>
  493. /// <param name="e"></param>
  494. private void BtnBlue_Click(object sender, RoutedEventArgs e)
  495. {
  496. //drawingAttributes.Color = Colors.DeepSkyBlue;
  497. APP.W_PracticeWindow.Blue();
  498. gridColour.Visibility = Visibility.Collapsed;
  499. gridThickness.Visibility = Visibility.Collapsed;
  500. }
  501. /// <summary>
  502. /// 画笔 细
  503. /// </summary>
  504. /// <param name="sender"></param>
  505. /// <param name="e"></param>
  506. private void BtnFine_Click(object sender, RoutedEventArgs e)
  507. {
  508. APP.W_PracticeWindow.Fine();
  509. gridColour.Visibility = Visibility.Collapsed;
  510. gridThickness.Visibility = Visibility.Collapsed;
  511. }
  512. /// <summary>
  513. /// 画笔 中
  514. /// </summary>
  515. /// <param name="sender"></param>
  516. /// <param name="e"></param>
  517. private void BtnIn_Click(object sender, RoutedEventArgs e)
  518. {
  519. APP.W_PracticeWindow.In();
  520. gridColour.Visibility = Visibility.Collapsed;
  521. gridThickness.Visibility = Visibility.Collapsed;
  522. }
  523. /// <summary>
  524. /// 画笔粗
  525. /// </summary>
  526. /// <param name="sender"></param>
  527. /// <param name="e"></param>
  528. private void BtnCrude_Click(object sender, RoutedEventArgs e)
  529. {
  530. APP.W_PracticeWindow.Crude();
  531. gridColour.Visibility = Visibility.Collapsed;
  532. gridThickness.Visibility = Visibility.Collapsed;
  533. }
  534. /// <summary>
  535. /// 橡皮
  536. /// </summary>
  537. /// <param name="sender"></param>
  538. /// <param name="e"></param>
  539. private void BtnEraser_Click(object sender, RoutedEventArgs e)
  540. {
  541. APP.W_PracticeWindow.Eraser();
  542. }
  543. /// <summary>
  544. /// 🖊
  545. /// </summary>
  546. /// <param name="sender"></param>
  547. /// <param name="e"></param>
  548. private void BtnPen_Click(object sender, RoutedEventArgs e)
  549. {
  550. APP.W_PracticeWindow.Pen();
  551. }
  552. /// <summary>
  553. /// ⚪
  554. /// </summary>
  555. /// <param name="sender"></param>
  556. /// <param name="e"></param>
  557. private void BtnRound_Click(object sender, RoutedEventArgs e)
  558. {
  559. APP.W_PracticeWindow.Round();
  560. }
  561. /// <summary>
  562. /// 矩形
  563. /// </summary>
  564. /// <param name="sender"></param>
  565. /// <param name="e"></param>
  566. private void BtnRectangle_Click(object sender, RoutedEventArgs e)
  567. {
  568. APP.W_PracticeWindow.Rectangle();
  569. }
  570. #endregion
  571. #endregion
  572. /// <summary>
  573. /// 停止录屏
  574. /// </summary>
  575. public delegate void StopRecordingScreen();
  576. /// <summary>
  577. /// 停止录屏
  578. /// </summary>
  579. public event StopRecordingScreen Click_stopRecordingScreen;
  580. /// <summary>
  581. /// 黑笔
  582. /// </summary>
  583. /// <param name="sender"></param>
  584. /// <param name="e"></param>
  585. private void BtnBlackPen_Click(object sender, RoutedEventArgs e)
  586. {
  587. }
  588. /// <summary>
  589. /// 移动工具栏
  590. /// </summary>
  591. /// <param name="sender"></param>
  592. /// <param name="e"></param>
  593. private void Grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  594. {
  595. DragMove();
  596. }
  597. }
  598. }