星火微课系统客户端
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JieTuWindow.xaml.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. this.Hide();
  114. }
  115. }
  116. #region 属性 Current
  117. private static JieTuWindow _Current = null;
  118. public static JieTuWindow Current => _Current;
  119. #endregion 属性 Current
  120. #region 全屏+置顶
  121. private void MaxWindow()
  122. {
  123. Left = 0;
  124. Top = 0;
  125. Width = ScreenWidth;
  126. Height = ScreenHeight;
  127. Activate();
  128. }
  129. #endregion 全屏+置顶
  130. #region 注册画图
  131. public static void Register(object control)
  132. {
  133. string name = "Draw" + _Current.num;
  134. _Current.MainCanvas.RegisterName(name, control);
  135. _Current.list.Add(new NameAndLimit(name));
  136. _Current.num++;
  137. }
  138. #endregion 注册画图
  139. #region 截图区域添加画图
  140. public static void AddControl(UIElement e)
  141. {
  142. _Current.MainCanvas.Children.Add(e);
  143. }
  144. #endregion 截图区域添加画图
  145. #region 截图区域撤回画图
  146. public static void RemoveControl(UIElement e)
  147. {
  148. _Current.MainCanvas.Children.Remove(e);
  149. }
  150. #endregion 截图区域撤回画图
  151. #region 撤销
  152. public void OnRevoke()
  153. {
  154. if (list.Count > 0)
  155. {
  156. string name = list[list.Count - 1].Name;
  157. object obj = MainCanvas.FindName(name);
  158. if (obj != null)
  159. {
  160. MainCanvas.Children.Remove(obj as UIElement);
  161. MainCanvas.UnregisterName(name);
  162. list.RemoveAt(list.Count - 1);
  163. MainImage.Limit = list.Count == 0 ? new Limit() : list[list.Count - 1].Limit;
  164. }
  165. }
  166. }
  167. #endregion 撤销
  168. #region 保存
  169. public void OnSave()
  170. {
  171. //var sfd = new Microsoft.Win32.SaveFileDialog
  172. //{
  173. // FileName = "截图" + DateTime.Now.ToString("yyyyMMddhhmmss"),
  174. // Filter = "png|*.png",
  175. // AddExtension = true,
  176. // RestoreDirectory = true
  177. //};
  178. //if (sfd.ShowDialog() == true)
  179. //{
  180. // Hidden();
  181. // Thread t = new Thread(new ThreadStart(() =>
  182. // {
  183. // Thread.Sleep(200);
  184. // WpfHelper.SafeRun(() =>
  185. // {
  186. // var source = GetCapture();
  187. // if (source != null)
  188. // {
  189. // ImageHelper.SaveToPng(source, sfd.FileName);
  190. // Shared.TeachingData.FilePath = sfd.FileName;
  191. // //Thread.Sleep(200);
  192. // }
  193. // Close();
  194. // if (ChangeTextEvent != null)
  195. // {
  196. // ChangeTextEvent("关闭窗口");
  197. // }
  198. // //Go(sfd.FileName);
  199. // });
  200. // }))
  201. // {
  202. // IsBackground = true
  203. // };
  204. // t.Start();
  205. //}
  206. }
  207. #endregion 保存
  208. //定义事件
  209. public event ChangeTextHandler ChangeTextEvent;
  210. #region 获取截图
  211. private void GetCapture()
  212. {
  213. string time = GetTimeStamp();
  214. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
  215. if (!Directory.Exists(tempPath))
  216. {
  217. Directory.CreateDirectory(tempPath);
  218. }
  219. APP.ImgPath = string.Empty;
  220. string imagePath = Path.Combine(tempPath, time + ".jpg");
  221. APP.ImgPath = imagePath;
  222. //LogHelper.WriteInfoLog(imagePath);
  223. //this.Close();
  224. //string imagePath = ImageHelper.GetImagePath(out string serverSavePath);
  225. //BitmapSource bitmap = ImageHelper.GetBitmapSource((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2, out JieTuBitmapPtr);
  226. /* BitmapImage bitmap = */
  227. 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);
  228. _IsMouseDown = false;
  229. _IsCapture = false;
  230. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  231. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  232. SizeRGB.Visibility = Visibility.Collapsed;
  233. MainImage.Visibility = Visibility.Collapsed;
  234. DataContext = new AppModel();
  235. //ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), imagePath, true, out BitmapImage bitmap);//zxy
  236. //Background = new ImageBrush(bitmap);
  237. WpfHelper.MainDispatcher = Dispatcher;
  238. MaxWindow();
  239. MaskLeft.Height = ScreenHeight;
  240. MaskRight.Height = ScreenHeight;
  241. //计算Windows项目缩放比例
  242. ScreenHelper.ResetScreenScale();
  243. //if (bitmap != null)
  244. //{
  245. // //Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = serverSavePath;
  246. // //Shared.TeachingData.FilePath = imagePath;
  247. //}
  248. ///修改为统一截图时存储到服务器 修改人:赵耀 修改时间:2020年8月11日
  249. //if (bitmap != null)
  250. //{
  251. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  252. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  253. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  254. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  255. // Shared.TeachingData.FilePath = path;
  256. // FileInfo fi = new FileInfo(path); var di = fi.Directory; if (!di.Exists) {
  257. // di.Create();
  258. //}
  259. // using (var fs = System.IO.File.Create(path))
  260. // {
  261. // BitmapEncoder encoder = new PngBitmapEncoder();
  262. // encoder.Frames.Add(BitmapFrame.Create(bitmap));
  263. // encoder.Save(fs);
  264. // bitmap = null;
  265. // encoder = null;
  266. // }
  267. //}
  268. //Dispose();
  269. //GC.Collect();
  270. //ImageHelper.DeleteObject(BitmapPtr);
  271. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  272. initialization();
  273. Owner = null;
  274. WindowState = WindowState.Minimized;
  275. Visibility = Visibility.Hidden;
  276. ShowInTaskbar = false;
  277. //this.Hide();
  278. if (ChangeTextEvent != null)
  279. {
  280. k_hook.Stop();
  281. ChangeTextEvent("关闭窗口");
  282. }
  283. }
  284. #endregion 获取截图
  285. /// <summary>
  286. /// 获取时间戳
  287. /// </summary>
  288. /// <returns></returns>
  289. public string GetTimeStamp()
  290. {
  291. TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  292. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  293. }
  294. #region 退出截图
  295. public void OnCancel()
  296. {
  297. //Close();
  298. //ImageHelper.DeleteObject(BitmapPtr);
  299. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  300. initialization();
  301. Owner = null;
  302. WindowState = WindowState.Minimized;
  303. Visibility = Visibility.Hidden;
  304. ShowInTaskbar = false;
  305. //this.Hide();
  306. //_Bitmap.Dispose();
  307. }
  308. #endregion 退出截图
  309. #region 完成截图
  310. public void OnOK()
  311. {
  312. Hidden();
  313. Thread t = new Thread(new ThreadStart(() =>
  314. {
  315. Thread.Sleep(50);
  316. //Thread.Sleep(200);
  317. WpfHelper.SafeRun(() =>
  318. {
  319. /*var source = */
  320. GetCapture();
  321. //Shared.TeachingData.Source = source;
  322. //if (source != null)
  323. //{
  324. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  325. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  326. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  327. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  328. // ImageHelper.SaveToPng(source, path);
  329. // Shared.TeachingData.FilePath = path;
  330. // //Clipboard.SetImage(source);
  331. //}
  332. //Close();
  333. //if (ChangeTextEvent != null)
  334. //{
  335. // ChangeTextEvent("关闭窗口");
  336. //}
  337. });
  338. }))
  339. {
  340. IsBackground = true
  341. };
  342. t.Start();
  343. }
  344. #endregion 完成截图
  345. #region 截图前隐藏窗口
  346. private void Hidden()
  347. {
  348. //隐藏尺寸RGB框
  349. if (AppModel.Current.MaskTopHeight < 40)
  350. {
  351. SizeRGB.Visibility = Visibility.Collapsed;
  352. }
  353. int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
  354. if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
  355. {
  356. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  357. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  358. }
  359. MainImage.ZoomThumbVisibility = Visibility.Collapsed;
  360. }
  361. #endregion 截图前隐藏窗口
  362. #region 鼠标及键盘事件
  363. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  364. {
  365. if (_IsCapture)
  366. {
  367. return;
  368. }
  369. System.Windows.Point point = e.GetPosition(this);
  370. _X0 = point.X;
  371. _Y0 = point.Y;
  372. _IsMouseDown = true;
  373. Canvas.SetLeft(MainImage, _X0);
  374. Canvas.SetTop(MainImage, _Y0);
  375. AppModel.Current.MaskLeftWidth = _X0;
  376. AppModel.Current.MaskRightWidth = ScreenWidth - _X0;
  377. AppModel.Current.MaskTopHeight = _Y0;
  378. Show_Size.Visibility = Visibility.Visible;
  379. }
  380. private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  381. {
  382. if (!_IsMouseDown || _IsCapture)
  383. {
  384. return;
  385. }
  386. _IsMouseDown = false;
  387. if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
  388. {
  389. _IsCapture = true;
  390. ImageEditBar.Current.Visibility = Visibility.Visible;
  391. ImageEditBar.Current.ResetCanvas();
  392. Cursor = Cursors.Arrow;
  393. }
  394. }
  395. private void Window_MouseMove(object sender, MouseEventArgs e)
  396. {
  397. System.Windows.Point point = e.GetPosition(this);
  398. System.Windows.Point screenP = PointToScreen(point);
  399. try
  400. {
  401. AppModel.Current.ShowRGB = ImageHelper.GetRGB((int)screenP.X, (int)screenP.Y);
  402. }
  403. catch (Exception ex)
  404. {
  405. MessageWindow.Show(ex.Message);
  406. }
  407. if (_IsCapture)
  408. {
  409. return;
  410. }
  411. if (Show_RGB.Visibility == Visibility.Collapsed)
  412. {
  413. Show_RGB.Visibility = Visibility.Visible;
  414. }
  415. if (_IsMouseDown)
  416. {
  417. double w = point.X - _X0;
  418. double h = point.Y - _Y0;
  419. if (w < MinSize || h < MinSize)
  420. {
  421. return;
  422. }
  423. if (MainImage.Visibility == Visibility.Collapsed)
  424. {
  425. MainImage.Visibility = Visibility.Visible;
  426. }
  427. AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
  428. AppModel.Current.MaskTopWidth = w;
  429. AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
  430. AppModel.Current.ChangeShowSize();
  431. MainImage.Width = w;
  432. MainImage.Height = h;
  433. }
  434. else
  435. {
  436. AppModel.Current.ShowSizeLeft = point.X;
  437. AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
  438. }
  439. }
  440. private void Window_KeyDown(object sender, KeyEventArgs e)
  441. {
  442. if (e.Key == Key.Escape)
  443. {
  444. initialization();
  445. Owner = null;
  446. WindowState = WindowState.Minimized;
  447. Visibility = Visibility.Hidden;
  448. ShowInTaskbar = false;
  449. }
  450. }
  451. #endregion 鼠标及键盘事件
  452. /// <summary>
  453. /// 关闭时清除tool页记录的截图数据
  454. /// </summary>
  455. /// <param name="sender"></param>
  456. /// <param name="e"></param>
  457. private void Window_Closed(object sender, EventArgs e)
  458. {
  459. click_closeJietuWindowClick();
  460. }
  461. /// <summary>
  462. /// 非正常关闭时jietuWindow=null;
  463. /// </summary>
  464. /// <param name="sender"></param>
  465. /// <param name="e"></param>
  466. public delegate void CloseJietuWindowClick();
  467. /// <summary>
  468. /// 非正常关闭时jietuWindow=null;
  469. /// </summary>
  470. public event CloseJietuWindowClick click_closeJietuWindowClick;
  471. }
  472. }