星火微课系统客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PracticeWindow.xaml.cs 14KB

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