星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

PracticeWindow.xaml.cs 10KB

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