123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- using ComeCapture.Controls;
- using ComeCapture.Helpers;
- using ComeCapture.Models;
-
- using Common.system;
-
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
-
- using XHWK.WKTool;
-
- using static XHWK.WKTool.MainWindow;
-
- namespace ComeCapture
- {
- /// <summary>
- /// 截图 MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class JieTuWindow
- {
- public static double ScreenWidth = SystemParameters.PrimaryScreenWidth;
- public static double ScreenHeight = SystemParameters.PrimaryScreenHeight;
- public static double ScreenScale = 1;
- public static int MinSize = 10;
- internal double pwidth = SystemParameters.PrimaryScreenWidth;
-
- internal double pHeight = SystemParameters.PrimaryScreenHeight;
-
- //画图注册名称集合
- public List<NameAndLimit> list = new List<NameAndLimit>();
-
- //画图注册名称
- public int num = 1;
-
- //是否截图开始
- private bool _isMouseDown;
-
- //是否截图完毕
- private bool _isCapture;
-
- private double _x0;
- private double _y0;
-
- /// <summary>
- /// 键盘钩子
- /// </summary>
- private readonly KeyboardHookCommon _kHook;
-
- //private IntPtr BitmapPtr = IntPtr.Zero;
- //private IntPtr JieTuBitmapPtr = IntPtr.Zero;
- /// <summary>
- /// 截图
- /// </summary>
- public JieTuWindow()
- {
- _current = this;
- InitializeComponent();
- _kHook = new KeyboardHookCommon();
- _kHook.KeyDownEvent += K_hook_KeyDownEvent;
- DataContext = new AppModel();
- Initialization();
- Screenshot();
- MaxWindow();
- MaskLeft.Height = ScreenHeight;
- MaskRight.Height = ScreenHeight;
- //计算Windows项目缩放比例
- ScreenHelper.ResetScreenScale();
- //RemoveControl();
- //AllowsTransparency = "True"
- }
-
- public void Screenshot()
- {
- string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
- if (!Directory.Exists(tempPath))
- {
- Directory.CreateDirectory(tempPath);
- }
- if (ImageHelper.GetScreenshot(
- new Rectangle(
- 0,
- 0,
- 0,
- 0
- ),
- null,
- true,
- out BitmapImage bitmap
- ))
- {
- Background = new ImageBrush(bitmap);
- }
- }
-
- public void Initialization()
- {
- Background = null;
- _isMouseDown = false;
- _isCapture = false;
- ImageEditBar.Current.Visibility = Visibility.Collapsed;
- SizeColorBar.Current.Visibility = Visibility.Collapsed;
- SizeRgb.Visibility = Visibility.Collapsed;
- MainImage.Visibility = Visibility.Collapsed;
- DataContext = new AppModel();
- WpfHelper.MainDispatcher = Dispatcher;
- }
-
- /// <summary>
- /// 初始化快捷键
- /// </summary>
- public void InitializeKeyDownEvent()
- {
- _kHook.Start(); //安装键盘钩子
- }
-
- /// <summary>
- /// 快捷键
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e)
- {
- if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
- {
- _kHook.Stop();
- Current.OnCancel();
- App.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent();
- App.W_XHMicroLessonSystemWindow.WindowType();
- Hide();
- }
- }
-
- #region 属性 Current
-
- private static JieTuWindow _current;
-
- public static JieTuWindow Current => _current;
-
- #endregion 属性 Current
-
- #region 全屏+置顶
-
- private void MaxWindow()
- {
- Left = 0;
- Top = 0;
- Width = ScreenWidth;
- Height = ScreenHeight;
- Activate();
- }
-
- #endregion 全屏+置顶
-
- #region 注册画图
-
- public static void Register(object control)
- {
- string name = "Draw" + _current.num;
- _current.MainCanvas.RegisterName(name, control);
- _current.list.Add(new NameAndLimit(name));
- _current.num++;
- }
-
- #endregion 注册画图
-
- #region 截图区域添加画图
-
- public static void AddControl(UIElement e)
- {
- _current.MainCanvas.Children.Add(e);
- }
-
- #endregion 截图区域添加画图
-
- #region 截图区域撤回画图
-
- public static void RemoveControl(UIElement e)
- {
- _current.MainCanvas.Children.Remove(e);
- }
-
- #endregion 截图区域撤回画图
-
- #region 撤销
-
- public void OnRevoke()
- {
- if (list.Count > 0)
- {
- string name = list[list.Count - 1].Name;
- object obj = MainCanvas.FindName(name);
- if (obj != null)
- {
- MainCanvas.Children.Remove(obj as UIElement);
- MainCanvas.UnregisterName(name);
- list.RemoveAt(list.Count - 1);
- MainImage.Limit = list.Count == 0 ? new Limit() : list[list.Count - 1].Limit;
- }
- }
- }
-
- #endregion 撤销
-
- #region 保存
-
- public void OnSave()
- {
- //var sfd = new Microsoft.Win32.SaveFileDialog
- //{
- // FileName = "截图" + DateTime.Now.ToString("yyyyMMddhhmmss"),
- // Filter = "png|*.png",
- // AddExtension = true,
- // RestoreDirectory = true
- //};
- //if (sfd.ShowDialog() == true)
- //{
- // Hidden();
- // Thread t = new Thread(new ThreadStart(() =>
- // {
- // Thread.Sleep(200);
- // WpfHelper.SafeRun(() =>
- // {
- // var source = GetCapture();
- // if (source != null)
- // {
- // ImageHelper.SaveToPng(source, sfd.FileName);
- // Shared.TeachingData.FilePath = sfd.FileName;
- // //Thread.Sleep(200);
- // }
- // Close();
- // if (ChangeTextEvent != null)
- // {
- // ChangeTextEvent("关闭窗口");
- // }
- // //Go(sfd.FileName);
- // });
- // }))
- // {
- // IsBackground = true
- // };
- // t.Start();
- //}
- }
-
- #endregion 保存
-
- //定义事件
- public event ChangeTextHandler ChangeTextEvent;
-
- #region 获取截图
-
- private void GetCapture()
- {
- string time = GetTimeStamp();
- string tempPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
- if (!Directory.Exists(tempPath))
- {
- Directory.CreateDirectory(tempPath);
- }
- App.ImgPath = string.Empty;
- string imagePath = Path.Combine(tempPath, time + ".jpg");
- App.ImgPath = imagePath;
- //LogHelper.Loginfo.Info(imagePath);
- //this.Close();
- //string imagePath = ImageHelper.GetImagePath(out string serverSavePath);
-
- //BitmapSource bitmap = ImageHelper.GetBitmapSource((int)AppModel.Current.MaskLeftWidth + 1, (int)AppModel.Current.MaskTopHeight + 1, (int)MainImage.ActualWidth - 2, (int)MainImage.ActualHeight - 2, out JieTuBitmapPtr);
- /* BitmapImage bitmap = */
- 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 _
- );
- _isMouseDown = false;
- _isCapture = false;
- ImageEditBar.Current.Visibility = Visibility.Collapsed;
- SizeColorBar.Current.Visibility = Visibility.Collapsed;
- SizeRgb.Visibility = Visibility.Collapsed;
- MainImage.Visibility = Visibility.Collapsed;
- DataContext = new AppModel();
- WpfHelper.MainDispatcher = Dispatcher;
- MaxWindow();
- MaskLeft.Height = ScreenHeight;
- MaskRight.Height = ScreenHeight;
- //计算Windows项目缩放比例
- ScreenHelper.ResetScreenScale();
- Initialization();
- Owner = null;
- WindowState = WindowState.Minimized;
- Visibility = Visibility.Hidden;
- ShowInTaskbar = false;
- //this.Hide();
- if (ChangeTextEvent != null)
- {
- _kHook.Stop();
- ChangeTextEvent("关闭窗口");
- }
- }
-
- #endregion 获取截图
-
- /// <summary>
- /// 获取时间戳
- /// </summary>
- /// <returns></returns>
- public string GetTimeStamp()
- {
- TimeSpan ts = DateTime.Now -
- new DateTime(
- 1970,
- 1,
- 1,
- 0,
- 0,
- 0,
- 0
- );
- return Convert.ToInt64(ts.TotalMilliseconds).ToString();
- }
-
- #region 退出截图
-
- public void OnCancel()
- {
- //Close();
- //ImageHelper.DeleteObject(BitmapPtr);
- //ImageHelper.DeleteObject(JieTuBitmapPtr);
- Initialization();
- Owner = null;
- WindowState = WindowState.Minimized;
- Visibility = Visibility.Hidden;
- ShowInTaskbar = false;
- //this.Hide();
- //_Bitmap.Dispose();
- if (ChangeTextEvent != null)
- {
- _kHook.Stop();
- ChangeTextEvent("取消");
- }
- }
-
- #endregion 退出截图
-
- #region 完成截图
-
- public void OnOK()
- {
- Hidden();
- Thread t = new Thread(
- () =>
- {
- Thread.Sleep(50);
- WpfHelper.SafeRun(GetCapture);
- }
- ) { IsBackground = true };
- t.Start();
- }
-
- #endregion 完成截图
-
- #region 截图前隐藏窗口
-
- private void Hidden()
- {
- //隐藏尺寸RGB框
- if (AppModel.Current.MaskTopHeight < 40)
- {
- SizeRgb.Visibility = Visibility.Collapsed;
- }
- int need = SizeColorBar.Current.Selected == Tool.Null ? 30 : 67;
- if (AppModel.Current.MaskBottomHeight < need && AppModel.Current.MaskTopHeight < need)
- {
- ImageEditBar.Current.Visibility = Visibility.Collapsed;
- SizeColorBar.Current.Visibility = Visibility.Collapsed;
- }
- MainImage.ZoomThumbVisibility = Visibility.Collapsed;
- }
-
- #endregion 截图前隐藏窗口
-
- #region 鼠标及键盘事件
-
- private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (_isCapture)
- {
- return;
- }
- System.Windows.Point point = e.GetPosition(this);
- _x0 = point.X;
- _y0 = point.Y;
- _isMouseDown = true;
- Canvas.SetLeft(MainImage, _x0);
- Canvas.SetTop(MainImage, _y0);
- AppModel.Current.MaskLeftWidth = _x0;
- AppModel.Current.MaskRightWidth = ScreenWidth - _x0;
- AppModel.Current.MaskTopHeight = _y0;
- ShowSize.Visibility = Visibility.Visible;
- }
-
- private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (!_isMouseDown || _isCapture)
- {
- return;
- }
- _isMouseDown = false;
- if (MainImage.Width >= MinSize && MainImage.Height >= MinSize)
- {
- _isCapture = true;
- ImageEditBar.Current.Visibility = Visibility.Visible;
- ImageEditBar.Current.ResetCanvas();
- Cursor = Cursors.Arrow;
- }
- }
-
- private void Window_MouseMove(object sender, MouseEventArgs e)
- {
- System.Windows.Point point = e.GetPosition(this);
- System.Windows.Point screenP = PointToScreen(point);
- try
- {
- AppModel.Current.ShowRGB = ImageHelper.GetRgb((int)screenP.X, (int)screenP.Y);
- }
- catch (Exception ex)
- {
- MessageWindow.Show(ex.Message);
- }
- if (_isCapture)
- {
- return;
- }
- if (ShowRgb.Visibility == Visibility.Collapsed)
- {
- ShowRgb.Visibility = Visibility.Visible;
- }
- if (_isMouseDown)
- {
- double w = point.X - _x0;
- double h = point.Y - _y0;
- if (w < MinSize || h < MinSize)
- {
- return;
- }
- if (MainImage.Visibility == Visibility.Collapsed)
- {
- MainImage.Visibility = Visibility.Visible;
- }
- AppModel.Current.MaskRightWidth = ScreenWidth - point.X;
- AppModel.Current.MaskTopWidth = w;
- AppModel.Current.MaskBottomHeight = ScreenHeight - point.Y;
- AppModel.Current.ChangeShowSize();
- MainImage.Width = w;
- MainImage.Height = h;
- }
- else
- {
- AppModel.Current.ShowSizeLeft = point.X;
- AppModel.Current.ShowSizeTop = ScreenHeight - point.Y < 30 ? point.Y - 30 : point.Y + 10;
- }
- }
-
- private void Window_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Escape)
- {
- Initialization();
- Owner = null;
- WindowState = WindowState.Minimized;
- Visibility = Visibility.Hidden;
- ShowInTaskbar = false;
- }
- }
-
- #endregion 鼠标及键盘事件
-
- /// <summary>
- /// 关闭时清除tool页记录的截图数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Window_Closed(object sender, EventArgs e)
- {
- if (ClickCloseJietuWindowClick != null)
- {
- ClickCloseJietuWindowClick();
- }
- }
-
- /// <summary>
- /// 非正常关闭时jietuWindow=null;
- /// </summary>
- public delegate void CloseJietuWindowClick();
-
- /// <summary>
- /// 非正常关闭时jietuWindow=null;
- /// </summary>
- public event CloseJietuWindowClick ClickCloseJietuWindowClick;
- }
- }
|