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 { /// /// 截图 MainWindow.xaml 的交互逻辑 /// 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 list = new List(); //画图注册名称 public int num = 1; //是否截图开始 private bool _isMouseDown; //是否截图完毕 private bool _isCapture; private double _x0; private double _y0; /// /// 键盘钩子 /// private readonly KeyboardHookCommon _kHook; //private IntPtr BitmapPtr = IntPtr.Zero; //private IntPtr JieTuBitmapPtr = IntPtr.Zero; /// /// 截图 /// 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; } /// /// 初始化快捷键 /// public void InitializeKeyDownEvent() { _kHook.Start(); //安装键盘钩子 } /// /// 快捷键 /// /// /// 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 获取截图 /// /// 获取时间戳 /// /// 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 鼠标及键盘事件 /// /// 关闭时清除tool页记录的截图数据 /// /// /// private void Window_Closed(object sender, EventArgs e) { if (ClickCloseJietuWindowClick != null) { ClickCloseJietuWindowClick(); } } /// /// 非正常关闭时jietuWindow=null; /// public delegate void CloseJietuWindowClick(); /// /// 非正常关闭时jietuWindow=null; /// public event CloseJietuWindowClick ClickCloseJietuWindowClick; } }