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

BlackboardNew.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. if (pagenum + 1 > strokes_page_all.Count)
  100. {
  101. ZBBPage pagetemp = new ZBBPage();
  102. pagetemp.lines.Add(new ZBBPageStep());
  103. strokes_page_all.Add(pagetemp);
  104. }
  105. ZBBPage page = strokes_page_all[pagenum];
  106. if (page != null)
  107. {
  108. step.lines_curr.Add(m_canvas.Strokes);
  109. page.lines.Add(step);
  110. step = null;
  111. }
  112. }
  113. }
  114. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  115. {
  116. if (undoOrRedo > 0)
  117. {
  118. undoOrRedo -= 1;
  119. return;
  120. }
  121. if (step == null)
  122. {
  123. step = new ZBBPageStep();
  124. }
  125. // 笔模式
  126. if (e.Added.Count > 0 && e.Removed.Count == 0)
  127. {
  128. step.lines_add.Add(e.Added);
  129. }
  130. // 橡皮模式 会多次进入回掉
  131. else if (e.Removed.Count > 0)
  132. {
  133. step.lines_add.Add(e.Added);
  134. for (int i = 0; i < e.Removed.Count; i++)
  135. {
  136. Stroke removeItem = e.Removed[i];
  137. if (step.lines_add.Contains(removeItem))
  138. {
  139. step.lines_add.Remove(removeItem);
  140. }
  141. else
  142. {
  143. step.lines_remove.Add(removeItem);
  144. }
  145. }
  146. }
  147. }
  148. // public方法 笔
  149. public void change_pen(Color _color)
  150. {
  151. //this.type = ZPenType.Pen;
  152. DrawingAttributes drawingAttributes = new DrawingAttributes();
  153. m_canvas.DefaultDrawingAttributes = drawingAttributes;
  154. drawingAttributes.Color = _color;
  155. drawingAttributes.Width = pensize;
  156. drawingAttributes.Height = pensize;
  157. drawingAttributes.FitToCurve = true;
  158. drawingAttributes.IgnorePressure = false;
  159. m_canvas.EditingMode = InkCanvasEditingMode.Ink;
  160. }
  161. // 橡皮
  162. public void change_erase()
  163. {
  164. //this.type = ZPenType.Erase;
  165. m_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  166. m_canvas.EraserShape = new EllipseStylusShape(erasesize, erasesize, 0);
  167. }
  168. // 撤销
  169. public void undo()
  170. {
  171. ZBBPage page = strokes_page_all[pagenum];
  172. if (page != null && m_canvas.Strokes.Count > 0 && page.lines.Count > 1)
  173. {
  174. ZBBPageStep last = page.lines.Last();
  175. page.lines.Remove(last);
  176. page.lines_histoty.Add(last);
  177. if (page.lines.Last().lines_curr.Count > 0)
  178. {
  179. undoOrRedo = 2;
  180. }
  181. else
  182. {
  183. undoOrRedo = 1;
  184. }
  185. m_canvas.Strokes.Clear();
  186. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  187. }
  188. }
  189. // 恢复
  190. public void redo()
  191. {
  192. ZBBPage page = strokes_page_all[pagenum];
  193. if (page != null && page.lines_histoty.Count > 0)
  194. {
  195. ZBBPageStep line = page.lines_histoty[page.lines_histoty.Count - 1];
  196. page.lines.Add(line);
  197. page.lines_histoty.Remove(line);
  198. if (page.lines.Last().lines_curr.Count > 0)
  199. {
  200. undoOrRedo = 2;
  201. }
  202. else
  203. {
  204. undoOrRedo = 1;
  205. }
  206. m_canvas.Strokes.Clear();
  207. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  208. }
  209. }
  210. // 清空
  211. public void clear()
  212. {
  213. //ZBBPage page = new ZBBPage();
  214. //page.lines.Add(new ZBBPageStep());
  215. //page.lines = new List<ZBBPageStep>();
  216. //page.lines_histoty = new List<ZBBPageStep>();
  217. //strokes_page_all.Add(page);
  218. strokes_page_all.Clear();
  219. m_canvas.Strokes.Clear();
  220. //for (int i=0;i< strokes_page_all.Count;i++)
  221. //{
  222. // ZBBPage page = strokes_page_all[pagenum];
  223. // if (page != null)
  224. // {
  225. // m_canvas.Strokes.Clear();
  226. // page.lines_histoty.Clear();
  227. // page.lines.Clear();
  228. // page.lines.Add(new ZBBPageStep());
  229. // }
  230. //}
  231. }
  232. public void changepage(int mpagenum)
  233. {
  234. if (pagenum != mpagenum)
  235. {
  236. pagenum = mpagenum;
  237. if (pagenum >= strokes_page_all.Count)
  238. {
  239. int numadd = pagenum - strokes_page_all.Count + 1;
  240. for (int i = 0; i < numadd; i++)
  241. {
  242. ZBBPage pagetemp = new ZBBPage();
  243. pagetemp.lines.Add(new ZBBPageStep());
  244. strokes_page_all.Add(pagetemp);
  245. }
  246. }
  247. ZBBPage page = strokes_page_all[pagenum];
  248. if (page != null&& page.lines.Count>0)
  249. {
  250. if (page.lines.Last().lines_curr.Count > 0)
  251. {
  252. undoOrRedo += 1;
  253. }
  254. if (m_canvas.Strokes.Count > 0)
  255. {
  256. undoOrRedo += 1;
  257. m_canvas.Strokes.Clear();
  258. }
  259. StrokeCollection strokes = new StrokeCollection();
  260. StylusPointCollection stylusPoints = new StylusPointCollection();
  261. System.Windows.Input.StylusPointDescription stylusPointDescription = new StylusPointDescription();
  262. StylusPoint stylusPoint = new StylusPoint();
  263. stylusPoint.X = 580;
  264. stylusPoint.Y = 212;
  265. stylusPoints.Add(stylusPoint);
  266. Stroke stroke = new Stroke(stylusPoints);
  267. stylusPoint.X = 581;
  268. stylusPoint.Y = 213;
  269. stylusPoints.Add(stylusPoint);
  270. stroke = new Stroke(stylusPoints);
  271. page.lines.Last().lines_curr.Add(stroke);
  272. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  273. }
  274. }
  275. }
  276. StylusPointCollection stylusPoints = new StylusPointCollection();
  277. StylusPoint stylusPoint = new StylusPoint();
  278. Stroke stroke;
  279. bool isFirst = true;
  280. public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  281. {
  282. if (_new)
  283. {
  284. if (stroke != null && stroke.StylusPoints.Count > 1)
  285. {
  286. isFirst = true;
  287. strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  288. }
  289. stylusPoints = new StylusPointCollection();
  290. stylusPoint = new StylusPoint();
  291. stroke = null;
  292. }
  293. else
  294. {
  295. if (isFirst)
  296. {
  297. stylusPoint.X = _x;
  298. stylusPoint.Y = _y;
  299. stylusPoints.Add(stylusPoint);
  300. if (stylusPoints.Count > 1)
  301. {
  302. stroke = new Stroke(stylusPoints);
  303. drawingAttributes = new DrawingAttributes();
  304. drawingAttributes.Color = _color;
  305. drawingAttributes.Width = _size*4.5;
  306. drawingAttributes.Height = _size * 4.5;
  307. drawingAttributes.FitToCurve = true;
  308. drawingAttributes.IgnorePressure = false;
  309. stroke.DrawingAttributes = drawingAttributes;
  310. m_canvas.Strokes.Add(stroke);
  311. isFirst = false;
  312. }
  313. }
  314. else
  315. {
  316. if (m_canvas.Strokes.Count > 0)
  317. {
  318. stylusPoint.X = _x;
  319. stylusPoint.Y = _y;
  320. stylusPoints.Add(stylusPoint);
  321. stroke = new Stroke(stylusPoints);
  322. drawingAttributes = new DrawingAttributes();
  323. drawingAttributes.Color = _color;
  324. drawingAttributes.Width = _size * 4.5;
  325. drawingAttributes.Height = _size * 4.5;
  326. drawingAttributes.FitToCurve = true;
  327. drawingAttributes.IgnorePressure = false;
  328. stroke.DrawingAttributes = drawingAttributes;
  329. m_canvas.Strokes[m_canvas.Strokes.Count - 1] = stroke;
  330. }
  331. }
  332. }
  333. }
  334. //public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  335. //{
  336. // try
  337. // {
  338. // if (_new)
  339. // {
  340. // if (stroke != null && stroke.StylusPoints.Count > 1)
  341. // {
  342. // drawingAttributes = new DrawingAttributes();
  343. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  344. // drawingAttributes.Color = _color;
  345. // drawingAttributes.Width = _size * 5;
  346. // drawingAttributes.Height = _size * 5;
  347. // drawingAttributes.FitToCurve = true;
  348. // drawingAttributes.IgnorePressure = false;
  349. // stroke.DrawingAttributes = drawingAttributes;
  350. // //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  351. // m_canvas.Strokes.Add(stroke);
  352. // strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  353. // //int currCount= strokes_page_all[i].lines.Last().lines_curr.Count;
  354. // //if (currCount > 0)
  355. // //{
  356. // // strokes_page_all[i].lines.Last().lines_curr[currCount - 1] = stroke;
  357. // //}
  358. // stroke = null;
  359. // }
  360. // stylusPoints = new StylusPointCollection();
  361. // stylusPoint = new StylusPoint();
  362. // //stroke = new Stroke(stylusPoints);
  363. // //stroke = null;
  364. // }
  365. // else
  366. // {
  367. // //stylusPoints = new StylusPointCollection();
  368. // //stylusPoint = new StylusPoint();
  369. // stylusPoint.X = _x;
  370. // stylusPoint.Y = _y;
  371. // stylusPoints.Add(stylusPoint);
  372. // stroke = new Stroke(stylusPoints);
  373. // }
  374. // }
  375. // catch (Exception ex)
  376. // {
  377. // LogHelper.WriteErrLog("【XHMicroLessonSystemWindow】(changepages)点阵比书写报错:" + ex.Message, ex);
  378. // }
  379. //}
  380. }
  381. }