星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PracticeWindow.xaml.cs 35KB

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