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

PracticeWindow.xaml.cs 13KB

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