星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PracticeWindow.xaml.cs 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. using Common.system;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Ink;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using TStudyDigitalPen.HID;
  11. using XHWK.Model;
  12. namespace XHWK.WKTool
  13. {
  14. /// <summary>
  15. /// 录屏画板
  16. /// </summary>
  17. public partial class PracticeWindow : Window
  18. {
  19. //声明一个 DrawingAttributes 类型的变量
  20. private DrawingAttributes drawingAttributes;
  21. private DrawingAttributes drawing;
  22. private ViewModel viewModel;
  23. /// <summary>
  24. /// 0 画笔 1 矩形 2 圆形
  25. /// </summary>
  26. private int flg = 0;
  27. /// <summary>
  28. /// 当前画笔颜色
  29. /// </summary>
  30. private Color Colour = Colors.Red;
  31. /// <summary>
  32. /// 当前画笔宽
  33. /// </summary>
  34. private int Wit = 2;
  35. /// <summary>
  36. /// 当前画笔高
  37. /// </summary>
  38. private int Hei = 2;
  39. public PracticeWindow()
  40. {
  41. InitializeComponent();
  42. InitPen();
  43. }
  44. public void Initialize(string _imgPath = null)
  45. {
  46. flg = 0;
  47. blackboard_canvas.Strokes.Clear();
  48. blackboard_canvas.UseCustomCursor = true;
  49. //blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  50. //加背景 废弃更利于讲解ppt
  51. //if (File.Exists(_imgPath))
  52. //{
  53. // imgCanvas.Source = new BitmapImage(new Uri(_imgPath));
  54. //}
  55. //创建 DrawingAttributes 类的一个实例
  56. drawingAttributes = new DrawingAttributes();
  57. //drawingAttributes.StylusTip = StylusTip.Rectangle;
  58. //drawingAttributes.IsHighlighter = false;
  59. //drawingAttributes.IgnorePressure = true;
  60. ////将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
  61. ////InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
  62. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  63. Pen();
  64. //blackboard_canvas.Cursor = Cursors.Pen;
  65. //Cursor cus = new Cursor(@"G:\06.cur");
  66. //blackboard_canvas.Cursor = cus;
  67. viewModel = new ViewModel
  68. {
  69. InkStrokes = new StrokeCollection(),
  70. };
  71. DataContext = viewModel;
  72. //APP.W_ScreenRecordingToolbarWindow.Owner = this;
  73. //APP.W_ScreenRecordingToolbarWindow.Topmost = true;
  74. }
  75. /// <summary>
  76. /// 画笔颜色事件 白色
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. public void White()
  81. {
  82. //flg = 0;
  83. drawingAttributes.Color = Colors.White;
  84. Colour = Colors.White;
  85. }
  86. /// <summary>
  87. /// 画笔颜色事件 红色
  88. /// </summary>
  89. /// <param name="sender"></param>
  90. /// <param name="e"></param>
  91. public void Red()
  92. {
  93. drawingAttributes.Color = Colors.Red;
  94. Colour = Colors.Red;
  95. }
  96. /// <summary>
  97. /// 画笔颜色事件 黑色
  98. /// </summary>
  99. /// <param name="sender"></param>
  100. /// <param name="e"></param>
  101. public void Gray()
  102. {
  103. //flg = 0;
  104. drawingAttributes.Color = Colors.Black;
  105. Colour = Colors.Black;
  106. }
  107. /// <summary>
  108. /// 画笔颜色事件 青色
  109. /// </summary>
  110. /// <param name="sender"></param>
  111. /// <param name="e"></param>
  112. public void CyanBlue()
  113. {
  114. //flg = 0;
  115. drawingAttributes.Color = Colors.LimeGreen;
  116. Colour = Colors.LimeGreen;
  117. }
  118. /// <summary>
  119. /// 画笔颜色事件 黄色
  120. /// </summary>
  121. /// <param name="sender"></param>
  122. /// <param name="e"></param>
  123. public void Yellow()
  124. {
  125. //flg = 0;
  126. drawingAttributes.Color = Colors.Gold;
  127. Colour = Colors.Gold;
  128. }
  129. /// <summary>
  130. /// 画笔颜色事件 蓝色
  131. /// </summary>
  132. /// <param name="sender"></param>
  133. /// <param name="e"></param>
  134. public void Blue()
  135. {
  136. //flg = 0;
  137. drawingAttributes.Color = Colors.DeepSkyBlue;
  138. Colour = Colors.DeepSkyBlue;
  139. }
  140. /// <summary>
  141. /// 画笔粗细事件 细
  142. /// </summary>
  143. /// <param name="sender"></param>
  144. /// <param name="e"></param>
  145. public void Fine()
  146. {
  147. //flg = 0;
  148. drawingAttributes.Width = 1;
  149. drawingAttributes.Height = 1;
  150. Wit = 1;
  151. Hei = 1;
  152. }
  153. /// <summary>
  154. /// 画笔粗细事件 中
  155. /// </summary>
  156. /// <param name="sender"></param>
  157. /// <param name="e"></param>
  158. public void In()
  159. {
  160. //flg = 0;
  161. drawingAttributes.Width = 3;
  162. drawingAttributes.Height = 3;
  163. Wit = 3;
  164. Hei = 3;
  165. }
  166. /// <summary>
  167. /// 画笔粗细事件 粗
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. public void Crude()
  172. {
  173. //flg = 0;
  174. drawingAttributes.Width = 5;
  175. drawingAttributes.Height = 5;
  176. Wit = 5;
  177. Hei = 5;
  178. }
  179. /// <summary>
  180. /// 橡皮
  181. /// </summary>
  182. public void Eraser()
  183. {
  184. flg = 0;
  185. //this.type = ZPenType.Erase;
  186. blackboard_canvas.UseCustomCursor = false;
  187. blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  188. blackboard_canvas.EraserShape = new EllipseStylusShape(64, 64, 0);
  189. }
  190. /// <summary>
  191. /// 画笔
  192. /// </summary>
  193. public void Pen()
  194. {
  195. flg = 0;
  196. blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
  197. blackboard_canvas.UseCustomCursor = true;
  198. drawingAttributes.FitToCurve = true;
  199. drawingAttributes.IgnorePressure = false;
  200. blackboard_canvas.Cursor = Cursors.Pen;
  201. }
  202. /// <summary>
  203. /// 圆
  204. /// </summary>
  205. public void Round()
  206. {
  207. flg = 2;
  208. drawingAttributes = new DrawingAttributes
  209. {
  210. Color = Colour,
  211. Width = Wit,
  212. Height = Hei,
  213. StylusTip = StylusTip.Rectangle,
  214. //FitToCurve = true,
  215. IsHighlighter = false,
  216. IgnorePressure = true,
  217. };
  218. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  219. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  220. blackboard_canvas.Cursor = Cursors.Cross;
  221. }
  222. /// <summary>
  223. /// 矩形
  224. /// </summary>
  225. public void Rectangle()
  226. {
  227. flg = 1;
  228. drawingAttributes = new DrawingAttributes
  229. {
  230. Color = Colour,
  231. Width = Wit,
  232. Height = Hei,
  233. StylusTip = StylusTip.Rectangle,
  234. //FitToCurve = true,
  235. IsHighlighter = false,
  236. IgnorePressure = true,
  237. };
  238. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  239. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  240. blackboard_canvas.Cursor = Cursors.Cross;
  241. }
  242. private System.Windows.Point iniP;
  243. private void blackboard_canvas_MouseDown(object sender, MouseButtonEventArgs e)
  244. {
  245. if (flg != 0)
  246. {
  247. if (e.LeftButton == MouseButtonState.Pressed)
  248. {
  249. iniP = e.GetPosition(blackboard_canvas);
  250. }
  251. }
  252. }
  253. private Stroke StrokeRound;
  254. private Stroke StrokeRectangle;
  255. private void blackboard_canvas_MouseMove(object sender, MouseEventArgs e)
  256. {
  257. if (flg != 0)
  258. {
  259. if (e.LeftButton == MouseButtonState.Pressed)
  260. {
  261. // Draw square
  262. if (flg == 1)
  263. {
  264. System.Windows.Point endP = e.GetPosition(blackboard_canvas);
  265. List<System.Windows.Point> pointList = new List<System.Windows.Point>
  266. {
  267. new System.Windows.Point(iniP.X, iniP.Y),
  268. new System.Windows.Point(iniP.X, endP.Y),
  269. new System.Windows.Point(endP.X, endP.Y),
  270. new System.Windows.Point(endP.X, iniP.Y),
  271. new System.Windows.Point(iniP.X, iniP.Y),
  272. };
  273. //Stroke stroke1 = new Stroke(drawingAttributesRound);
  274. StylusPointCollection point = new StylusPointCollection(pointList);
  275. Stroke stroke = new Stroke(point)
  276. {
  277. DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
  278. };
  279. if (StrokeRectangle != null)
  280. {
  281. viewModel.InkStrokes.Remove(StrokeRectangle);
  282. }
  283. StrokeRectangle = stroke;
  284. viewModel.InkStrokes.Add(stroke);
  285. }
  286. // Draw Eclipse
  287. else if (flg == 2)
  288. {
  289. System.Windows.Point endP = e.GetPosition(blackboard_canvas);
  290. List<System.Windows.Point> pointList = GenerateEclipseGeometry(iniP, endP);
  291. StylusPointCollection point = new StylusPointCollection(pointList);
  292. Stroke stroke = new Stroke(point)
  293. {
  294. DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
  295. };
  296. //viewModel.InkStrokes.Clear();
  297. if (StrokeRound != null)
  298. {
  299. viewModel.InkStrokes.Remove(StrokeRound);
  300. }
  301. StrokeRound = stroke;
  302. viewModel.InkStrokes.Add(stroke);
  303. }
  304. }
  305. }
  306. }
  307. private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed)
  308. {
  309. double a = 0.5 * (ed.X - st.X);
  310. double b = 0.5 * (ed.Y - st.Y);
  311. List<System.Windows.Point> pointList = new List<System.Windows.Point>();
  312. for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
  313. {
  314. pointList.Add(new System.Windows.Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
  315. }
  316. return pointList;
  317. }
  318. /// <summary>
  319. /// 鼠标松开时
  320. /// </summary>
  321. /// <param name="sender"></param>
  322. /// <param name="e"></param>
  323. private void blackboard_canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  324. {
  325. StrokeRound = null;
  326. StrokeRectangle = null;
  327. }
  328. /// <summary>
  329. /// 鼠标右键
  330. /// </summary>
  331. /// <param name="sender"></param>
  332. /// <param name="e"></param>
  333. private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  334. {
  335. if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
  336. {
  337. APP.W_ScreenRecordingToolbarWindow.flg = 0;
  338. }
  339. else
  340. {
  341. APP.W_ScreenRecordingToolbarWindow.flg = 1;
  342. }
  343. Hide();
  344. }
  345. /// <summary>
  346. /// 鼠标中建点击
  347. /// </summary>
  348. void SwitchPages()
  349. {
  350. try
  351. {
  352. new Thread(() =>
  353. {
  354. Thread.Sleep(500);
  355. MouseEventCommon.MouseMiddleClickEvent(0);
  356. }).Start();
  357. }
  358. catch (Exception ex)
  359. {
  360. MessageBox.Show(ex.Message);
  361. }
  362. }
  363. private void blackboard_canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  364. {
  365. if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
  366. {
  367. APP.W_ScreenRecordingToolbarWindow.flg = 0;
  368. }
  369. else
  370. {
  371. APP.W_ScreenRecordingToolbarWindow.flg = 1;
  372. }
  373. Hide();
  374. }
  375. private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  376. {
  377. if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
  378. {
  379. APP.W_ScreenRecordingToolbarWindow.flg = 0;
  380. }
  381. else
  382. {
  383. APP.W_ScreenRecordingToolbarWindow.flg = 1;
  384. }
  385. ReturnPractice();
  386. //模拟切到ppt
  387. SwitchPages();
  388. //Hide();
  389. }
  390. /// <summary>
  391. /// 退出批注并清空画板
  392. /// </summary>
  393. public void ReturnPractice()
  394. {
  395. blackboard_canvas.Strokes.Clear();
  396. new Thread(new ThreadStart(new Action(() =>
  397. {
  398. Dispatcher.Invoke(() =>
  399. {
  400. APP.W_ScreenRecordingToolbarWindow.ModifyState();
  401. });
  402. flg = 0;
  403. Thread.Sleep(500);
  404. Dispatcher.Invoke(() =>
  405. {
  406. Owner = null;
  407. APP.W_PracticeWindow.Hide();
  408. });
  409. }))).Start();
  410. }
  411. #region 点阵笔相关
  412. #region 值初始化
  413. // 不同尺寸点阵纸点阵宽高尺寸计算方法为:纸张物理尺寸(毫米)/0.3 *8,详见 开发必读.pdf 文档
  414. /// <summary>
  415. /// A4点阵纸点阵宽度
  416. /// </summary>
  417. private const int A4_WIDTH = 5600;
  418. /// <summary>
  419. /// A4点阵纸点阵高度
  420. /// </summary>
  421. private const int A4_HEIGHT = 7920;
  422. ///// <summary>
  423. ///// 画板
  424. ///// </summary>
  425. //private Graphics graphics;
  426. /// <summary>
  427. /// 笔画坐标数组
  428. /// </summary>
  429. private List<System.Drawing.Point> stroke;
  430. /// <summary>
  431. /// 笔序列号
  432. /// </summary>
  433. private string penSerial;
  434. /// <summary>
  435. /// 笔是否在点
  436. /// </summary>
  437. private bool isPenDown;
  438. //当前点阵地址
  439. private string currentPageSerial = string.Empty;
  440. //不同点阵地址对应的笔迹绘制图片,用于实现在不同点阵地址书写切换时,显示书写内容自动切换
  441. //本例图片放在内存中存储,对于大量或者需要在多个点阵地址对应图片进行切换演示,建议将图片存储到文件,以免内存溢出
  442. private Dictionary<string, System.Drawing.Image> pagesDic = new Dictionary<string, System.Drawing.Image>();
  443. #endregion
  444. public void InitPen()
  445. {
  446. stroke = new List<System.Drawing.Point>();
  447. //获取点阵笔实例,并绑定点阵笔事件
  448. //将授权文件内容传入,获取点阵笔对象实例
  449. APP.digitalPen = DigitalPenHID.GetInstance(certificates.MyLicense.Bytes);
  450. //绑定笔连接事件
  451. APP.digitalPen.PenConnected += OnPenConnect;
  452. //绑定笔断开事件
  453. APP.digitalPen.PenDisconnect += OnPenDisconnect;
  454. //绑定笔书写输出坐标事件
  455. APP.digitalPen.PenCoordinate += OnPenCoordinate;
  456. //绑定抬笔事件
  457. APP.digitalPen.PenUp += OnPenUp;
  458. //绑定落笔事件
  459. APP.digitalPen.PenDown += OnPenDown;
  460. APP.digitalPen.PenBatteryCapacity += OnBatteryCapacity;
  461. APP.digitalPen.PenMemoryFillLevel += OnMemoryFillLevel;
  462. //完成初始化点阵笔,开始与点阵笔通信
  463. ERROR_CODE ER = APP.digitalPen.Start();
  464. ////绑定笔在新的点阵地址页面书写事件
  465. //APP.digitalPen.PenNewPage += APP.digitalPen_OnPenNewPage;
  466. ////绑定笔信息事件
  467. //APP.digitalPen.PenInfo += APP.digitalPen_OnPenInfo;
  468. //启动接收笔数据,完成初始化工作
  469. //ERROR_CODE rc = APP.digitalPen.Start();
  470. //判断是否成功
  471. if (ER != ERROR_CODE.ERROR_OK)
  472. {
  473. MessageWindow.Show("初始化失败,授权过期,返回值:" + ER.ToString());
  474. }
  475. }
  476. /// <summary>
  477. /// 落笔
  478. /// </summary>
  479. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  480. /// <param name="penSerial">点阵笔序列号</param>
  481. /// <param name="penType">点阵笔型号编号</param>
  482. private void OnPenDown(ulong time, string penSerial, int penType)
  483. {
  484. if (CheckAccess())
  485. {
  486. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDown);
  487. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  488. }
  489. else
  490. {
  491. isPenDown = true;
  492. }
  493. }
  494. /// <summary>
  495. /// 抬笔
  496. /// </summary>
  497. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  498. /// <param name="penSerial">点阵笔序列号</param>
  499. /// <param name="penType">点阵笔型号编号</param>
  500. private void OnPenUp(ulong time, string penSerial, int penType)
  501. {
  502. if (CheckAccess())
  503. {
  504. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenUp);
  505. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  506. }
  507. else
  508. {
  509. isPenDown = false;
  510. APP.PenSerial = penSerial;
  511. stroke.Clear();
  512. }
  513. //if (APP.pageData.currpage > 0)
  514. //{
  515. // Dispatcher.Invoke(new Action(() =>
  516. // {
  517. // myblackboard.changepages(0, 0, true, Color, PenSize, APP.pageData.currpage - 1, 0);
  518. // }));
  519. //}
  520. if (strokes != null && strokes.StylusPoints.Count > 1)
  521. {
  522. isFirst = true;
  523. }
  524. stylusPoints = new StylusPointCollection();
  525. stylusPoint = new StylusPoint();
  526. strokes = null;
  527. }
  528. /// <summary>
  529. /// 笔断开
  530. /// </summary>
  531. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  532. /// <param name="penSerial">点阵笔序列号</param>
  533. /// <param name="penType">点阵笔型号编号</param>
  534. private void OnPenDisconnect(ulong time, string penSerial, int penType)
  535. {
  536. if (CheckAccess())
  537. {
  538. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDisconnect);
  539. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  540. }
  541. else
  542. {
  543. APP.PenSerial = penSerial;
  544. APP.PenStatus = false;
  545. //UpdateDevStatus();
  546. ////Dispatcher.Invoke(new Action(() =>
  547. ////{
  548. //// txbNotConnected.Text = "未连接";
  549. //// txbNotConnecteds.Text = "未连接";
  550. ////}));
  551. }
  552. }
  553. /// <summary>
  554. /// 笔连接
  555. /// </summary>
  556. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  557. /// <param name="penSerial">点阵笔序列号</param>
  558. /// <param name="penType">点阵笔型号编号</param>
  559. private void OnPenConnect(ulong time, string penSerial, int penType)
  560. {
  561. if (CheckAccess())
  562. {
  563. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenConnect);
  564. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  565. }
  566. else
  567. {
  568. APP.PenSerial = penSerial;
  569. APP.PenStatus = true;
  570. this.penSerial = penSerial;
  571. //连接后,在获取笔数据前,可以清除笔内的历史数据
  572. //APP.digitalPen.ClearMemory(penSerial);
  573. //开始接收笔数据
  574. APP.digitalPen.GetPenData(penSerial);
  575. //UpdateDevStatus();
  576. ////Dispatcher.Invoke(new Action(() =>
  577. ////{
  578. //// txbNotConnected.Text = "已连接";
  579. //// txbNotConnecteds.Text = "已连接";
  580. ////}));
  581. }
  582. }
  583. /// <summary>
  584. /// 电池电量
  585. /// </summary>
  586. /// <param name="time"></param>
  587. /// <param name="penSerial"></param>
  588. /// <param name="penType"></param>
  589. /// <param name="capacity"></param>
  590. private void OnBatteryCapacity(ulong time, string penSerial, int penType, byte capacity)
  591. {
  592. if (CheckAccess())
  593. {
  594. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnBatteryCapacity);
  595. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, capacity });
  596. }
  597. else
  598. {
  599. //System.Windows.MessageWindow.Show("电池电量:" + capacity.ToString());
  600. }
  601. }
  602. /// <summary>
  603. /// 已用存储
  604. /// </summary>
  605. /// <param name="time"></param>
  606. /// <param name="penSerial"></param>
  607. /// <param name="penType"></param>
  608. /// <param name="fillLevel"></param>
  609. private void OnMemoryFillLevel(ulong time, string penSerial, int penType, byte fillLevel)
  610. {
  611. if (CheckAccess())
  612. {
  613. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnMemoryFillLevel);
  614. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, fillLevel });
  615. }
  616. else
  617. {
  618. //System.Windows.MessageWindow.Show("存储:" + fillLevel.ToString());
  619. }
  620. }
  621. /// <summary>
  622. /// 笔书写,收到坐标
  623. /// </summary>
  624. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  625. /// <param name="penSerial">点阵笔序列号</param>
  626. /// <param name="penType">点阵笔型号编号</param>
  627. /// <param name="pageSerial">点阵地址</param>
  628. /// <param name="cx">x坐标</param>
  629. /// <param name="cy">y坐标</param>
  630. /// <param name="force">压力值</param>
  631. private void OnPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
  632. {
  633. if (CheckAccess())
  634. {
  635. Action<ulong, string, int, string, int, int, byte> ac = new Action<ulong, string, int, string, int, int, byte>(OnPenCoordinate);
  636. Dispatcher.Invoke(ac, new object[] { time, pageSerial, penType, pageSerial, cx, cy, force });
  637. }
  638. else
  639. {
  640. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  641. if (!isPenDown)
  642. {
  643. return;
  644. }
  645. stroke.Add(new System.Drawing.Point(cx, cy));
  646. double PropW = blackboard_canvas.ActualWidth / A4_HEIGHT;
  647. double PropH = blackboard_canvas.ActualHeight / A4_WIDTH;
  648. //点
  649. double testX = cy * PropW;
  650. double testY = (A4_WIDTH - cx) * PropH;
  651. //pageSerial //点阵IP地址 与打印的页面关联
  652. if (true)
  653. {
  654. Dispatcher.Invoke(new Action(() =>
  655. {
  656. float Pressure = force / 100f;
  657. ////添加笔画
  658. //myblackboard.changepages(testX, testY, false, Color, PenSize, APP.pageData.currpage - 1, Pressure);
  659. if (isFirst)
  660. {
  661. stylusPoint.X = testX;
  662. stylusPoint.Y = testY;
  663. //_color.A = (byte)(Pressure * 255f);
  664. //stylusPoint.PressureFactor = Pressure;
  665. //for(int i=0;i<100;i++)
  666. //{
  667. // stylusPoint.X--;
  668. // stylusPoint.Y--;
  669. // stylusPoints.Add(stylusPoint);
  670. //}
  671. stylusPoints.Add(stylusPoint);
  672. if (stylusPoints.Count > 1)
  673. {
  674. drawing = new DrawingAttributes
  675. {
  676. Color = Colour,
  677. Width = Wit * 4.5,
  678. Height = Wit * 4.5,
  679. FitToCurve = true,
  680. IgnorePressure = false
  681. };
  682. //blackboard_canvas.Strokes = new StrokeCollection();
  683. strokes = new Stroke(stylusPoints);
  684. strokes.DrawingAttributes = drawing;
  685. //blackboard_canvas.Strokes.Add(strokes);
  686. blackboard_canvas.Strokes.Add(strokes);
  687. isFirst = false;
  688. }
  689. }
  690. else
  691. {
  692. if (blackboard_canvas.Strokes.Count>0)
  693. {
  694. stylusPoint.X = testX;
  695. stylusPoint.Y = testY;
  696. stylusPoints.Add(stylusPoint);
  697. strokes = new Stroke(stylusPoints);
  698. drawing = new DrawingAttributes
  699. {
  700. Color = Colour,
  701. Width = Wit * 4.5,
  702. Height = Wit * 4.5,
  703. FitToCurve = true,
  704. IgnorePressure = false
  705. };
  706. strokes.DrawingAttributes = drawing;
  707. //viewModel.InkStrokes.Add(strokes);
  708. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
  709. }
  710. }
  711. //设置鼠标位置
  712. if (testX > 0 && testY > 0)
  713. {
  714. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  715. SetCursorPos((int)((float)testX* (PrimaryScreen.DpiX / 96f)), (int)((float)testY* (PrimaryScreen.DpiY / 96f)));
  716. }
  717. }));
  718. }
  719. }
  720. }
  721. bool isFirst = true;
  722. private StylusPointCollection stylusPoints = new StylusPointCollection();
  723. private StylusPoint stylusPoint = new StylusPoint();
  724. private Stroke strokes;
  725. /// <summary>
  726. /// 停止笔
  727. /// </summary>
  728. public void StopDigitalPen()
  729. {
  730. //停止,释放资源
  731. APP.digitalPen.Stop();
  732. }
  733. /// <summary>
  734. /// 清空笔内存储
  735. /// </summary>
  736. public void ClearPenStorage()
  737. {
  738. if (!string.IsNullOrEmpty(penSerial))
  739. {
  740. APP.digitalPen.ClearMemory(penSerial);
  741. }
  742. }
  743. /// <summary>
  744. /// 获取剩余电量
  745. /// </summary>
  746. public void GetPenElectricityQuantity()
  747. {
  748. if (!string.IsNullOrEmpty(penSerial))
  749. {
  750. APP.digitalPen.GetBatteryCapacity(penSerial);
  751. }
  752. }
  753. /// <summary>
  754. /// 获取存储空间
  755. /// </summary>
  756. public void GetUsedStorage()
  757. {
  758. if (!string.IsNullOrEmpty(penSerial))
  759. {
  760. APP.digitalPen.GetMemoryFillLevel(penSerial);
  761. }
  762. }
  763. /// <summary>
  764. /// 开启悬浮
  765. /// </summary>
  766. public void 开启悬浮()
  767. {
  768. if (!string.IsNullOrEmpty(penSerial))
  769. {
  770. APP.digitalPen.SetPenHoverMode(true, penSerial);
  771. }
  772. }
  773. /// <summary>
  774. /// 关闭悬浮
  775. /// </summary>
  776. public void 关闭悬浮()
  777. {
  778. if (!string.IsNullOrEmpty(penSerial))
  779. {
  780. APP.digitalPen.SetPenHoverMode(false, penSerial);
  781. }
  782. }
  783. /// <summary>
  784. /// 引用user32.dll动态链接库(windows api),
  785. /// 使用库中定义 API:SetCursorPos
  786. /// 设置光标位置
  787. /// </summary>
  788. [System.Runtime.InteropServices.DllImport("user32.dll")]
  789. private static extern int SetCursorPos(int x, int y);
  790. #endregion
  791. }
  792. }