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

PracticeWindow.xaml.cs 13KB

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