星火微课系统客户端
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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  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 XHWK.Model;
  10. namespace XHWK.WKTool
  11. {
  12. /// <summary>
  13. /// 录屏画板
  14. /// </summary>
  15. public partial class PracticeWindow : Window
  16. {
  17. //声明一个 DrawingAttributes 类型的变量
  18. private DrawingAttributes drawingAttributes;
  19. private ViewModel viewModel;
  20. /// <summary>
  21. /// 0 画笔 1 矩形 2 圆形
  22. /// </summary>
  23. private int flg = 0;
  24. /// <summary>
  25. /// 当前画笔颜色
  26. /// </summary>
  27. private Color Colour = Colors.Red;
  28. /// <summary>
  29. /// 当前画笔宽
  30. /// </summary>
  31. private int Wit = 2;
  32. /// <summary>
  33. /// 当前画笔高
  34. /// </summary>
  35. private int Hei = 2;
  36. public PracticeWindow()
  37. {
  38. InitializeComponent();
  39. }
  40. public void Initialize(string _imgPath = null)
  41. {
  42. flg = 0;
  43. blackboard_canvas.Strokes.Clear();
  44. blackboard_canvas.UseCustomCursor = true;
  45. //blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  46. //加背景 废弃更利于讲解ppt
  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. //APP.W_ScreenRecordingToolbarWindow.Owner = this;
  69. //APP.W_ScreenRecordingToolbarWindow.Topmost = true;
  70. }
  71. /// <summary>
  72. /// 画笔颜色事件 白色
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. public void White()
  77. {
  78. //flg = 0;
  79. drawingAttributes.Color = Colors.White;
  80. Colour = Colors.White;
  81. }
  82. /// <summary>
  83. /// 画笔颜色事件 红色
  84. /// </summary>
  85. /// <param name="sender"></param>
  86. /// <param name="e"></param>
  87. public void Red()
  88. {
  89. //flg = 0;
  90. //设置 DrawingAttributes 的 Color 属性设置颜色
  91. drawingAttributes.Color = Colors.Red;
  92. Colour = Colors.Red;
  93. }
  94. /// <summary>
  95. /// 画笔颜色事件 黑色
  96. /// </summary>
  97. /// <param name="sender"></param>
  98. /// <param name="e"></param>
  99. public void Gray()
  100. {
  101. //flg = 0;
  102. drawingAttributes.Color = Colors.Black;
  103. Colour = Colors.Black;
  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. Colour = Colors.LimeGreen;
  115. }
  116. /// <summary>
  117. /// 画笔颜色事件 黄色
  118. /// </summary>
  119. /// <param name="sender"></param>
  120. /// <param name="e"></param>
  121. public void Yellow()
  122. {
  123. //flg = 0;
  124. drawingAttributes.Color = Colors.Gold;
  125. Colour = Colors.Gold;
  126. }
  127. /// <summary>
  128. /// 画笔颜色事件 蓝色
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. public void Blue()
  133. {
  134. //flg = 0;
  135. drawingAttributes.Color = Colors.DeepSkyBlue;
  136. Colour = Colors.DeepSkyBlue;
  137. }
  138. /// <summary>
  139. /// 画笔粗细事件 细
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. public void Fine()
  144. {
  145. //flg = 0;
  146. drawingAttributes.Width = 1;
  147. drawingAttributes.Height = 1;
  148. Wit = 1;
  149. Hei = 1;
  150. }
  151. /// <summary>
  152. /// 画笔粗细事件 中
  153. /// </summary>
  154. /// <param name="sender"></param>
  155. /// <param name="e"></param>
  156. public void In()
  157. {
  158. //flg = 0;
  159. drawingAttributes.Width = 3;
  160. drawingAttributes.Height = 3;
  161. Wit = 3;
  162. Hei = 3;
  163. }
  164. /// <summary>
  165. /// 画笔粗细事件 粗
  166. /// </summary>
  167. /// <param name="sender"></param>
  168. /// <param name="e"></param>
  169. public void Crude()
  170. {
  171. //flg = 0;
  172. drawingAttributes.Width = 5;
  173. drawingAttributes.Height = 5;
  174. Wit = 5;
  175. Hei = 5;
  176. }
  177. /// <summary>
  178. /// 橡皮
  179. /// </summary>
  180. public void Eraser()
  181. {
  182. flg = 0;
  183. //this.type = ZPenType.Erase;
  184. blackboard_canvas.UseCustomCursor = false;
  185. blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  186. blackboard_canvas.EraserShape = new EllipseStylusShape(64, 64, 0);
  187. }
  188. /// <summary>
  189. /// 画笔
  190. /// </summary>
  191. public void Pen()
  192. {
  193. flg = 0;
  194. blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
  195. blackboard_canvas.UseCustomCursor = true;
  196. drawingAttributes.FitToCurve = true;
  197. drawingAttributes.IgnorePressure = false;
  198. blackboard_canvas.Cursor = Cursors.Pen;
  199. }
  200. /// <summary>
  201. /// 圆
  202. /// </summary>
  203. public void Round()
  204. {
  205. flg = 2;
  206. drawingAttributes = new DrawingAttributes
  207. {
  208. Color = Colour,
  209. Width = Wit,
  210. Height = Hei,
  211. StylusTip = StylusTip.Rectangle,
  212. //FitToCurve = true,
  213. IsHighlighter = false,
  214. IgnorePressure = true,
  215. };
  216. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  217. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  218. blackboard_canvas.Cursor = Cursors.Cross;
  219. }
  220. /// <summary>
  221. /// 矩形
  222. /// </summary>
  223. public void Rectangle()
  224. {
  225. flg = 1;
  226. drawingAttributes = new DrawingAttributes
  227. {
  228. Color = Colour,
  229. Width = Wit,
  230. Height = Hei,
  231. StylusTip = StylusTip.Rectangle,
  232. //FitToCurve = true,
  233. IsHighlighter = false,
  234. IgnorePressure = true,
  235. };
  236. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  237. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  238. blackboard_canvas.Cursor = Cursors.Cross;
  239. }
  240. private System.Windows.Point iniP;
  241. private void blackboard_canvas_MouseDown(object sender, MouseButtonEventArgs e)
  242. {
  243. if (flg != 0)
  244. {
  245. if (e.LeftButton == MouseButtonState.Pressed)
  246. {
  247. iniP = e.GetPosition(blackboard_canvas);
  248. }
  249. }
  250. }
  251. private Stroke StrokeRound;
  252. private Stroke StrokeRectangle;
  253. private void blackboard_canvas_MouseMove(object sender, MouseEventArgs e)
  254. {
  255. if (flg != 0)
  256. {
  257. if (e.LeftButton == MouseButtonState.Pressed)
  258. {
  259. // Draw square
  260. if (flg == 1)
  261. {
  262. System.Windows.Point endP = e.GetPosition(blackboard_canvas);
  263. List<System.Windows.Point> pointList = new List<System.Windows.Point>
  264. {
  265. new System.Windows.Point(iniP.X, iniP.Y),
  266. new System.Windows.Point(iniP.X, endP.Y),
  267. new System.Windows.Point(endP.X, endP.Y),
  268. new System.Windows.Point(endP.X, iniP.Y),
  269. new System.Windows.Point(iniP.X, iniP.Y),
  270. };
  271. //Stroke stroke1 = new Stroke(drawingAttributesRound);
  272. StylusPointCollection point = new StylusPointCollection(pointList);
  273. Stroke stroke = new Stroke(point)
  274. {
  275. DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
  276. };
  277. if (StrokeRectangle != null)
  278. {
  279. viewModel.InkStrokes.Remove(StrokeRectangle);
  280. }
  281. StrokeRectangle = stroke;
  282. viewModel.InkStrokes.Add(stroke);
  283. }
  284. // Draw Eclipse
  285. else if (flg == 2)
  286. {
  287. System.Windows.Point endP = e.GetPosition(blackboard_canvas);
  288. List<System.Windows.Point> pointList = GenerateEclipseGeometry(iniP, endP);
  289. StylusPointCollection point = new StylusPointCollection(pointList);
  290. Stroke stroke = new Stroke(point)
  291. {
  292. DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
  293. };
  294. //viewModel.InkStrokes.Clear();
  295. if (StrokeRound != null)
  296. {
  297. viewModel.InkStrokes.Remove(StrokeRound);
  298. }
  299. StrokeRound = stroke;
  300. viewModel.InkStrokes.Add(stroke);
  301. }
  302. }
  303. }
  304. }
  305. private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed)
  306. {
  307. double a = 0.5 * (ed.X - st.X);
  308. double b = 0.5 * (ed.Y - st.Y);
  309. List<System.Windows.Point> pointList = new List<System.Windows.Point>();
  310. for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
  311. {
  312. 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)));
  313. }
  314. return pointList;
  315. }
  316. /// <summary>
  317. /// 鼠标松开时
  318. /// </summary>
  319. /// <param name="sender"></param>
  320. /// <param name="e"></param>
  321. private void blackboard_canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  322. {
  323. StrokeRound = null;
  324. StrokeRectangle = null;
  325. }
  326. /// <summary>
  327. /// 鼠标右键
  328. /// </summary>
  329. /// <param name="sender"></param>
  330. /// <param name="e"></param>
  331. private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  332. {
  333. if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
  334. {
  335. APP.W_ScreenRecordingToolbarWindow.flg = 0;
  336. }
  337. else
  338. {
  339. APP.W_ScreenRecordingToolbarWindow.flg = 1;
  340. }
  341. Hide();
  342. }
  343. private void blackboard_canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  344. {
  345. if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
  346. {
  347. APP.W_ScreenRecordingToolbarWindow.flg = 0;
  348. }
  349. else
  350. {
  351. APP.W_ScreenRecordingToolbarWindow.flg = 1;
  352. }
  353. Hide();
  354. }
  355. private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  356. {
  357. if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
  358. {
  359. APP.W_ScreenRecordingToolbarWindow.flg = 0;
  360. }
  361. else
  362. {
  363. APP.W_ScreenRecordingToolbarWindow.flg = 1;
  364. }
  365. ReturnPractice();
  366. //Hide();
  367. }
  368. /// <summary>
  369. /// 退出批注并清空画板
  370. /// </summary>
  371. public void ReturnPractice()
  372. {
  373. blackboard_canvas.Strokes.Clear();
  374. new Thread(new ThreadStart(new Action(() =>
  375. {
  376. Dispatcher.Invoke(() =>
  377. {
  378. APP.W_ScreenRecordingToolbarWindow.ModifyState();
  379. });
  380. flg = 0;
  381. Thread.Sleep(500);
  382. Dispatcher.Invoke(() =>
  383. {
  384. Owner = null;
  385. APP.W_PracticeWindow.Hide();
  386. });
  387. }))).Start();
  388. }
  389. }
  390. }