星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JieTuWindow.xaml.cs 18KB

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