星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PracticeWindow.xaml.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Ink;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Shapes;
  16. using XHWK.Model;
  17. namespace XHWK.WKTool
  18. {
  19. /// <summary>
  20. /// 录屏画板
  21. /// </summary>
  22. public partial class PracticeWindow : Window
  23. {
  24. //声明一个 DrawingAttributes 类型的变量
  25. DrawingAttributes drawingAttributes;
  26. private ViewModel viewModel;
  27. /// <summary>
  28. /// 0 画笔 1 矩形 2 圆形
  29. /// </summary>
  30. private int flg = 0;
  31. public PracticeWindow()
  32. {
  33. InitializeComponent();
  34. }
  35. public void Initialize(string _imgPath)
  36. {
  37. flg = 0;
  38. blackboard_canvas.Strokes.Clear();
  39. blackboard_canvas.UseCustomCursor = true;
  40. //blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  41. if (File.Exists(_imgPath))
  42. {
  43. imgCanvas.Source = new BitmapImage(new Uri(_imgPath));
  44. }
  45. drawingAttributes = new DrawingAttributes
  46. {
  47. Color = Colors.Red,
  48. Width = 2,
  49. Height = 2,
  50. StylusTip = StylusTip.Rectangle,
  51. //FitToCurve = true,
  52. IsHighlighter = false,
  53. IgnorePressure = true,
  54. };
  55. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  56. ////创建 DrawingAttributes 类的一个实例
  57. //drawingAttributes = new DrawingAttributes();
  58. //drawingAttributes.StylusTip = StylusTip.Rectangle;
  59. //drawingAttributes.IsHighlighter = false;
  60. //drawingAttributes.IgnorePressure = true;
  61. ////将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
  62. ////InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
  63. //blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  64. //Pen();
  65. //blackboard_canvas.Cursor = Cursors.Pen;
  66. //Cursor cus = new Cursor(@"G:\Icon.cur");
  67. //blackboard_canvas.Cursor = cus;
  68. viewModel = new ViewModel
  69. {
  70. InkStrokes = new StrokeCollection(),
  71. };
  72. DataContext = viewModel;
  73. }
  74. /// <summary>
  75. /// 画笔颜色事件 白色
  76. /// </summary>
  77. /// <param name="sender"></param>
  78. /// <param name="e"></param>
  79. public void White()
  80. {
  81. flg = 0;
  82. drawingAttributes.Color = Colors.White;
  83. }
  84. /// <summary>
  85. /// 画笔颜色事件 红色
  86. /// </summary>
  87. /// <param name="sender"></param>
  88. /// <param name="e"></param>
  89. public void Red()
  90. {
  91. flg = 0;
  92. //设置 DrawingAttributes 的 Color 属性设置颜色
  93. drawingAttributes.Color = Colors.Red;
  94. }
  95. /// <summary>
  96. /// 画笔颜色事件 灰色
  97. /// </summary>
  98. /// <param name="sender"></param>
  99. /// <param name="e"></param>
  100. public void Gray()
  101. {
  102. flg = 0;
  103. drawingAttributes.Color = Colors.Gray;
  104. }
  105. /// <summary>
  106. /// 画笔颜色事件 青色
  107. /// </summary>
  108. /// <param name="sender"></param>
  109. /// <param name="e"></param>
  110. public void CyanBlue()
  111. {
  112. flg = 0;
  113. drawingAttributes.Color = Colors.LimeGreen;
  114. }
  115. /// <summary>
  116. /// 画笔颜色事件 黄色
  117. /// </summary>
  118. /// <param name="sender"></param>
  119. /// <param name="e"></param>
  120. public void Yellow()
  121. {
  122. flg = 0;
  123. drawingAttributes.Color = Colors.Gold;
  124. }
  125. /// <summary>
  126. /// 画笔颜色事件 蓝色
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. public void Blue()
  131. {
  132. flg = 0;
  133. drawingAttributes.Color = Colors.DeepSkyBlue;
  134. }
  135. /// <summary>
  136. /// 画笔粗细事件 细
  137. /// </summary>
  138. /// <param name="sender"></param>
  139. /// <param name="e"></param>
  140. public void Fine()
  141. {
  142. flg = 0;
  143. drawingAttributes.Width = 1;
  144. drawingAttributes.Height = 1;
  145. }
  146. /// <summary>
  147. /// 画笔粗细事件 中
  148. /// </summary>
  149. /// <param name="sender"></param>
  150. /// <param name="e"></param>
  151. public void In()
  152. {
  153. flg = 0;
  154. drawingAttributes.Width = 3;
  155. drawingAttributes.Height = 3;
  156. }
  157. /// <summary>
  158. /// 画笔粗细事件 粗
  159. /// </summary>
  160. /// <param name="sender"></param>
  161. /// <param name="e"></param>
  162. public void Crude()
  163. {
  164. flg = 0;
  165. drawingAttributes.Width = 5;
  166. drawingAttributes.Height = 5;
  167. }
  168. /// <summary>
  169. /// 橡皮
  170. /// </summary>
  171. public void Eraser()
  172. {
  173. flg = 0;
  174. //this.type = ZPenType.Erase;
  175. blackboard_canvas.UseCustomCursor = false;
  176. blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  177. blackboard_canvas.EraserShape = new EllipseStylusShape(64, 64, 0);
  178. }
  179. /// <summary>
  180. /// 画笔
  181. /// </summary>
  182. public void Pen()
  183. {
  184. flg = 0;
  185. blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
  186. blackboard_canvas.UseCustomCursor = true;
  187. drawingAttributes.FitToCurve = true;
  188. drawingAttributes.IgnorePressure = false;
  189. blackboard_canvas.Cursor = Cursors.Pen;
  190. }
  191. private bool IsRound = false;
  192. StrokeCollection StrokeCollectionRound;
  193. /// <summary>
  194. /// 圆
  195. /// </summary>
  196. public void Round()
  197. {
  198. flg = 2;
  199. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  200. }
  201. /// <summary>
  202. /// 矩形
  203. /// </summary>
  204. public void Rectangle()
  205. {
  206. flg = 1;
  207. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  208. }
  209. private System.Windows.Point iniP;
  210. private void blackboard_canvas_MouseDown(object sender, MouseButtonEventArgs e)
  211. {
  212. if (flg != 0)
  213. {
  214. if (e.LeftButton == MouseButtonState.Pressed)
  215. {
  216. iniP = e.GetPosition(blackboard_canvas);
  217. }
  218. }
  219. }
  220. Stroke StrokeRound;
  221. Stroke StrokeRectangle;
  222. private void blackboard_canvas_MouseMove(object sender, MouseEventArgs e)
  223. {
  224. if(flg!=0)
  225. {
  226. if (e.LeftButton == MouseButtonState.Pressed)
  227. {
  228. // Draw square
  229. if (flg == 1)
  230. {
  231. System.Windows.Point endP = e.GetPosition(blackboard_canvas);
  232. List<System.Windows.Point> pointList = new List<System.Windows.Point>
  233. {
  234. new System.Windows.Point(iniP.X, iniP.Y),
  235. new System.Windows.Point(iniP.X, endP.Y),
  236. new System.Windows.Point(endP.X, endP.Y),
  237. new System.Windows.Point(endP.X, iniP.Y),
  238. new System.Windows.Point(iniP.X, iniP.Y),
  239. };
  240. //Stroke stroke1 = new Stroke(drawingAttributesRound);
  241. StylusPointCollection point = new StylusPointCollection(pointList);
  242. Stroke stroke = new Stroke(point)
  243. {
  244. DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
  245. };
  246. if(StrokeRectangle!=null)
  247. {
  248. viewModel.InkStrokes.Remove(StrokeRectangle);
  249. }
  250. StrokeRectangle = stroke;
  251. viewModel.InkStrokes.Add(stroke);
  252. }
  253. // Draw Eclipse
  254. else if (flg == 2)
  255. {
  256. System.Windows.Point endP = e.GetPosition(blackboard_canvas);
  257. List<System.Windows.Point> pointList = GenerateEclipseGeometry(iniP, endP);
  258. StylusPointCollection point = new StylusPointCollection(pointList);
  259. Stroke stroke = new Stroke(point)
  260. {
  261. DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
  262. };
  263. //viewModel.InkStrokes.Clear();
  264. if(StrokeRound!=null)
  265. {
  266. viewModel.InkStrokes.Remove(StrokeRound);
  267. }
  268. StrokeRound = stroke;
  269. viewModel.InkStrokes.Add(stroke);
  270. }
  271. }
  272. }
  273. }
  274. private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed)
  275. {
  276. double a = 0.5 * (ed.X - st.X);
  277. double b = 0.5 * (ed.Y - st.Y);
  278. List<System.Windows.Point> pointList = new List<System.Windows.Point>();
  279. for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
  280. {
  281. 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)));
  282. }
  283. return pointList;
  284. }
  285. private void blackboard_canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  286. {
  287. }
  288. /// <summary>
  289. /// 鼠标松开时
  290. /// </summary>
  291. /// <param name="sender"></param>
  292. /// <param name="e"></param>
  293. private void blackboard_canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  294. {
  295. StrokeRound = null;
  296. StrokeRectangle = null;
  297. }
  298. }
  299. }