|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using XHWK.Model;
-
- namespace XHWK.WKTool
- {
- /// <summary>
- /// 录屏画板
- /// </summary>
- public partial class PracticeWindow : Window
- {
- //声明一个 DrawingAttributes 类型的变量
- DrawingAttributes drawingAttributes;
-
- private ViewModel viewModel;
- /// <summary>
- /// 0 画笔 1 矩形 2 圆形
- /// </summary>
- private int flg = 0;
- /// <summary>
- ///
- /// </summary>
- Color Colour=Colors.Black;
- 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:\Icon.cur");
-
- //blackboard_canvas.Cursor = cus;
-
- viewModel = new ViewModel
- {
- InkStrokes = new StrokeCollection(),
- };
-
- DataContext = viewModel;
- }
- /// <summary>
- /// 画笔颜色事件 白色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void White()
- {
- flg = 0;
- drawingAttributes.Color = Colors.White;
- }
- /// <summary>
- /// 画笔颜色事件 红色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void Red()
- {
- flg = 0;
- //设置 DrawingAttributes 的 Color 属性设置颜色
- drawingAttributes.Color = Colors.Red;
- }
- /// <summary>
- /// 画笔颜色事件 灰色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void Gray()
- {
- flg = 0;
- drawingAttributes.Color = Colors.Gray;
- }
- /// <summary>
- /// 画笔颜色事件 青色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void CyanBlue()
- {
- flg = 0;
- drawingAttributes.Color = Colors.LimeGreen;
- }
- /// <summary>
- /// 画笔颜色事件 黄色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void Yellow()
- {
- flg = 0;
- drawingAttributes.Color = Colors.Gold;
- }
- /// <summary>
- /// 画笔颜色事件 蓝色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void Blue()
- {
- flg = 0;
- drawingAttributes.Color = Colors.DeepSkyBlue;
- }
- /// <summary>
- /// 画笔粗细事件 细
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void Fine()
- {
- flg = 0;
- drawingAttributes.Width = 1;
- drawingAttributes.Height = 1;
- }
- /// <summary>
- /// 画笔粗细事件 中
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void In()
- {
- flg = 0;
- drawingAttributes.Width = 3;
- drawingAttributes.Height = 3;
- }
- /// <summary>
- /// 画笔粗细事件 粗
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void Crude()
- {
- flg = 0;
- drawingAttributes.Width = 5;
- drawingAttributes.Height = 5;
- }
- /// <summary>
- /// 橡皮
- /// </summary>
- 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);
- }
- /// <summary>
- /// 画笔
- /// </summary>
- 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;
- }
- /// <summary>
- /// 圆
- /// </summary>
- public void Round()
- {
- flg = 2;
- drawingAttributes = new DrawingAttributes
- {
- Color = Colors.Red,
- Width = 2,
- Height = 2,
- StylusTip = StylusTip.Rectangle,
- //FitToCurve = true,
- IsHighlighter = false,
- IgnorePressure = true,
-
- };
- blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
- blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
- }
- /// <summary>
- /// 矩形
- /// </summary>
- public void Rectangle()
- {
- flg = 1;
- blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
- }
- 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);
- }
- }
- }
- Stroke StrokeRound;
- 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<System.Windows.Point> pointList = new List<System.Windows.Point>
- {
- 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<System.Windows.Point> 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<System.Windows.Point> 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<System.Windows.Point> pointList = new List<System.Windows.Point>();
- 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;
- }
- /// <summary>
- /// 鼠标松开时
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void blackboard_canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- StrokeRound = null;
- StrokeRectangle = null;
- }
- }
- }
-
|