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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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.MainWindow;
  17. namespace ComeCapture
  18. {
  19. /// <summary>
  20. /// 截图 MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class JieTuWindow
  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;
  36. //是否截图完毕
  37. private bool _isCapture;
  38. private double _x0;
  39. private double _y0;
  40. /// <summary>
  41. /// 键盘钩子
  42. /// </summary>
  43. private readonly KeyboardHookCommon _kHook;
  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. _kHook = new KeyboardHookCommon();
  54. _kHook.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. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  69. if (!Directory.Exists(tempPath))
  70. {
  71. Directory.CreateDirectory(tempPath);
  72. }
  73. if (ImageHelper.GetScreenshot(
  74. new Rectangle(
  75. 0,
  76. 0,
  77. 0,
  78. 0
  79. ),
  80. null,
  81. true,
  82. out BitmapImage bitmap
  83. ))
  84. {
  85. Background = new ImageBrush(bitmap);
  86. }
  87. }
  88. public void Initialization()
  89. {
  90. Background = null;
  91. _isMouseDown = false;
  92. _isCapture = false;
  93. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  94. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  95. SizeRgb.Visibility = Visibility.Collapsed;
  96. MainImage.Visibility = Visibility.Collapsed;
  97. DataContext = new AppModel();
  98. WpfHelper.MainDispatcher = Dispatcher;
  99. }
  100. /// <summary>
  101. /// 初始化快捷键
  102. /// </summary>
  103. public void InitializeKeyDownEvent()
  104. {
  105. _kHook.Start(); //安装键盘钩子
  106. }
  107. /// <summary>
  108. /// 快捷键
  109. /// </summary>
  110. /// <param name="sender"></param>
  111. /// <param name="e"></param>
  112. private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e)
  113. {
  114. if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
  115. {
  116. _kHook.Stop();
  117. Current.OnCancel();
  118. App.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent();
  119. App.W_XHMicroLessonSystemWindow.WindowType();
  120. Hide();
  121. }
  122. }
  123. #region 属性 Current
  124. private static JieTuWindow _current;
  125. public static JieTuWindow Current => _current;
  126. #endregion 属性 Current
  127. #region 全屏+置顶
  128. private void MaxWindow()
  129. {
  130. Left = 0;
  131. Top = 0;
  132. Width = ScreenWidth;
  133. Height = ScreenHeight;
  134. Activate();
  135. }
  136. #endregion 全屏+置顶
  137. #region 注册画图
  138. public static void Register(object control)
  139. {
  140. string name = "Draw" + _current.num;
  141. _current.MainCanvas.RegisterName(name, control);
  142. _current.list.Add(new NameAndLimit(name));
  143. _current.num++;
  144. }
  145. #endregion 注册画图
  146. #region 截图区域添加画图
  147. public static void AddControl(UIElement e)
  148. {
  149. _current.MainCanvas.Children.Add(e);
  150. }
  151. #endregion 截图区域添加画图
  152. #region 截图区域撤回画图
  153. public static void RemoveControl(UIElement e)
  154. {
  155. _current.MainCanvas.Children.Remove(e);
  156. }
  157. #endregion 截图区域撤回画图
  158. #region 撤销
  159. public void OnRevoke()
  160. {
  161. if (list.Count > 0)
  162. {
  163. string name = list[list.Count - 1].Name;
  164. object obj = MainCanvas.FindName(name);
  165. if (obj != null)
  166. {
  167. MainCanvas.Children.Remove(obj as UIElement);
  168. MainCanvas.UnregisterName(name);
  169. list.RemoveAt(list.Count - 1);
  170. MainImage.Limit = list.Count == 0 ? new Limit() : list[list.Count - 1].Limit;
  171. }
  172. }
  173. }
  174. #endregion 撤销
  175. #region 保存
  176. public void OnSave()
  177. {
  178. //var sfd = new Microsoft.Win32.SaveFileDialog
  179. //{
  180. // FileName = "截图" + DateTime.Now.ToString("yyyyMMddhhmmss"),
  181. // Filter = "png|*.png",
  182. // AddExtension = true,
  183. // RestoreDirectory = true
  184. //};
  185. //if (sfd.ShowDialog() == true)
  186. //{
  187. // Hidden();
  188. // Thread t = new Thread(new ThreadStart(() =>
  189. // {
  190. // Thread.Sleep(200);
  191. // WpfHelper.SafeRun(() =>
  192. // {
  193. // var source = GetCapture();
  194. // if (source != null)
  195. // {
  196. // ImageHelper.SaveToPng(source, sfd.FileName);
  197. // Shared.TeachingData.FilePath = sfd.FileName;
  198. // //Thread.Sleep(200);
  199. // }
  200. // Close();
  201. // if (ChangeTextEvent != null)
  202. // {
  203. // ChangeTextEvent("关闭窗口");
  204. // }
  205. // //Go(sfd.FileName);
  206. // });
  207. // }))
  208. // {
  209. // IsBackground = true
  210. // };
  211. // t.Start();
  212. //}
  213. }
  214. #endregion 保存
  215. //定义事件
  216. public event ChangeTextHandler ChangeTextEvent;
  217. #region 获取截图
  218. private void GetCapture()
  219. {
  220. string time = GetTimeStamp();
  221. string tempPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
  222. if (!Directory.Exists(tempPath))
  223. {
  224. Directory.CreateDirectory(tempPath);
  225. }
  226. App.ImgPath = string.Empty;
  227. string imagePath = Path.Combine(tempPath, time + ".jpg");
  228. App.ImgPath = imagePath;
  229. //LogHelper.Loginfo.Info(imagePath);
  230. //this.Close();
  231. //string imagePath = ImageHelper.GetImagePath(out string serverSavePath);
  232. //BitmapSource bitmap = ImageHelper.GetBitmapSource((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2, out JieTuBitmapPtr);
  233. /* BitmapImage bitmap = */
  234. ImageHelper.GetScreenshot(
  235. new Rectangle(
  236. (int)AppModel.Current.MaskLeftWidth + 1,
  237. (int)AppModel.Current.MaskTopHeight + 1,
  238. (int)MainImage.ActualWidth - 2,
  239. (int)MainImage.ActualHeight - 2
  240. ),
  241. imagePath,
  242. true,
  243. out BitmapImage _
  244. );
  245. _isMouseDown = false;
  246. _isCapture = false;
  247. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  248. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  249. SizeRgb.Visibility = Visibility.Collapsed;
  250. MainImage.Visibility = Visibility.Collapsed;
  251. DataContext = new AppModel();
  252. WpfHelper.MainDispatcher = Dispatcher;
  253. MaxWindow();
  254. MaskLeft.Height = ScreenHeight;
  255. MaskRight.Height = ScreenHeight;
  256. //计算Windows项目缩放比例
  257. ScreenHelper.ResetScreenScale();
  258. Initialization();
  259. Owner = null;
  260. WindowState = WindowState.Minimized;
  261. Visibility = Visibility.Hidden;
  262. ShowInTaskbar = false;
  263. //this.Hide();
  264. if (ChangeTextEvent != null)
  265. {
  266. _kHook.Stop();
  267. ChangeTextEvent("关闭窗口");
  268. }
  269. }
  270. #endregion 获取截图
  271. /// <summary>
  272. /// 获取时间戳
  273. /// </summary>
  274. /// <returns></returns>
  275. public string GetTimeStamp()
  276. {
  277. TimeSpan ts = DateTime.Now -
  278. new DateTime(
  279. 1970,
  280. 1,
  281. 1,
  282. 0,
  283. 0,
  284. 0,
  285. 0
  286. );
  287. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  288. }
  289. #region 退出截图
  290. public void OnCancel()
  291. {
  292. //Close();
  293. //ImageHelper.DeleteObject(BitmapPtr);
  294. //ImageHelper.DeleteObject(JieTuBitmapPtr);
  295. Initialization();
  296. Owner = null;
  297. WindowState = WindowState.Minimized;
  298. Visibility = Visibility.Hidden;
  299. ShowInTaskbar = false;
  300. //this.Hide();
  301. //_Bitmap.Dispose();
  302. if (ChangeTextEvent != null)
  303. {
  304. _kHook.Stop();
  305. ChangeTextEvent("取消");
  306. }
  307. }
  308. #endregion 退出截图
  309. #region 完成截图
  310. public void OnOK()
  311. {
  312. Hidden();
  313. Thread t = new Thread(
  314. () =>
  315. {
  316. Thread.Sleep(50);
  317. WpfHelper.SafeRun(GetCapture);
  318. }
  319. ) { IsBackground = true };
  320. t.Start();
  321. }
  322. #endregion 完成截图
  323. #region 截图前隐藏窗口
  324. private void Hidden()
  325. {
  326. //隐藏尺寸RGB框
  327. if (AppModel.Current.MaskTopHeight < 40)
  328. {
  329. SizeRgb.Visibility = Visibility.Collapsed;
  330. }
  331. int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
  332. if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
  333. {
  334. ImageEditBar.Current.Visibility = Visibility.Collapsed;
  335. SizeColorBar.Current.Visibility = Visibility.Collapsed;
  336. }
  337. MainImage.ZoomThumbVisibility = Visibility.Collapsed;
  338. }
  339. #endregion 截图前隐藏窗口
  340. #region 鼠标及键盘事件
  341. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  342. {
  343. if (_isCapture)
  344. {
  345. return;
  346. }
  347. System.Windows.Point point = e.GetPosition(this);
  348. _x0 = point.X;
  349. _y0 = point.Y;
  350. _isMouseDown = true;
  351. Canvas.SetLeft(MainImage, _x0);
  352. Canvas.SetTop(MainImage, _y0);
  353. AppModel.Current.MaskLeftWidth = _x0;
  354. AppModel.Current.MaskRightWidth = ScreenWidth - _x0;
  355. AppModel.Current.MaskTopHeight = _y0;
  356. ShowSize.Visibility = Visibility.Visible;
  357. }
  358. private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  359. {
  360. if (!_isMouseDown || _isCapture)
  361. {
  362. return;
  363. }
  364. _isMouseDown = false;
  365. if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
  366. {
  367. _isCapture = true;
  368. ImageEditBar.Current.Visibility = Visibility.Visible;
  369. ImageEditBar.Current.ResetCanvas();
  370. Cursor = Cursors.Arrow;
  371. }
  372. }
  373. private void Window_MouseMove(object sender, MouseEventArgs e)
  374. {
  375. System.Windows.Point point = e.GetPosition(this);
  376. System.Windows.Point screenP = PointToScreen(point);
  377. try
  378. {
  379. AppModel.Current.ShowRGB = ImageHelper.GetRgb((int)screenP.X, (int)screenP.Y);
  380. }
  381. catch (Exception ex)
  382. {
  383. MessageWindow.Show(ex.Message);
  384. }
  385. if (_isCapture)
  386. {
  387. return;
  388. }
  389. if (ShowRgb.Visibility == Visibility.Collapsed)
  390. {
  391. ShowRgb.Visibility = Visibility.Visible;
  392. }
  393. if (_isMouseDown)
  394. {
  395. double w = point.X - _x0;
  396. double h = point.Y - _y0;
  397. if (w < MinSize || h < MinSize)
  398. {
  399. return;
  400. }
  401. if (MainImage.Visibility == Visibility.Collapsed)
  402. {
  403. MainImage.Visibility = Visibility.Visible;
  404. }
  405. AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
  406. AppModel.Current.MaskTopWidth = w;
  407. AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
  408. AppModel.Current.ChangeShowSize();
  409. MainImage.Width = w;
  410. MainImage.Height = h;
  411. }
  412. else
  413. {
  414. AppModel.Current.ShowSizeLeft = point.X;
  415. AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
  416. }
  417. }
  418. private void Window_KeyDown(object sender, KeyEventArgs e)
  419. {
  420. if (e.Key == Key.Escape)
  421. {
  422. Initialization();
  423. Owner = null;
  424. WindowState = WindowState.Minimized;
  425. Visibility = Visibility.Hidden;
  426. ShowInTaskbar = false;
  427. }
  428. }
  429. #endregion 鼠标及键盘事件
  430. /// <summary>
  431. /// 关闭时清除tool页记录的截图数据
  432. /// </summary>
  433. /// <param name="sender"></param>
  434. /// <param name="e"></param>
  435. private void Window_Closed(object sender, EventArgs e)
  436. {
  437. if (ClickCloseJietuWindowClick != null)
  438. {
  439. ClickCloseJietuWindowClick();
  440. }
  441. }
  442. /// <summary>
  443. /// 非正常关闭时jietuWindow=null;
  444. /// </summary>
  445. public delegate void CloseJietuWindowClick();
  446. /// <summary>
  447. /// 非正常关闭时jietuWindow=null;
  448. /// </summary>
  449. public event CloseJietuWindowClick ClickCloseJietuWindowClick;
  450. }
  451. }