using System; using System.Collections.Generic; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using XHWK.Model; namespace XHWK.WKTool { /// /// 录屏画板 /// public partial class PracticeWindow : Window { //声明一个 DrawingAttributes 类型的变量 private DrawingAttributes drawingAttributes; private ViewModel viewModel; /// /// 0 画笔 1 矩形 2 圆形 /// private int flg = 0; /// /// 当前画笔颜色 /// private Color Colour = Colors.Red; /// /// 当前画笔宽 /// private int Wit = 2; /// /// 当前画笔高 /// private int Hei = 2; public PracticeWindow() { InitializeComponent(); } public void Initialize(string _imgPath) { flg = 0; blackboard_canvas.Strokes.Clear(); blackboard_canvas.UseCustomCursor = true; //blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByStroke; if (File.Exists(_imgPath)) { imgCanvas.Source = new BitmapImage(new Uri(_imgPath)); } //创建 DrawingAttributes 类的一个实例 drawingAttributes = new DrawingAttributes(); //drawingAttributes.StylusTip = StylusTip.Rectangle; //drawingAttributes.IsHighlighter = false; //drawingAttributes.IgnorePressure = true; ////将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用 ////InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型 blackboard_canvas.DefaultDrawingAttributes = drawingAttributes; Pen(); //blackboard_canvas.Cursor = Cursors.Pen; //Cursor cus = new Cursor(@"G:\06.cur"); //blackboard_canvas.Cursor = cus; viewModel = new ViewModel { InkStrokes = new StrokeCollection(), }; DataContext = viewModel; } /// /// 画笔颜色事件 白色 /// /// /// public void White() { //flg = 0; drawingAttributes.Color = Colors.White; Colour = Colors.White; } /// /// 画笔颜色事件 红色 /// /// /// public void Red() { //flg = 0; //设置 DrawingAttributes 的 Color 属性设置颜色 drawingAttributes.Color = Colors.Red; Colour = Colors.Red; } /// /// 画笔颜色事件 黑色 /// /// /// public void Gray() { //flg = 0; drawingAttributes.Color = Colors.Black; Colour = Colors.Black; } /// /// 画笔颜色事件 青色 /// /// /// public void CyanBlue() { //flg = 0; drawingAttributes.Color = Colors.LimeGreen; Colour = Colors.LimeGreen; } /// /// 画笔颜色事件 黄色 /// /// /// public void Yellow() { //flg = 0; drawingAttributes.Color = Colors.Gold; Colour = Colors.Gold; } /// /// 画笔颜色事件 蓝色 /// /// /// public void Blue() { //flg = 0; drawingAttributes.Color = Colors.DeepSkyBlue; Colour = Colors.DeepSkyBlue; } /// /// 画笔粗细事件 细 /// /// /// public void Fine() { //flg = 0; drawingAttributes.Width = 1; drawingAttributes.Height = 1; Wit = 1; Hei = 1; } /// /// 画笔粗细事件 中 /// /// /// public void In() { //flg = 0; drawingAttributes.Width = 3; drawingAttributes.Height = 3; Wit = 3; Hei = 3; } /// /// 画笔粗细事件 粗 /// /// /// public void Crude() { //flg = 0; drawingAttributes.Width = 5; drawingAttributes.Height = 5; Wit = 5; Hei = 5; } /// /// 橡皮 /// public void Eraser() { flg = 0; //this.type = ZPenType.Erase; blackboard_canvas.UseCustomCursor = false; blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint; blackboard_canvas.EraserShape = new EllipseStylusShape(64, 64, 0); } /// /// 画笔 /// public void Pen() { flg = 0; blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink; blackboard_canvas.UseCustomCursor = true; drawingAttributes.FitToCurve = true; drawingAttributes.IgnorePressure = false; blackboard_canvas.Cursor = Cursors.Pen; } /// /// 圆 /// public void Round() { flg = 2; drawingAttributes = new DrawingAttributes { Color = Colour, Width = Wit, Height = Hei, StylusTip = StylusTip.Rectangle, //FitToCurve = true, IsHighlighter = false, IgnorePressure = true, }; blackboard_canvas.DefaultDrawingAttributes = drawingAttributes; blackboard_canvas.EditingMode = InkCanvasEditingMode.None; blackboard_canvas.Cursor = Cursors.Cross; } /// /// 矩形 /// public void Rectangle() { flg = 1; drawingAttributes = new DrawingAttributes { Color = Colour, Width = Wit, Height = Hei, StylusTip = StylusTip.Rectangle, //FitToCurve = true, IsHighlighter = false, IgnorePressure = true, }; blackboard_canvas.DefaultDrawingAttributes = drawingAttributes; blackboard_canvas.EditingMode = InkCanvasEditingMode.None; blackboard_canvas.Cursor = Cursors.Cross; } private System.Windows.Point iniP; private void blackboard_canvas_MouseDown(object sender, MouseButtonEventArgs e) { if (flg != 0) { if (e.LeftButton == MouseButtonState.Pressed) { iniP = e.GetPosition(blackboard_canvas); } } } private Stroke StrokeRound; private Stroke StrokeRectangle; private void blackboard_canvas_MouseMove(object sender, MouseEventArgs e) { if (flg != 0) { if (e.LeftButton == MouseButtonState.Pressed) { // Draw square if (flg == 1) { System.Windows.Point endP = e.GetPosition(blackboard_canvas); List pointList = new List { new System.Windows.Point(iniP.X, iniP.Y), new System.Windows.Point(iniP.X, endP.Y), new System.Windows.Point(endP.X, endP.Y), new System.Windows.Point(endP.X, iniP.Y), new System.Windows.Point(iniP.X, iniP.Y), }; //Stroke stroke1 = new Stroke(drawingAttributesRound); StylusPointCollection point = new StylusPointCollection(pointList); Stroke stroke = new Stroke(point) { DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone() }; if (StrokeRectangle != null) { viewModel.InkStrokes.Remove(StrokeRectangle); } StrokeRectangle = stroke; viewModel.InkStrokes.Add(stroke); } // Draw Eclipse else if (flg == 2) { System.Windows.Point endP = e.GetPosition(blackboard_canvas); List pointList = GenerateEclipseGeometry(iniP, endP); StylusPointCollection point = new StylusPointCollection(pointList); Stroke stroke = new Stroke(point) { DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone() }; //viewModel.InkStrokes.Clear(); if (StrokeRound != null) { viewModel.InkStrokes.Remove(StrokeRound); } StrokeRound = stroke; viewModel.InkStrokes.Add(stroke); } } } } private List GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed) { double a = 0.5 * (ed.X - st.X); double b = 0.5 * (ed.Y - st.Y); List pointList = new List(); for (double r = 0; r <= 2 * Math.PI; r = r + 0.01) { pointList.Add(new System.Windows.Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r))); } return pointList; } /// /// 鼠标松开时 /// /// /// private void blackboard_canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { StrokeRound = null; StrokeRectangle = null; } /// /// 鼠标右键 /// /// /// private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (APP.W_ScreenRecordingToolbarWindow.flg == 10) { APP.W_ScreenRecordingToolbarWindow.flg = 0; } else { APP.W_ScreenRecordingToolbarWindow.flg = 1; } Hide(); } private void blackboard_canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (APP.W_ScreenRecordingToolbarWindow.flg == 10) { APP.W_ScreenRecordingToolbarWindow.flg = 0; } else { APP.W_ScreenRecordingToolbarWindow.flg = 1; } Hide(); } private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (APP.W_ScreenRecordingToolbarWindow.flg == 10) { APP.W_ScreenRecordingToolbarWindow.flg = 0; } else { APP.W_ScreenRecordingToolbarWindow.flg = 1; } blackboard_canvas.Strokes.Clear(); APP.W_ScreenRecordingToolbarWindow.ReturnPractice(); //Hide(); } } }