星火微课系统客户端
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 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. 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. if (ChangeTextEvent != null)
  310. {
  311. k_hook.Stop();
  312. ChangeTextEvent("取消");
  313. }
  314. }
  315. #endregion 退出截图
  316. #region 完成截图
  317. public void OnOK()
  318. {
  319. Hidden();
  320. Thread t = new Thread(new ThreadStart(() =>
  321. {
  322. Thread.Sleep(50);
  323. //Thread.Sleep(200);
  324. WpfHelper.SafeRun(() =>
  325. {
  326. /*var source = */
  327. GetCapture();
  328. //Shared.TeachingData.Source = source;
  329. //if (source != null)
  330. //{
  331. // TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  332. // string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
  333. // string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
  334. // Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
  335. // ImageHelper.SaveToPng(source, path);
  336. // Shared.TeachingData.FilePath = path;
  337. // //Clipboard.SetImage(source);
  338. //}
  339. //Close();
  340. //if (ChangeTextEvent != null)
  341. //{
  342. // ChangeTextEvent("关闭窗口");
  343. //}
  344. });
  345. }))
  346. {
  347. IsBackground = true
  348. };
  349. t.Start();
  350. }
  351. #endregion 完成截图
  352. #region 截图前隐藏窗口
  353. private void Hidden()
  354. {
  355. //隐藏尺寸RGB框
  356. if (AppModel.Current.MaskTopHeight < 40)
  357. {
  358. SizeRGB.Visibility = Visibility.Collapsed;
  359. }
  360. int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
  361. if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
  362. {
  363. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  364. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  365. }
  366. MainImage.ZoomThumbVisibility = Visibility.Collapsed;
  367. }
  368. #endregion 截图前隐藏窗口
  369. #region 鼠标及键盘事件
  370. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  371. {
  372. if (_IsCapture)
  373. {
  374. return;
  375. }
  376. System.Windows.Point point = e.GetPosition(this);
  377. _X0 = point.X;
  378. _Y0 = point.Y;
  379. _IsMouseDown = true;
  380. Canvas.SetLeft(MainImage, _X0);
  381. Canvas.SetTop(MainImage, _Y0);
  382. AppModel.Current.MaskLeftWidth = _X0;
  383. AppModel.Current.MaskRightWidth = ScreenWidth - _X0;
  384. AppModel.Current.MaskTopHeight = _Y0;
  385. Show_Size.Visibility = Visibility.Visible;
  386. }
  387. private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  388. {
  389. if (!_IsMouseDown || _IsCapture)
  390. {
  391. return;
  392. }
  393. _IsMouseDown = false;
  394. if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
  395. {
  396. _IsCapture = true;
  397. ImageEditBar.Current.Visibility = Visibility.Visible;
  398. ImageEditBar.Current.ResetCanvas();
  399. Cursor = Cursors.Arrow;
  400. }
  401. }
  402. private void Window_MouseMove(object sender, MouseEventArgs e)
  403. {
  404. System.Windows.Point point = e.GetPosition(this);
  405. System.Windows.Point screenP = PointToScreen(point);
  406. try
  407. {
  408. AppModel.Current.ShowRGB = ImageHelper.GetRGB((int)screenP.X, (int)screenP.Y);
  409. }
  410. catch (Exception ex)
  411. {
  412. MessageWindow.Show(ex.Message);
  413. }
  414. if (_IsCapture)
  415. {
  416. return;
  417. }
  418. if (Show_RGB.Visibility == Visibility.Collapsed)
  419. {
  420. Show_RGB.Visibility = Visibility.Visible;
  421. }
  422. if (_IsMouseDown)
  423. {
  424. double w = point.X - _X0;
  425. double h = point.Y - _Y0;
  426. if (w < MinSize || h < MinSize)
  427. {
  428. return;
  429. }
  430. if (MainImage.Visibility == Visibility.Collapsed)
  431. {
  432. MainImage.Visibility = Visibility.Visible;
  433. }
  434. AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
  435. AppModel.Current.MaskTopWidth = w;
  436. AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
  437. AppModel.Current.ChangeShowSize();
  438. MainImage.Width = w;
  439. MainImage.Height = h;
  440. }
  441. else
  442. {
  443. AppModel.Current.ShowSizeLeft = point.X;
  444. AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
  445. }
  446. }
  447. private void Window_KeyDown(object sender, KeyEventArgs e)
  448. {
  449. if (e.Key == Key.Escape)
  450. {
  451. initialization();
  452. Owner = null;
  453. WindowState = WindowState.Minimized;
  454. Visibility = Visibility.Hidden;
  455. ShowInTaskbar = false;
  456. }
  457. }
  458. #endregion 鼠标及键盘事件
  459. /// <summary>
  460. /// 关闭时清除tool页记录的截图数据
  461. /// </summary>
  462. /// <param name="sender"></param>
  463. /// <param name="e"></param>
  464. private void Window_Closed(object sender, EventArgs e)
  465. {
  466. click_closeJietuWindowClick();
  467. }
  468. /// <summary>
  469. /// 非正常关闭时jietuWindow=null;
  470. /// </summary>
  471. /// <param name="sender"></param>
  472. /// <param name="e"></param>
  473. public delegate void CloseJietuWindowClick();
  474. /// <summary>
  475. /// 非正常关闭时jietuWindow=null;
  476. /// </summary>
  477. public event CloseJietuWindowClick click_closeJietuWindowClick;
  478. }
  479. }