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 : Window
{
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 = false;
//是否截图完毕
private bool _IsCapture = false;
private double _X0 = 0;
private double _Y0 = 0;
///
/// 键盘钩子
///
private KeyboardHookCommon k_hook;
//private IntPtr BitmapPtr = IntPtr.Zero;
//private IntPtr JieTuBitmapPtr = IntPtr.Zero;
///
/// 截图
///
public JieTuWindow()
{
_Current = this;
InitializeComponent();
k_hook = new KeyboardHookCommon();
k_hook.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()
{
//IntPtr BitmapPtr = IntPtr.Zero;
//Background = new ImageBrush(ImageHelper.GetFullBitmapSource(out BitmapPtr));
string time = GetTimeStamp();
string tempPath = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
string imagePath = Path.Combine(tempPath, time + ".jpg");
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()
{
k_hook.Start();//安装键盘钩子
}
///
/// 快捷键
///
///
///
private void K_hook_KeyDownEvent(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
{
k_hook.Stop();
Current.OnCancel();
APP.W_XHMicroLessonSystemWindow.InitializeKeyDownEvent();
APP.W_XHMicroLessonSystemWindow.WindowType();
Hide();
}
}
#region 属性 Current
private static JieTuWindow _Current = null;
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.WriteInfoLog(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 bitmapimg);
_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();
//ImageHelper.GetScreenshot(new Rectangle(0, 0, 0, 0), imagePath, true, out BitmapImage bitmap);//zxy
//Background = new ImageBrush(bitmap);
WpfHelper.MainDispatcher = Dispatcher;
MaxWindow();
MaskLeft.Height = ScreenHeight;
MaskRight.Height = ScreenHeight;
//计算Windows项目缩放比例
ScreenHelper.ResetScreenScale();
//if (bitmap != null)
//{
// //Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = serverSavePath;
// //Shared.TeachingData.FilePath = imagePath;
//}
///修改为统一截图时存储到服务器 修改人:赵耀 修改时间:2020年8月11日
//if (bitmap != null)
//{
// TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
// string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
// string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
// Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
// Shared.TeachingData.FilePath = path;
// FileInfo fi = new FileInfo(path); var di = fi.Directory; if (!di.Exists) {
// di.Create();
//}
// using (var fs = System.IO.File.Create(path))
// {
// BitmapEncoder encoder = new PngBitmapEncoder();
// encoder.Frames.Add(BitmapFrame.Create(bitmap));
// encoder.Save(fs);
// bitmap = null;
// encoder = null;
// }
//}
//Dispose();
//GC.Collect();
//ImageHelper.DeleteObject(BitmapPtr);
//ImageHelper.DeleteObject(JieTuBitmapPtr);
initialization();
Owner = null;
WindowState = WindowState.Minimized;
Visibility = Visibility.Hidden;
ShowInTaskbar = false;
//this.Hide();
if (ChangeTextEvent != null)
{
k_hook.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)
{
k_hook.Stop();
ChangeTextEvent("取消");
}
}
#endregion 退出截图
#region 完成截图
public void OnOK()
{
Hidden();
Thread t = new Thread(new ThreadStart(() =>
{
Thread.Sleep(50);
//Thread.Sleep(200);
WpfHelper.SafeRun(() =>
{
/*var source = */
GetCapture();
//Shared.TeachingData.Source = source;
//if (source != null)
//{
// TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
// string httpurl = Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".png";
// string path = ZConfig.classRoomImagePath() + httpurl.Substring(httpurl.LastIndexOf('/') + 1);
// Shared.TeachingData.REQ_InteractiveLaunch.teacherpic = ZConfig.dbImagePath() + httpurl;
// ImageHelper.SaveToPng(source, path);
// Shared.TeachingData.FilePath = path;
// //Clipboard.SetImage(source);
//}
//Close();
//if (ChangeTextEvent != null)
//{
// ChangeTextEvent("关闭窗口");
//}
});
}))
{
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;
Show_Size.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 (Show_RGB.Visibility == Visibility.Collapsed)
{
Show_RGB.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)
{
click_closeJietuWindowClick();
}
///
/// 非正常关闭时jietuWindow=null;
///
///
///
public delegate void CloseJietuWindowClick();
///
/// 非正常关闭时jietuWindow=null;
///
public event CloseJietuWindowClick click_closeJietuWindowClick;
}
}