星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PracticeWindow.xaml.cs 11KB

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