星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. }
  263. }
  264. }
  265. StylusPointCollection stylusPoints = new StylusPointCollection();
  266. StylusPoint stylusPoint = new StylusPoint();
  267. Stroke stroke;
  268. bool isFirst = true;
  269. public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  270. {
  271. if (_new)
  272. {
  273. if (stroke != null && stroke.StylusPoints.Count > 1)
  274. {
  275. isFirst = true;
  276. strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  277. }
  278. stylusPoints = new StylusPointCollection();
  279. stylusPoint = new StylusPoint();
  280. stroke = null;
  281. }
  282. else
  283. {
  284. if (isFirst)
  285. {
  286. stylusPoint.X = _x;
  287. stylusPoint.Y = _y;
  288. stylusPoints.Add(stylusPoint);
  289. if (stylusPoints.Count > 1)
  290. {
  291. stroke = new Stroke(stylusPoints);
  292. drawingAttributes = new DrawingAttributes();
  293. drawingAttributes.Color = _color;
  294. drawingAttributes.Width = _size*4.5;
  295. drawingAttributes.Height = _size * 4.5;
  296. drawingAttributes.FitToCurve = true;
  297. drawingAttributes.IgnorePressure = false;
  298. stroke.DrawingAttributes = drawingAttributes;
  299. m_canvas.Strokes.Add(stroke);
  300. isFirst = false;
  301. }
  302. }
  303. else
  304. {
  305. if (m_canvas.Strokes.Count > 0)
  306. {
  307. stylusPoint.X = _x;
  308. stylusPoint.Y = _y;
  309. stylusPoints.Add(stylusPoint);
  310. stroke = new Stroke(stylusPoints);
  311. drawingAttributes = new DrawingAttributes();
  312. drawingAttributes.Color = _color;
  313. drawingAttributes.Width = _size * 4.5;
  314. drawingAttributes.Height = _size * 4.5;
  315. drawingAttributes.FitToCurve = true;
  316. drawingAttributes.IgnorePressure = false;
  317. stroke.DrawingAttributes = drawingAttributes;
  318. m_canvas.Strokes[m_canvas.Strokes.Count - 1] = stroke;
  319. }
  320. }
  321. }
  322. }
  323. //public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  324. //{
  325. // try
  326. // {
  327. // if (_new)
  328. // {
  329. // if (stroke != null && stroke.StylusPoints.Count > 1)
  330. // {
  331. // drawingAttributes = new DrawingAttributes();
  332. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  333. // drawingAttributes.Color = _color;
  334. // drawingAttributes.Width = _size * 5;
  335. // drawingAttributes.Height = _size * 5;
  336. // drawingAttributes.FitToCurve = true;
  337. // drawingAttributes.IgnorePressure = false;
  338. // stroke.DrawingAttributes = drawingAttributes;
  339. // //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  340. // m_canvas.Strokes.Add(stroke);
  341. // strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  342. // //int currCount= strokes_page_all[i].lines.Last().lines_curr.Count;
  343. // //if (currCount > 0)
  344. // //{
  345. // // strokes_page_all[i].lines.Last().lines_curr[currCount - 1] = stroke;
  346. // //}
  347. // stroke = null;
  348. // }
  349. // stylusPoints = new StylusPointCollection();
  350. // stylusPoint = new StylusPoint();
  351. // //stroke = new Stroke(stylusPoints);
  352. // //stroke = null;
  353. // }
  354. // else
  355. // {
  356. // //stylusPoints = new StylusPointCollection();
  357. // //stylusPoint = new StylusPoint();
  358. // stylusPoint.X = _x;
  359. // stylusPoint.Y = _y;
  360. // stylusPoints.Add(stylusPoint);
  361. // stroke = new Stroke(stylusPoints);
  362. // }
  363. // }
  364. // catch (Exception ex)
  365. // {
  366. // LogHelper.WriteErrLog("【XHMicroLessonSystemWindow】(changepages)点阵比书写报错:" + ex.Message, ex);
  367. // }
  368. //}
  369. }
  370. }