星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

JieTuWindow.xaml.cs 18KB

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