星火批注
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  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. namespace Common.system
  10. {
  11. internal enum ZPenType : byte
  12. {
  13. Pen = 1,
  14. Erase = 2
  15. };
  16. internal class ZBBPage
  17. {
  18. public List<ZBBPageStep> lines { get; set; }
  19. public List<ZBBPageStep> lines_histoty { get; set; }
  20. public ZBBPage()
  21. {
  22. lines = new List<ZBBPageStep>();
  23. lines_histoty = new List<ZBBPageStep>();
  24. }
  25. }
  26. internal class ZBBPageStep
  27. {
  28. public StrokeCollection lines_curr { get; set; }
  29. public StrokeCollection lines_add { get; set; }
  30. public StrokeCollection lines_remove { get; set; }
  31. public ZBBPageStep()
  32. {
  33. lines_curr = new StrokeCollection();
  34. lines_add = new StrokeCollection();
  35. lines_remove = new StrokeCollection();
  36. }
  37. }
  38. public class ZJBlackboardNew
  39. {
  40. private InkCanvas m_canvas;
  41. //private ZPenType type = ZPenType.Pen;
  42. private int pagenum = 0;
  43. private readonly int erasesize = 64;
  44. private readonly int pensize = 3;
  45. private int undoOrRedo = 0; //是否在进行撤销恢复操作
  46. private Color pencolor;
  47. private readonly List<ZBBPage> strokes_page_all = new List<ZBBPage>();
  48. // 添加这个变量是因为在用橡皮擦时 一次操作会触发多次StrokesChanged回掉 这里是把多次回掉合并在一起
  49. private ZBBPageStep step = null;
  50. public ZJBlackboardNew(InkCanvas canvas)
  51. {
  52. init(canvas, Colors.White);
  53. }
  54. public ZJBlackboardNew(InkCanvas canvas, Color _pencolor)
  55. {
  56. init(canvas, _pencolor);
  57. }
  58. private void init(InkCanvas canvas, Color _pencolor)
  59. {
  60. m_canvas = canvas;
  61. pencolor = _pencolor;
  62. ZBBPage page = new ZBBPage();
  63. page.lines.Add(new ZBBPageStep());
  64. strokes_page_all.Add(page);
  65. if (canvas != null)
  66. {
  67. canvas.EditingMode = InkCanvasEditingMode.Ink;
  68. canvas.UseCustomCursor = true;
  69. canvas.Cursor = Cursors.Pen;
  70. DrawingAttributes drawingAttributes = new DrawingAttributes();
  71. canvas.DefaultDrawingAttributes = drawingAttributes;
  72. drawingAttributes.Width = pensize;
  73. drawingAttributes.Height = pensize;
  74. drawingAttributes.Color = pencolor;
  75. drawingAttributes.FitToCurve = true;
  76. drawingAttributes.IgnorePressure = false;
  77. canvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
  78. canvas.StrokeCollected += Canvas_StrokeCollected;
  79. canvas.StrokeErasing += Canvas_StrokeErasing;
  80. canvas.StrokeErased += Canvas_StrokeErased;
  81. canvas.MouseUp += Canvas_MouseUp;
  82. }
  83. }
  84. private void Canvas_StrokeErasing(object sender, InkCanvasStrokeErasingEventArgs e)
  85. {
  86. undoOrRedo = 0;
  87. }
  88. private void Canvas_StrokeErased(object sender, RoutedEventArgs e)
  89. {
  90. }
  91. private void Canvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
  92. {
  93. }
  94. private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
  95. {
  96. if (step != null)
  97. {
  98. ZBBPage page = strokes_page_all[pagenum];
  99. if (page != null)
  100. {
  101. step.lines_curr.Add(m_canvas.Strokes);
  102. page.lines.Add(step);
  103. step = null;
  104. }
  105. }
  106. }
  107. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  108. {
  109. if (undoOrRedo > 0)
  110. {
  111. undoOrRedo -= 1;
  112. return;
  113. }
  114. if (step == null)
  115. {
  116. step = new ZBBPageStep();
  117. }
  118. // 笔模式
  119. if (e.Added.Count > 0 && e.Removed.Count == 0)
  120. {
  121. step.lines_add.Add(e.Added);
  122. }
  123. // 橡皮模式 会多次进入回掉
  124. else if (e.Removed.Count > 0)
  125. {
  126. step.lines_add.Add(e.Added);
  127. for (int i = 0; i < e.Removed.Count; i++)
  128. {
  129. Stroke removeItem = e.Removed[i];
  130. try
  131. {
  132. if (step.lines_add.Contains(removeItem))
  133. {
  134. step.lines_add.Remove(removeItem);
  135. }
  136. else
  137. {
  138. step.lines_remove.Add(removeItem);
  139. }
  140. }
  141. catch (System.Exception ex)
  142. {
  143. LogHelper.WriteErrLog("【笔迹添加移除事件】(Strokes_StrokesChanged)" + ex.Message, ex);
  144. }
  145. }
  146. }
  147. }
  148. // public方法 笔
  149. public void change_pen(Color _color, int _size)
  150. {
  151. //this.type = ZPenType.Pen;
  152. DrawingAttributes drawingAttributes = new DrawingAttributes();
  153. m_canvas.DefaultDrawingAttributes = drawingAttributes;
  154. drawingAttributes.Color = _color;
  155. drawingAttributes.Width = _size;
  156. drawingAttributes.Height = _size;
  157. drawingAttributes.FitToCurve = true;
  158. drawingAttributes.IgnorePressure = false;
  159. m_canvas.EditingMode = InkCanvasEditingMode.Ink;
  160. m_canvas.UseCustomCursor = true;
  161. m_canvas.Cursor = Cursors.Pen;
  162. }
  163. // 橡皮
  164. public void change_erase()
  165. {
  166. //this.type = ZPenType.Erase;
  167. m_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  168. m_canvas.UseCustomCursor = false;
  169. m_canvas.EraserShape = new EllipseStylusShape(erasesize, erasesize, 0);
  170. }
  171. // 撤销
  172. public void undo()
  173. {
  174. ZBBPage page = strokes_page_all[pagenum];
  175. if (page != null && m_canvas.Strokes.Count > 0 && page.lines.Count > 1)
  176. {
  177. ZBBPageStep last = page.lines.Last();
  178. page.lines.Remove(last);
  179. page.lines_histoty.Add(last);
  180. if (page.lines.Last().lines_curr.Count > 0)
  181. {
  182. undoOrRedo = 2;
  183. }
  184. else
  185. {
  186. undoOrRedo = 1;
  187. }
  188. m_canvas.Strokes.Clear();
  189. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  190. }
  191. }
  192. // 恢复
  193. public void redo()
  194. {
  195. ZBBPage page = strokes_page_all[pagenum];
  196. if (page != null && page.lines_histoty.Count > 0)
  197. {
  198. ZBBPageStep line = page.lines_histoty[page.lines_histoty.Count - 1];
  199. page.lines.Add(line);
  200. page.lines_histoty.Remove(line);
  201. if (page.lines.Last().lines_curr.Count > 0)
  202. {
  203. undoOrRedo = 2;
  204. }
  205. else
  206. {
  207. undoOrRedo = 1;
  208. }
  209. m_canvas.Strokes.Clear();
  210. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  211. }
  212. }
  213. // 清空
  214. public void clear()
  215. {
  216. ZBBPage page = strokes_page_all[pagenum];
  217. int num = page.lines.Count;
  218. for (int i = 0; i < num; i++)
  219. {
  220. page = strokes_page_all[pagenum];
  221. if (page != null && m_canvas.Strokes.Count > 0 && page.lines.Count > 1)
  222. {
  223. ZBBPageStep last = page.lines.Last();
  224. page.lines.Remove(last);
  225. page.lines_histoty.Add(last);
  226. if (page.lines.Last().lines_curr.Count > 0)
  227. {
  228. undoOrRedo = 2;
  229. }
  230. else
  231. {
  232. undoOrRedo = 1;
  233. }
  234. m_canvas.Strokes.Clear();
  235. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  236. }
  237. }
  238. }
  239. public void changepage(int mpagenum)
  240. {
  241. if (pagenum != mpagenum)
  242. {
  243. pagenum = mpagenum;
  244. if (pagenum >= strokes_page_all.Count)
  245. {
  246. int numadd = pagenum - strokes_page_all.Count + 1;
  247. for (int i = 0; i < numadd; i++)
  248. {
  249. ZBBPage pagetemp = new ZBBPage();
  250. pagetemp.lines.Add(new ZBBPageStep());
  251. strokes_page_all.Add(pagetemp);
  252. }
  253. }
  254. ZBBPage page = strokes_page_all[pagenum];
  255. if (page != null)
  256. {
  257. if (page.lines.Last().lines_curr.Count > 0)
  258. {
  259. undoOrRedo += 1;
  260. }
  261. if (m_canvas.Strokes.Count > 0)
  262. {
  263. undoOrRedo += 1;
  264. m_canvas.Strokes.Clear();
  265. }
  266. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  267. }
  268. }
  269. }
  270. }
  271. }