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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. strokes_page_all.Clear();
  213. m_canvas.Strokes.Clear();
  214. //for (int i=0;i< strokes_page_all.Count;i++)
  215. //{
  216. // ZBBPage page = strokes_page_all[pagenum];
  217. // if (page != null)
  218. // {
  219. // m_canvas.Strokes.Clear();
  220. // page.lines_histoty.Clear();
  221. // page.lines.Clear();
  222. // page.lines.Add(new ZBBPageStep());
  223. // }
  224. //}
  225. }
  226. public void changepage(int mpagenum)
  227. {
  228. if (pagenum != mpagenum)
  229. {
  230. pagenum = mpagenum;
  231. if (pagenum >= strokes_page_all.Count)
  232. {
  233. int numadd = pagenum - strokes_page_all.Count + 1;
  234. for (int i = 0; i < numadd; i++)
  235. {
  236. ZBBPage pagetemp = new ZBBPage();
  237. pagetemp.lines.Add(new ZBBPageStep());
  238. strokes_page_all.Add(pagetemp);
  239. }
  240. }
  241. ZBBPage page = strokes_page_all[pagenum];
  242. if (page != null&& page.lines.Count>0)
  243. {
  244. if (page.lines.Last().lines_curr.Count > 0)
  245. {
  246. undoOrRedo += 1;
  247. }
  248. if (m_canvas.Strokes.Count > 0)
  249. {
  250. undoOrRedo += 1;
  251. m_canvas.Strokes.Clear();
  252. }
  253. StrokeCollection strokes = new StrokeCollection();
  254. StylusPointCollection stylusPoints = new StylusPointCollection();
  255. System.Windows.Input.StylusPointDescription stylusPointDescription = new StylusPointDescription();
  256. StylusPoint stylusPoint = new StylusPoint();
  257. stylusPoint.X = 580;
  258. stylusPoint.Y = 212;
  259. stylusPoints.Add(stylusPoint);
  260. Stroke stroke = new Stroke(stylusPoints);
  261. stylusPoint.X = 581;
  262. stylusPoint.Y = 213;
  263. stylusPoints.Add(stylusPoint);
  264. stroke = new Stroke(stylusPoints);
  265. page.lines.Last().lines_curr.Add(stroke);
  266. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  267. }
  268. }
  269. }
  270. StylusPointCollection stylusPoints = new StylusPointCollection();
  271. StylusPoint stylusPoint = new StylusPoint();
  272. Stroke stroke;
  273. bool isFirst = true;
  274. public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  275. {
  276. if (_new)
  277. {
  278. if (stroke != null && stroke.StylusPoints.Count > 1)
  279. {
  280. isFirst = true;
  281. strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  282. }
  283. stylusPoints = new StylusPointCollection();
  284. stylusPoint = new StylusPoint();
  285. stroke = null;
  286. }
  287. else
  288. {
  289. if (isFirst)
  290. {
  291. stylusPoint.X = _x;
  292. stylusPoint.Y = _y;
  293. stylusPoints.Add(stylusPoint);
  294. if (stylusPoints.Count > 1)
  295. {
  296. stroke = new Stroke(stylusPoints);
  297. drawingAttributes = new DrawingAttributes();
  298. drawingAttributes.Color = _color;
  299. drawingAttributes.Width = _size*4.5;
  300. drawingAttributes.Height = _size * 4.5;
  301. drawingAttributes.FitToCurve = true;
  302. drawingAttributes.IgnorePressure = false;
  303. stroke.DrawingAttributes = drawingAttributes;
  304. m_canvas.Strokes.Add(stroke);
  305. isFirst = false;
  306. }
  307. }
  308. else
  309. {
  310. if (m_canvas.Strokes.Count > 0)
  311. {
  312. stylusPoint.X = _x;
  313. stylusPoint.Y = _y;
  314. stylusPoints.Add(stylusPoint);
  315. stroke = new Stroke(stylusPoints);
  316. drawingAttributes = new DrawingAttributes();
  317. drawingAttributes.Color = _color;
  318. drawingAttributes.Width = _size * 4.5;
  319. drawingAttributes.Height = _size * 4.5;
  320. drawingAttributes.FitToCurve = true;
  321. drawingAttributes.IgnorePressure = false;
  322. stroke.DrawingAttributes = drawingAttributes;
  323. m_canvas.Strokes[m_canvas.Strokes.Count - 1] = stroke;
  324. }
  325. }
  326. }
  327. }
  328. //public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  329. //{
  330. // try
  331. // {
  332. // if (_new)
  333. // {
  334. // if (stroke != null && stroke.StylusPoints.Count > 1)
  335. // {
  336. // drawingAttributes = new DrawingAttributes();
  337. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  338. // drawingAttributes.Color = _color;
  339. // drawingAttributes.Width = _size * 5;
  340. // drawingAttributes.Height = _size * 5;
  341. // drawingAttributes.FitToCurve = true;
  342. // drawingAttributes.IgnorePressure = false;
  343. // stroke.DrawingAttributes = drawingAttributes;
  344. // //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  345. // m_canvas.Strokes.Add(stroke);
  346. // strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  347. // //int currCount= strokes_page_all[i].lines.Last().lines_curr.Count;
  348. // //if (currCount > 0)
  349. // //{
  350. // // strokes_page_all[i].lines.Last().lines_curr[currCount - 1] = stroke;
  351. // //}
  352. // stroke = null;
  353. // }
  354. // stylusPoints = new StylusPointCollection();
  355. // stylusPoint = new StylusPoint();
  356. // //stroke = new Stroke(stylusPoints);
  357. // //stroke = null;
  358. // }
  359. // else
  360. // {
  361. // //stylusPoints = new StylusPointCollection();
  362. // //stylusPoint = new StylusPoint();
  363. // stylusPoint.X = _x;
  364. // stylusPoint.Y = _y;
  365. // stylusPoints.Add(stylusPoint);
  366. // stroke = new Stroke(stylusPoints);
  367. // }
  368. // }
  369. // catch (Exception ex)
  370. // {
  371. // LogHelper.WriteErrLog("【XHMicroLessonSystemWindow】(changepages)点阵比书写报错:" + ex.Message, ex);
  372. // }
  373. //}
  374. }
  375. }