星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BlackboardNew.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Ink;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. namespace Common.system
  12. {
  13. internal enum ZPenType : byte
  14. {
  15. Pen = 1,
  16. Erase = 2
  17. };
  18. internal class ZBBPage
  19. {
  20. public List<ZBBPageStep> lines { get; set; }
  21. public List<ZBBPageStep> lines_histoty { get; set; }
  22. public ZBBPage()
  23. {
  24. lines = new List<ZBBPageStep>();
  25. lines_histoty = new List<ZBBPageStep>();
  26. }
  27. }
  28. internal class ZBBPageStep
  29. {
  30. public StrokeCollection lines_curr { get; set; }
  31. public StrokeCollection lines_add { get; set; }
  32. public StrokeCollection lines_remove { get; set; }
  33. public ZBBPageStep()
  34. {
  35. lines_curr = new StrokeCollection();
  36. lines_add = new StrokeCollection();
  37. lines_remove = new StrokeCollection();
  38. }
  39. }
  40. public class BlackboardNew
  41. {
  42. private InkCanvas m_canvas;
  43. //private ZPenType type = ZPenType.Pen;
  44. private int pagenum = 0;
  45. private readonly int erasesize = 64;
  46. private readonly int pensize = 3;
  47. private int undoOrRedo = 0; //是否在进行撤销恢复操作
  48. private Color pencolor;
  49. private readonly List<ZBBPage> strokes_page_all = new List<ZBBPage>();
  50. // 添加这个变量是因为在用橡皮擦时 一次操作会触发多次StrokesChanged回掉 这里是把多次回掉合并在一起
  51. private ZBBPageStep step = null;
  52. public BlackboardNew(InkCanvas canvas)
  53. {
  54. init(canvas, Colors.White);
  55. }
  56. public BlackboardNew(InkCanvas canvas, Color _pencolor)
  57. {
  58. init(canvas, _pencolor);
  59. }
  60. DrawingAttributes drawingAttributes;
  61. private void init(InkCanvas canvas, Color _pencolor)
  62. {
  63. m_canvas = canvas;
  64. pencolor = _pencolor;
  65. ZBBPage page = new ZBBPage();
  66. page.lines.Add(new ZBBPageStep());
  67. strokes_page_all.Add(page);
  68. if (canvas != null)
  69. {
  70. canvas.EditingMode = InkCanvasEditingMode.Ink;
  71. drawingAttributes = new DrawingAttributes();
  72. canvas.DefaultDrawingAttributes = drawingAttributes;
  73. drawingAttributes.Width = pensize;
  74. drawingAttributes.Height = pensize;
  75. drawingAttributes.Color = pencolor;
  76. drawingAttributes.FitToCurve = true;
  77. drawingAttributes.IgnorePressure = false;
  78. canvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
  79. canvas.StrokeCollected += Canvas_StrokeCollected;
  80. canvas.StrokeErasing += Canvas_StrokeErasing;
  81. canvas.StrokeErased += Canvas_StrokeErased;
  82. canvas.MouseUp += Canvas_MouseUp;
  83. }
  84. }
  85. private void Canvas_StrokeErasing(object sender, InkCanvasStrokeErasingEventArgs e)
  86. {
  87. undoOrRedo = 0;
  88. }
  89. private void Canvas_StrokeErased(object sender, RoutedEventArgs e)
  90. {
  91. }
  92. private void Canvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
  93. {
  94. }
  95. private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
  96. {
  97. if (step != null)
  98. {
  99. ZBBPage page = strokes_page_all[pagenum];
  100. if (page != null)
  101. {
  102. step.lines_curr.Add(m_canvas.Strokes);
  103. page.lines.Add(step);
  104. step = null;
  105. }
  106. }
  107. }
  108. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  109. {
  110. if (undoOrRedo > 0)
  111. {
  112. undoOrRedo -= 1;
  113. return;
  114. }
  115. if (step == null)
  116. {
  117. step = new ZBBPageStep();
  118. }
  119. // 笔模式
  120. if (e.Added.Count > 0 && e.Removed.Count == 0)
  121. {
  122. step.lines_add.Add(e.Added);
  123. }
  124. // 橡皮模式 会多次进入回掉
  125. else if (e.Removed.Count > 0)
  126. {
  127. step.lines_add.Add(e.Added);
  128. for (int i = 0; i < e.Removed.Count; i++)
  129. {
  130. Stroke removeItem = e.Removed[i];
  131. if (step.lines_add.Contains(removeItem))
  132. {
  133. step.lines_add.Remove(removeItem);
  134. }
  135. else
  136. {
  137. step.lines_remove.Add(removeItem);
  138. }
  139. }
  140. }
  141. }
  142. // public方法 笔
  143. public void change_pen(Color _color)
  144. {
  145. //this.type = ZPenType.Pen;
  146. DrawingAttributes drawingAttributes = new DrawingAttributes();
  147. m_canvas.DefaultDrawingAttributes = drawingAttributes;
  148. drawingAttributes.Color = _color;
  149. drawingAttributes.Width = pensize;
  150. drawingAttributes.Height = pensize;
  151. drawingAttributes.FitToCurve = true;
  152. drawingAttributes.IgnorePressure = false;
  153. m_canvas.EditingMode = InkCanvasEditingMode.Ink;
  154. }
  155. // 橡皮
  156. public void change_erase()
  157. {
  158. //this.type = ZPenType.Erase;
  159. m_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  160. m_canvas.EraserShape = new EllipseStylusShape(erasesize, erasesize, 0);
  161. }
  162. // 撤销
  163. public void undo()
  164. {
  165. ZBBPage page = strokes_page_all[pagenum];
  166. if (page != null && m_canvas.Strokes.Count > 0 && page.lines.Count > 1)
  167. {
  168. ZBBPageStep last = page.lines.Last();
  169. page.lines.Remove(last);
  170. page.lines_histoty.Add(last);
  171. if (page.lines.Last().lines_curr.Count > 0)
  172. {
  173. undoOrRedo = 2;
  174. }
  175. else
  176. {
  177. undoOrRedo = 1;
  178. }
  179. m_canvas.Strokes.Clear();
  180. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  181. }
  182. }
  183. // 恢复
  184. public void redo()
  185. {
  186. ZBBPage page = strokes_page_all[pagenum];
  187. if (page != null && page.lines_histoty.Count > 0)
  188. {
  189. ZBBPageStep line = page.lines_histoty[page.lines_histoty.Count - 1];
  190. page.lines.Add(line);
  191. page.lines_histoty.Remove(line);
  192. if (page.lines.Last().lines_curr.Count > 0)
  193. {
  194. undoOrRedo = 2;
  195. }
  196. else
  197. {
  198. undoOrRedo = 1;
  199. }
  200. m_canvas.Strokes.Clear();
  201. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  202. }
  203. }
  204. // 清空
  205. public void clear()
  206. {
  207. ZBBPage page = new ZBBPage();
  208. page.lines.Add(new ZBBPageStep());
  209. page.lines = new List<ZBBPageStep>();
  210. page.lines_histoty = new List<ZBBPageStep>();
  211. strokes_page_all.Add(page);
  212. //ZBBPage page = strokes_page_all[pagenum];
  213. // if (page != null)
  214. //{
  215. // m_canvas.Strokes.Clear();
  216. // page.lines_histoty.Clear();
  217. // page.lines.Clear();
  218. // page.lines.Add(new ZBBPageStep());
  219. //}
  220. }
  221. public void changepage(int mpagenum)
  222. {
  223. if (pagenum != mpagenum)
  224. {
  225. pagenum = mpagenum;
  226. if (pagenum >= strokes_page_all.Count)
  227. {
  228. int numadd = pagenum - strokes_page_all.Count + 1;
  229. for (int i = 0; i < numadd; i++)
  230. {
  231. ZBBPage pagetemp = new ZBBPage();
  232. pagetemp.lines.Add(new ZBBPageStep());
  233. strokes_page_all.Add(pagetemp);
  234. }
  235. }
  236. ZBBPage page = strokes_page_all[pagenum];
  237. if (page != null&& page.lines.Count>0)
  238. {
  239. if (page.lines.Last().lines_curr.Count > 0)
  240. {
  241. undoOrRedo += 1;
  242. }
  243. if (m_canvas.Strokes.Count > 0)
  244. {
  245. undoOrRedo += 1;
  246. m_canvas.Strokes.Clear();
  247. }
  248. StrokeCollection strokes = new StrokeCollection();
  249. StylusPointCollection stylusPoints = new StylusPointCollection();
  250. System.Windows.Input.StylusPointDescription stylusPointDescription = new StylusPointDescription();
  251. StylusPoint stylusPoint = new StylusPoint();
  252. stylusPoint.X = 580;
  253. stylusPoint.Y = 212;
  254. stylusPoints.Add(stylusPoint);
  255. Stroke stroke = new Stroke(stylusPoints);
  256. stylusPoint.X = 581;
  257. stylusPoint.Y = 213;
  258. stylusPoints.Add(stylusPoint);
  259. stroke = new Stroke(stylusPoints);
  260. page.lines.Last().lines_curr.Add(stroke);
  261. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  262. //stylusPoint.X = 586;
  263. //stylusPoint.Y = 214;
  264. //stylusPoints.Add(stylusPoint);
  265. //stroke = new Stroke(stylusPoints);
  266. //stylusPoint.X = 599;
  267. //stylusPoint.Y = 214;
  268. //stylusPoints.Add(stylusPoint);
  269. //stroke = new Stroke(stylusPoints);
  270. //stylusPoint.X = 623;
  271. //stylusPoint.Y = 214;
  272. //stylusPoints.Add(stylusPoint);
  273. //stroke = new Stroke(stylusPoints);
  274. //stylusPoint.X = 663;
  275. //stylusPoint.Y = 214;
  276. //stylusPoints.Add(stylusPoint);
  277. //stroke = new Stroke(stylusPoints);
  278. //stylusPoint.X = 963;
  279. //stylusPoint.Y = 214;
  280. //stylusPoints.Add(stylusPoint);
  281. //stroke = new Stroke(stylusPoints);
  282. //DrawingAttributes drawingAttributes = new DrawingAttributes();
  283. //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  284. //drawingAttributes.Color = Colors.Red;
  285. //drawingAttributes.Width = pensize;
  286. //drawingAttributes.Height = pensize;
  287. //drawingAttributes.FitToCurve = true;
  288. //drawingAttributes.IgnorePressure = false;
  289. //stroke.DrawingAttributes = drawingAttributes;
  290. //m_canvas.Strokes.Add(stroke);
  291. }
  292. }
  293. }
  294. StylusPointCollection stylusPoints = new StylusPointCollection();
  295. StylusPoint stylusPoint = new StylusPoint();
  296. Stroke stroke;
  297. //bool isFirst = true;
  298. //public void changepages(double _x, double _y, bool _new, Color _color, int _size)
  299. //{
  300. // if (_new)
  301. // {
  302. // if (stroke != null && stroke.StylusPoints.Count > 1)
  303. // {
  304. // drawingAttributes = new DrawingAttributes();
  305. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  306. // drawingAttributes.Color = _color;
  307. // drawingAttributes.Width = _size;
  308. // drawingAttributes.Height = _size;
  309. // drawingAttributes.FitToCurve = true;
  310. // drawingAttributes.IgnorePressure = false;
  311. // stroke.DrawingAttributes = drawingAttributes;
  312. // m_canvas.Strokes.Add(stroke);
  313. // isFirst = true;
  314. // }
  315. // stylusPoints = new StylusPointCollection();
  316. // stylusPoint = new StylusPoint();
  317. // //stroke = new Stroke(stylusPoints);
  318. // stroke = null;
  319. // }
  320. // else
  321. // {
  322. // if(isFirst)
  323. // {
  324. // stylusPoint.X = _x;
  325. // stylusPoint.Y = _y;
  326. // stylusPoints.Add(stylusPoint);
  327. // if (stylusPoints.Count>1)
  328. // {
  329. // stroke = new Stroke(stylusPoints);
  330. // m_canvas.Strokes.Add(stroke);
  331. // isFirst = false;
  332. // }
  333. // }
  334. // else
  335. // {
  336. // stylusPoint.X = _x;
  337. // stylusPoint.Y = _y;
  338. // stylusPoints.Add(stylusPoint);
  339. // stroke = new Stroke(stylusPoints);
  340. // m_canvas.Strokes[m_canvas.Strokes.Count - 1].StylusPoints.Add(stylusPoints);
  341. // }
  342. // }
  343. //}
  344. public void changepages(double _x, double _y, bool _new, Color _color, int _size)
  345. {
  346. if (_new)
  347. {
  348. if (stroke != null && stroke.StylusPoints.Count > 1)
  349. {
  350. drawingAttributes = new DrawingAttributes();
  351. //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  352. drawingAttributes.Color = _color;
  353. drawingAttributes.Width = _size*5;
  354. drawingAttributes.Height = _size*5;
  355. drawingAttributes.FitToCurve = true;
  356. drawingAttributes.IgnorePressure = false;
  357. stroke.DrawingAttributes = drawingAttributes;
  358. //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  359. m_canvas.Strokes.Add(stroke);
  360. }
  361. stylusPoints = new StylusPointCollection();
  362. stylusPoint = new StylusPoint();
  363. //stroke = new Stroke(stylusPoints);
  364. //stroke = null;
  365. }
  366. else
  367. {
  368. //stylusPoints = new StylusPointCollection();
  369. //stylusPoint = new StylusPoint();
  370. stylusPoint.X = _x;
  371. stylusPoint.Y = _y;
  372. stylusPoints.Add(stylusPoint);
  373. stroke = new Stroke(stylusPoints);
  374. }
  375. }
  376. ////声明一个 DrawingAttributes 类型的变量
  377. //DrawingAttributes drawingAttributes;
  378. //public void changepaget(double _x, double _y, bool _new,InkCanvas canvas)
  379. //{
  380. // if (_new)
  381. // {
  382. // if (stroke != null && stroke.StylusPoints.Count > 1)
  383. // {
  384. // //m_canvas.Strokes.Add(stroke);
  385. // DrawingAttributes drawingAttributes = new DrawingAttributes();
  386. // canvas.DefaultDrawingAttributes = drawingAttributes;
  387. // drawingAttributes.Width = pensize;
  388. // drawingAttributes.Height = pensize;
  389. // drawingAttributes.Color = Colors.Red;
  390. // drawingAttributes.FitToCurve = true;
  391. // drawingAttributes.IgnorePressure = false;
  392. // canvas.DefaultDrawingAttributes = drawingAttributes;
  393. // canvas.Strokes.Add(stroke);
  394. // ZBBPage page = strokes_page_all[pagenum];
  395. // if (page != null)
  396. // {
  397. // step.lines_curr.Add(m_canvas.Strokes);
  398. // page.lines.Add(step);
  399. // step = null;
  400. // }
  401. // }
  402. // stylusPoints = new StylusPointCollection();
  403. // stylusPoint = new StylusPoint();
  404. // //stroke = new Stroke(stylusPoints);
  405. // stroke = null;
  406. // }
  407. // else
  408. // {
  409. // change_pen(Colors.Red);
  410. // //stylusPoints = new StylusPointCollection();
  411. // //stylusPoint = new StylusPoint();
  412. // stylusPoint.X = _x;
  413. // stylusPoint.Y = _y;
  414. // stylusPoints.Add(stylusPoint);
  415. // stroke = new Stroke(stylusPoints);
  416. // //m_canvas.
  417. // //page.lines.Last().lines_curr
  418. // }
  419. // //page.lines.Last().lines_curr.Add();
  420. // //if (stroke.StylusPoints.Count > 20)
  421. // //{
  422. //}
  423. }
  424. }