星火微课系统客户端
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.

JieTuWindow.xaml.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. using ComeCapture.Controls;
  2. using ComeCapture.Helpers;
  3. using ComeCapture.Models;
  4. using Common.system;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using XHWK.WKTool;
  16. using static XHWK.WKTool.XHMicroLessonSystemWindow;
  17. namespace ComeCapture
  18. {
  19. /// <summary>
  20. /// 截图 MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class JieTuWindow : Window
  23. {
  24. public static double ScreenWidth = SystemParameters.PrimaryScreenWidth;
  25. public static double ScreenHeight = SystemParameters.PrimaryScreenHeight;
  26. public static double ScreenScale = 1;
  27. public static int MinSize = 10;
  28. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  29. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  30. //画图注册名称集合
  31. public List<NameAndLimit> list = new List<NameAndLimit>();
  32. //画图注册名称
  33. public int num = 1;
  34. //是否截图开始
  35. private bool _IsMouseDown = false;
  36. //是否截图完毕
  37. private bool _IsCapture = false;
  38. private double _X0 = 0;
  39. private double _Y0 = 0;
  40. /// <summary>
  41. /// 键盘钩子
  42. /// </summary>
  43. private KeyboardHookCommon k_hook;
  44. //private IntPtr BitmapPtr = IntPtr.Zero;
  45. //private IntPtr JieTuBitmapPtr = IntPtr.Zero;
  46. /// <summary>
  47. /// 截图
  48. /// </summary>
  49. public JieTuWindow()
  50. {
  51. _Current = this;
  52. InitializeComponent();
  53. k_hook = new KeyboardHookCommon();
  54. k_hook.KeyDownEvent += K_hook_KeyDownEvent;
  55. DataContext = new AppModel();
  56. initialization();
  57. Screenshot();
  58. MaxWindow();
  59. MaskLeft.Height = ScreenHeight;
  60. MaskRight.Height = ScreenHeight;
  61. //计算Windows项目缩放比例
  62. ScreenHelper.ResetScreenScale();
  63. //RemoveControl();
  64. //AllowsTransparency = "True"
  65. }
  66. public void Screenshot()
  67. {
  68. //IntPtr BitmapPtr = IntPtr.Zero;
  69. //Background = new ImageBrush(ImageHelper.GetFullBitmapSource(out BitmapPtr));
  70. string time = GetTimeStamp();
  71. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  72. if (!Directory.Exists(tempPath))
  73. {
  74. Directory.CreateDirectory(tempPath);
  75. }
  76. string imagePath = Path.Combine(tempPath, time + ".jpg");
  77. if (ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), null, true, out BitmapImage bitmap))
  78. {
  79. Background = new ImageBrush(bitmap);
  80. }
  81. }
  82. public void initialization()
  83. {
  84. Background = null;
  85. _IsMouseDown = false;
  86. _IsCapture = false;
  87. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  88. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  89. SizeRGB.Visibility = Visibility.Collapsed;
  90. MainImage.Visibility = Visibility.Collapsed;
  91. DataContext = new AppModel();
  92. WpfHelper.MainDispatcher = Dispatcher;
  93. }
  94. /// <summary>
  95. /// 初始化快捷键
  96. /// </summary>
  97. public void InitializeKeyDownEvent()
  98. {
  99. k_hook.Start();//安装键盘钩子
  100. }
  101. /// <summary>
  102. /// 快捷键
  103. /// </summary>
  104. /// <param name="sender"></param>
  105. /// <param name="e"></param>
  106. private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e)
  107. {
  108. if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
  109. {
  110. k_hook.Stop();
  111. Current.OnCancel();
  112. APP.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent();
  113. APP.W_XHMicroLessonSystemWindow.WindowType();
  114. Hide();
  115. }
  116. }
  117. #region 属性 Current
  118. private static JieTuWindow _Current = null;
  119. public static JieTuWindow Current => _Current;
  120. #endregion 属性 Current
  121. #region 全屏+置顶
  122. private void MaxWindow()
  123. {
  124. Left = 0;
  125. Top = 0;
  126. Width = ScreenWidth;
  127. Height = ScreenHeight;
  128. Activate();
  129. }
  130. #endregion 全屏+置顶
  131. #region 注册画图
  132. public static void Register(object control)
  133. {
  134. string name = "Draw" + _Current.num;
  135. _Current.MainCanvas.RegisterName(name, control);
  136. _Current.list.Add(new NameAndLimit(name));
  137. _Current.num++;
  138. }
  139. #endregion 注册画图
  140. #region 截图区域添加画图
  141. public static void AddControl(UIElement e)
  142. {
  143. _Current.MainCanvas.Children.Add(e);
  144. }
  145. #endregion 截图区域添加画图
  146. #region 截图区域撤回画图
  147. public static void RemoveControl(UIElement e)
  148. {
  149. _Current.MainCanvas.Children.Remove(e);
  150. }
  151. #endregion 截图区域撤回画图
  152. #region 撤销
  153. public void OnRevoke()
  154. {
  155. if (list.Count > 0)
  156. {
  157. string name = list[list.Count - 1].Name;
  158. object obj = MainCanvas.FindName(name);
  159. if (obj != null)
  160. {
  161. MainCanvas.Children.Remove(obj as UIElement);
  162. MainCanvas.UnregisterName(name);
  163. list.RemoveAt(list.Count - 1);
  164. MainImage.Limit = list.Count == 0 ? new Limit() : list[list.Count - 1].Limit;
  165. }
  166. }
  167. }
  168. #endregion 撤销
  169. #region 保存
  170. public void OnSave()
  171. {
  172. //var sfd = new Microsoft.Win32.SaveFileDialog
  173. //{
  174. // FileName = "截图" + DateTime.Now.ToString("yyyyMMddhhmmss"),
  175. // Filter = "png|*.png",
  176. // AddExtension = true,
  177. // RestoreDirectory = true
  178. //};
  179. //if (sfd.ShowDialog() == true)
  180. //{
  181. // Hidden();
  182. // Thread t = new Thread(new ThreadStart(() =>
  183. // {
  184. // Thread.Sleep(200);
  185. // WpfHelper.SafeRun(() =>
  186. // {
  187. // var source = GetCapture();
  188. // if (source != null)
  189. // {
  190. // ImageHelper.SaveToPng(source, sfd.FileName);
  191. // Shared.TeachingData.FilePath = sfd.FileName;
  192. // //Thread.Sleep(200);
  193. // }
  194. // Close();
  195. // if (ChangeTextEvent != null)
  196. // {
  197. // ChangeTextEvent("关闭窗口");
  198. // }
  199. // //Go(sfd.FileName);
  200. // });
  201. // }))
  202. // {
  203. // IsBackground = true
  204. // };
  205. // t.Start();
  206. //}
  207. }
  208. #endregion 保存
  209. //定义事件
  210. public event ChangeTextHandler ChangeTextEvent;
  211. #region 获取截图
  212. private void GetCapture()
  213. {
  214. string time = GetTimeStamp();
  215. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
  216. if (!Directory.Exists(tempPath))
  217. {
  218. Directory.CreateDirectory(tempPath);
  219. }
  220. APP.ImgPath = string.Empty;
  221. string imagePath = Path.Combine(tempPath, time + ".jpg");
  222. APP.ImgPath = imagePath;
  223. //LogHelper.WriteInfoLog(imagePath);
  224. //this.Close();
  225. //string imagePath = ImageHelper.GetImagePath(out string serverSavePath);
  226. //BitmapSource bitmap = ImageHelper.GetBitmapSource((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2, out JieTuBitmapPtr);
  227. /* BitmapImage bitmap = */
  228. ImageHelper.GetScreenshot(new Rectangle((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2), imagePath, true, out BitmapImage bitmapimg);
  229. _IsMouseDown = false;
  230. _IsCapture = false;
  231. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  232. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  233. SizeRGB.Visibility = Visibility.Collapsed;
  234. MainImage.Visibility = Visibility.Collapsed;
  235. DataContext = new AppModel();
  236. //ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), imagePath, true, out BitmapImage bitmap);//zxy
  237. //Background = new ImageBrush(bitmap);
  238. WpfHelper.MainDispatcher = Dispatcher;
  239. MaxWindow();
  240. MaskLeft.Height = ScreenHeight;
  241. MaskRight.Height = ScreenHeight;
  242. //计算Windows项目缩放比例
  243. ScreenHelper.ResetScreenScale();
  244. //if (bitmap != null)
  245. //{
  246. // //Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = serverSavePath;
  247. // //Shared.TeachingData.FilePath = imagePath;
  248. //}
  249. ///修改为统一截图时存储到服务器 修改人:赵耀 修改时间:2020年8月11日
  250. //if (bitmap != null)
  251. //{
  252. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  253. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  254. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  255. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  256. // Shared.TeachingData.FilePath = path;
  257. // FileInfo fi = new FileInfo(path); var di = fi.Directory; if (!di.Exists) {
  258. // di.Create();
  259. //}
  260. // using (var fs = System.IO.File.Create(path))
  261. // {
  262. // BitmapEncoder encoder = new PngBitmapEncoder();
  263. // encoder.Frames.Add(BitmapFrame.Create(bitmap));
  264. // encoder.Save(fs);
  265. // bitmap = null;
  266. // encoder = null;
  267. // }
  268. //}
  269. //Dispose();
  270. //GC.Collect();
  271. //ImageHelper.DeleteObject(BitmapPtr);
  272. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  273. initialization();
  274. Owner = null;
  275. WindowState = WindowState.Minimized;
  276. Visibility = Visibility.Hidden;
  277. ShowInTaskbar = false;
  278. //this.Hide();
  279. if (ChangeTextEvent != null)
  280. {
  281. k_hook.Stop();
  282. ChangeTextEvent("关闭窗口");
  283. }
  284. }
  285. #endregion 获取截图
  286. /// <summary>
  287. /// 获取时间戳
  288. /// </summary>
  289. /// <returns></returns>
  290. public string GetTimeStamp()
  291. {
  292. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  293. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  294. }
  295. #region 退出截图
  296. public void OnCancel()
  297. {
  298. //Close();
  299. //ImageHelper.DeleteObject(BitmapPtr);
  300. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  301. initialization();
  302. Owner = null;
  303. WindowState = WindowState.Minimized;
  304. Visibility = Visibility.Hidden;
  305. ShowInTaskbar = false;
  306. //this.Hide();
  307. //_Bitmap.Dispose();
  308. if (ChangeTextEvent != null)
  309. {
  310. k_hook.Stop();
  311. ChangeTextEvent("取消");
  312. }
  313. }
  314. #endregion 退出截图
  315. #region 完成截图
  316. public void OnOK()
  317. {
  318. Hidden();
  319. Thread t = new Thread(new ThreadStart(() =>
  320. {
  321. Thread.Sleep(50);
  322. //Thread.Sleep(200);
  323. WpfHelper.SafeRun(() =>
  324. {
  325. /*var source = */
  326. GetCapture();
  327. //Shared.TeachingData.Source = source;
  328. //if (source != null)
  329. //{
  330. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  331. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  332. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  333. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  334. // ImageHelper.SaveToPng(source, path);
  335. // Shared.TeachingData.FilePath = path;
  336. // //Clipboard.SetImage(source);
  337. //}
  338. //Close();
  339. //if (ChangeTextEvent != null)
  340. //{
  341. // ChangeTextEvent("关闭窗口");
  342. //}
  343. });
  344. }))
  345. {
  346. IsBackground = true
  347. };
  348. t.Start();
  349. }
  350. #endregion 完成截图
  351. #region 截图前隐藏窗口
  352. private void Hidden()
  353. {
  354. //隐藏尺寸RGB框
  355. if (AppModel.Current.MaskTopHeight < 40)
  356. {
  357. SizeRGB.Visibility = Visibility.Collapsed;
  358. }
  359. int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
  360. if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
  361. {
  362. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  363. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  364. }
  365. MainImage.ZoomThumbVisibility = Visibility.Collapsed;
  366. }
  367. #endregion 截图前隐藏窗口
  368. #region 鼠标及键盘事件
  369. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  370. {
  371. if (_IsCapture)
  372. {
  373. return;
  374. }
  375. System.Windows.Point point = e.GetPosition(this);
  376. _X0 = point.X;
  377. _Y0 = point.Y;
  378. _IsMouseDown = true;
  379. Canvas.SetLeft(MainImage, _X0);
  380. Canvas.SetTop(MainImage, _Y0);
  381. AppModel.Current.MaskLeftWidth = _X0;
  382. AppModel.Current.MaskRightWidth = ScreenWidth - _X0;
  383. AppModel.Current.MaskTopHeight = _Y0;
  384. Show_Size.Visibility = Visibility.Visible;
  385. }
  386. private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  387. {
  388. if (!_IsMouseDown || _IsCapture)
  389. {
  390. return;
  391. }
  392. _IsMouseDown = false;
  393. if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
  394. {
  395. _IsCapture = true;
  396. ImageEditBar.Current.Visibility = Visibility.Visible;
  397. ImageEditBar.Current.ResetCanvas();
  398. Cursor = Cursors.Arrow;
  399. }
  400. }
  401. private void Window_MouseMove(object sender, MouseEventArgs e)
  402. {
  403. System.Windows.Point point = e.GetPosition(this);
  404. System.Windows.Point screenP = PointToScreen(point);
  405. try
  406. {
  407. AppModel.Current.ShowRGB = ImageHelper.GetRGB((int)screenP.X, (int)screenP.Y);
  408. }
  409. catch (Exception ex)
  410. {
  411. MessageWindow.Show(ex.Message);
  412. }
  413. if (_IsCapture)
  414. {
  415. return;
  416. }
  417. if (Show_RGB.Visibility == Visibility.Collapsed)
  418. {
  419. Show_RGB.Visibility = Visibility.Visible;
  420. }
  421. if (_IsMouseDown)
  422. {
  423. double w = point.X - _X0;
  424. double h = point.Y - _Y0;
  425. if (w < MinSize || h < MinSize)
  426. {
  427. return;
  428. }
  429. if (MainImage.Visibility == Visibility.Collapsed)
  430. {
  431. MainImage.Visibility = Visibility.Visible;
  432. }
  433. AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
  434. AppModel.Current.MaskTopWidth = w;
  435. AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
  436. AppModel.Current.ChangeShowSize();
  437. MainImage.Width = w;
  438. MainImage.Height = h;
  439. }
  440. else
  441. {
  442. AppModel.Current.ShowSizeLeft = point.X;
  443. AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
  444. }
  445. }
  446. private void Window_KeyDown(object sender, KeyEventArgs e)
  447. {
  448. if (e.Key == Key.Escape)
  449. {
  450. initialization();
  451. Owner = null;
  452. WindowState = WindowState.Minimized;
  453. Visibility = Visibility.Hidden;
  454. ShowInTaskbar = false;
  455. }
  456. }
  457. #endregion 鼠标及键盘事件
  458. /// <summary>
  459. /// 关闭时清除tool页记录的截图数据
  460. /// </summary>
  461. /// <param name="sender"></param>
  462. /// <param name="e"></param>
  463. private void Window_Closed(object sender, EventArgs e)
  464. {
  465. click_closeJietuWindowClick();
  466. }
  467. /// <summary>
  468. /// 非正常关闭时jietuWindow=null;
  469. /// </summary>
  470. /// <param name="sender"></param>
  471. /// <param name="e"></param>
  472. public delegate void CloseJietuWindowClick();
  473. /// <summary>
  474. /// 非正常关闭时jietuWindow=null;
  475. /// </summary>
  476. public event CloseJietuWindowClick click_closeJietuWindowClick;
  477. }
  478. }