星火批注
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 23KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. using Common.system;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Ink;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using TStudyDigitalPen.HID;
  18. namespace XHPZ.Desktop
  19. {
  20. /// <summary>
  21. /// PracticeWindow.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class PracticeWindow : Window
  24. {
  25. private DrawingAttributes drawingAttributes;
  26. private DrawingAttributes drawing;
  27. /// <summary>
  28. /// 屏幕宽
  29. /// </summary>
  30. internal double pwidth = SystemParameters.PrimaryScreenWidth;
  31. /// <summary>
  32. /// 屏幕高
  33. /// </summary>
  34. internal double pHeight = SystemParameters.PrimaryScreenHeight;
  35. /// <summary>
  36. /// 当前画笔颜色
  37. /// </summary>
  38. private Color Colour = Colors.Red;
  39. /// <summary>
  40. /// 当前画笔宽
  41. /// </summary>
  42. private int Wit = 2;
  43. /// <summary>
  44. /// A4点阵纸点阵宽度
  45. /// </summary>
  46. private const int A4_WIDTH = 5600;
  47. /// <summary>
  48. /// A4点阵纸点阵高度
  49. /// </summary>
  50. private const int A4_HEIGHT = 7920;
  51. /// <summary>
  52. /// 引用user32.dll动态链接库(windows api),
  53. /// 使用库中定义 API:SetCursorPos
  54. /// 设置光标位置
  55. /// </summary>
  56. [System.Runtime.InteropServices.DllImport("user32.dll")]
  57. private static extern int SetCursorPos(int x, int y);
  58. public PracticeWindow()
  59. {
  60. InitializeComponent();
  61. InitPen();
  62. InitTQLPPen();
  63. }
  64. /// <summary>
  65. /// 初始化
  66. /// </summary>
  67. /// <param name="_imgPath"></param>
  68. public void Initialize(string _imgPath = null)
  69. {
  70. blackboard_canvas.Strokes.Clear();
  71. blackboard_canvas.UseCustomCursor = true;
  72. //创建 DrawingAttributes 类的一个实例
  73. drawingAttributes = new DrawingAttributes();
  74. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  75. Pen();
  76. }
  77. public void InitPen()
  78. {
  79. //stroke = new List<System.Drawing.Point>();
  80. //获取点阵笔实例,并绑定点阵笔事件
  81. //将授权文件内容传入,获取点阵笔对象实例
  82. APP.digitalPen = DigitalPenHID.GetInstance(certificates.MyLicense.Bytes);
  83. //绑定笔连接事件
  84. APP.digitalPen.PenConnected += OnPenConnect;
  85. //绑定笔断开事件
  86. APP.digitalPen.PenDisconnect += OnPenDisconnect;
  87. //绑定笔书写输出坐标事件
  88. APP.digitalPen.PenCoordinate += OnPenCoordinate;
  89. //绑定抬笔事件
  90. APP.digitalPen.PenUp += OnPenUp;
  91. //绑定落笔事件
  92. APP.digitalPen.PenDown += OnPenDown;
  93. APP.digitalPen.PenBatteryCapacity += OnBatteryCapacity;
  94. APP.digitalPen.PenMemoryFillLevel += OnMemoryFillLevel;
  95. //完成初始化点阵笔,开始与点阵笔通信
  96. ERROR_CODE ER = APP.digitalPen.Start();
  97. //判断是否成功
  98. //if (ER != ERROR_CODE.ERROR_OK)
  99. //{
  100. // MessageWindow.Show("初始化失败,授权过期,返回值:" + ER.ToString());
  101. //}
  102. }
  103. /// <summary>
  104. /// 画笔
  105. /// </summary>
  106. public void Pen()
  107. {
  108. blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
  109. blackboard_canvas.UseCustomCursor = true;
  110. drawingAttributes.FitToCurve = true;
  111. drawingAttributes.IgnorePressure = false;
  112. blackboard_canvas.Cursor = Cursors.Pen;
  113. }
  114. /// <summary>
  115. /// 笔连接
  116. /// </summary>
  117. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  118. /// <param name="penSerial">点阵笔序列号</param>
  119. /// <param name="penType">点阵笔型号编号</param>
  120. private void OnPenConnect(ulong time, string penSerial, int penType)
  121. {
  122. if (CheckAccess())
  123. {
  124. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenConnect);
  125. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  126. }
  127. else
  128. {
  129. APP.PenSerial = penSerial;
  130. APP.PenStatus = true;
  131. this.penSerial = penSerial;
  132. //连接后,在获取笔数据前,可以清除笔内的历史数据
  133. //APP.digitalPen.ClearMemory(penSerial);
  134. //开始接收笔数据
  135. APP.digitalPen.GetPenData(penSerial);
  136. APP.W_mainWindow.UpdateDevStatus();
  137. ////Dispatcher.Invoke(new Action(() =>
  138. ////{
  139. //// txbNotConnected.Text = "已连接";
  140. //// txbNotConnecteds.Text = "已连接";
  141. ////}));
  142. }
  143. }
  144. /// <summary>
  145. /// 笔断开
  146. /// </summary>
  147. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  148. /// <param name="penSerial">点阵笔序列号</param>
  149. /// <param name="penType">点阵笔型号编号</param>
  150. private void OnPenDisconnect(ulong time, string penSerial, int penType)
  151. {
  152. if (CheckAccess())
  153. {
  154. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDisconnect);
  155. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  156. }
  157. else
  158. {
  159. APP.PenSerial = penSerial;
  160. APP.PenStatus = false;
  161. APP.W_mainWindow.UpdateDevStatus();
  162. //UpdateDevStatus();
  163. ////Dispatcher.Invoke(new Action(() =>
  164. ////{
  165. //// txbNotConnected.Text = "未连接";
  166. //// txbNotConnecteds.Text = "未连接";
  167. ////}));
  168. }
  169. }
  170. /// <summary>
  171. /// 笔序列号
  172. /// </summary>
  173. private string penSerial;
  174. /// <summary>
  175. /// 笔是否在点
  176. /// </summary>
  177. private bool isPenDown;
  178. /// <summary>
  179. /// 笔书写,收到坐标
  180. /// </summary>
  181. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  182. /// <param name="penSerial">点阵笔序列号</param>
  183. /// <param name="penType">点阵笔型号编号</param>
  184. /// <param name="pageSerial">点阵地址</param>
  185. /// <param name="cx">x坐标</param>
  186. /// <param name="cy">y坐标</param>
  187. /// <param name="force">压力值</param>
  188. private void OnPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
  189. {
  190. if (this.Visibility == Visibility.Visible)
  191. {
  192. if (CheckAccess())
  193. {
  194. Action<ulong, string, int, string, int, int, byte> ac = new Action<ulong, string, int, string, int, int, byte>(OnPenCoordinate);
  195. Dispatcher.Invoke(ac, new object[] { time, pageSerial, penType, pageSerial, cx, cy, force });
  196. }
  197. else
  198. {
  199. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  200. if (!isPenDown)
  201. {
  202. return;
  203. }
  204. //stroke.Add(new System.Drawing.Point(cx, cy));
  205. double PropW = blackboard_canvas.ActualWidth / A4_HEIGHT;
  206. double PropH = blackboard_canvas.ActualHeight / A4_WIDTH;
  207. //点
  208. double testX = cy * PropW;
  209. double testY = (A4_WIDTH - cx) * PropH;
  210. //pageSerial //点阵IP地址 与打印的页面关联
  211. if (true)
  212. {
  213. Dispatcher.Invoke(new Action(() =>
  214. {
  215. float Pressure = force / 100f;
  216. ////添加笔画
  217. //myblackboard.changepages(testX, testY, false, Color, PenSize, APP.pageData.currpage - 1, Pressure);
  218. if (isFirst)
  219. {
  220. stylusPoint.X = testX;
  221. stylusPoint.Y = testY;
  222. //_color.A = (byte)(Pressure * 255f);
  223. //stylusPoint.PressureFactor = Pressure;
  224. //for(int i=0;i<100;i++)
  225. //{
  226. // stylusPoint.X--;
  227. // stylusPoint.Y--;
  228. // stylusPoints.Add(stylusPoint);
  229. //}
  230. stylusPoints.Add(stylusPoint);
  231. if (stylusPoints.Count > 1)
  232. {
  233. drawing = new DrawingAttributes
  234. {
  235. Color = Colour,
  236. Width = Wit * 4.5,
  237. Height = Wit * 4.5,
  238. FitToCurve = true,
  239. IgnorePressure = false
  240. };
  241. //blackboard_canvas.Strokes = new StrokeCollection();
  242. strokes = new Stroke(stylusPoints);
  243. strokes.DrawingAttributes = drawing;
  244. //blackboard_canvas.Strokes.Add(strokes);
  245. blackboard_canvas.Strokes.Add(strokes);
  246. isFirst = false;
  247. }
  248. }
  249. else
  250. {
  251. if (blackboard_canvas.Strokes.Count > 0)
  252. {
  253. stylusPoint.X = testX;
  254. stylusPoint.Y = testY;
  255. stylusPoints.Add(stylusPoint);
  256. strokes = new Stroke(stylusPoints);
  257. drawing = new DrawingAttributes
  258. {
  259. Color = Colour,
  260. Width = Wit * 4.5,
  261. Height = Wit * 4.5,
  262. FitToCurve = true,
  263. IgnorePressure = false
  264. };
  265. strokes.DrawingAttributes = drawing;
  266. //viewModel.InkStrokes.Add(strokes);
  267. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
  268. }
  269. }
  270. //设置鼠标位置
  271. if (testX > 0 && testY > 0)
  272. {
  273. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  274. SetCursorPos((int)((float)testX * (PrimaryScreen.DpiX / 96f)), (int)((float)testY * (PrimaryScreen.DpiY / 96f)));
  275. }
  276. }));
  277. }
  278. }
  279. }
  280. }
  281. bool isFirst = true;
  282. private StylusPointCollection stylusPoints = new StylusPointCollection();
  283. private StylusPoint stylusPoint = new StylusPoint();
  284. private Stroke strokes;
  285. /// <summary>
  286. /// 抬笔
  287. /// </summary>
  288. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  289. /// <param name="penSerial">点阵笔序列号</param>
  290. /// <param name="penType">点阵笔型号编号</param>
  291. private void OnPenUp(ulong time, string penSerial, int penType)
  292. {
  293. if (CheckAccess())
  294. {
  295. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenUp);
  296. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  297. }
  298. else
  299. {
  300. isPenDown = false;
  301. APP.PenSerial = penSerial;
  302. //stroke.Clear();
  303. }
  304. //if (APP.pageData.currpage > 0)
  305. //{
  306. // Dispatcher.Invoke(new Action(() =>
  307. // {
  308. // myblackboard.changepages(0, 0, true, Color, PenSize, APP.pageData.currpage - 1, 0);
  309. // }));
  310. //}
  311. if (strokes != null && strokes.StylusPoints.Count > 1)
  312. {
  313. isFirst = true;
  314. }
  315. stylusPoints = new StylusPointCollection();
  316. stylusPoint = new StylusPoint();
  317. strokes = null;
  318. }
  319. /// <summary>
  320. /// 落笔
  321. /// </summary>
  322. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  323. /// <param name="penSerial">点阵笔序列号</param>
  324. /// <param name="penType">点阵笔型号编号</param>
  325. private void OnPenDown(ulong time, string penSerial, int penType)
  326. {
  327. if (CheckAccess())
  328. {
  329. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDown);
  330. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  331. }
  332. else
  333. {
  334. isPenDown = true;
  335. if (APP.W_PracticeWindow != null && APP.W_PracticeWindow.Visibility == Visibility.Hidden)
  336. {
  337. PenPractice();
  338. }
  339. }
  340. }
  341. void PenPractice()
  342. {
  343. Dispatcher.Invoke(() =>
  344. {
  345. if (APP.W_PracticeWindow == null)
  346. {
  347. APP.W_PracticeWindow = new PracticeWindow
  348. {
  349. Width = pwidth,
  350. Height = pHeight,
  351. Left = 0,
  352. Top = 0
  353. };
  354. }
  355. APP.W_PracticeWindow.Topmost = true;
  356. APP.W_PracticeWindow.Initialize();
  357. APP.W_PracticeWindow.Show();
  358. });
  359. }
  360. /// <summary>
  361. /// 电池电量
  362. /// </summary>
  363. /// <param name="time"></param>
  364. /// <param name="penSerial"></param>
  365. /// <param name="penType"></param>
  366. /// <param name="capacity"></param>
  367. private void OnBatteryCapacity(ulong time, string penSerial, int penType, byte capacity)
  368. {
  369. if (CheckAccess())
  370. {
  371. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnBatteryCapacity);
  372. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, capacity });
  373. }
  374. else
  375. {
  376. //System.Windows.MessageWindow.Show("电池电量:" + capacity.ToString());
  377. }
  378. }
  379. /// <summary>
  380. /// 已用存储
  381. /// </summary>
  382. /// <param name="time"></param>
  383. /// <param name="penSerial"></param>
  384. /// <param name="penType"></param>
  385. /// <param name="fillLevel"></param>
  386. private void OnMemoryFillLevel(ulong time, string penSerial, int penType, byte fillLevel)
  387. {
  388. if (CheckAccess())
  389. {
  390. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnMemoryFillLevel);
  391. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, fillLevel });
  392. }
  393. else
  394. {
  395. //System.Windows.MessageWindow.Show("存储:" + fillLevel.ToString());
  396. }
  397. }
  398. /// <summary>
  399. /// 退出批注
  400. /// </summary>
  401. /// <param name="sender"></param>
  402. /// <param name="e"></param>
  403. private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  404. {
  405. ReturnPractice();
  406. SwitchPages();
  407. }
  408. /// <summary>
  409. /// 退出批注并清空画板
  410. /// </summary>
  411. public void ReturnPractice()
  412. {
  413. blackboard_canvas.Strokes.Clear();
  414. new Thread(new ThreadStart(new Action(() =>
  415. {
  416. Thread.Sleep(500);
  417. Dispatcher.Invoke(() =>
  418. {
  419. Owner = null;
  420. APP.W_PracticeWindow.Hide();
  421. });
  422. }))).Start();
  423. }
  424. /// <summary>
  425. /// 鼠标中建点击
  426. /// </summary>
  427. void SwitchPages()
  428. {
  429. try
  430. {
  431. new Thread(() =>
  432. {
  433. Thread.Sleep(500);
  434. MouseEventCommon.MouseMiddleClickEvent(0);
  435. }).Start();
  436. }
  437. catch (Exception ex)
  438. {
  439. MessageBox.Show(ex.Message);
  440. }
  441. }
  442. #region 腾千里手写笔
  443. /// <summary>
  444. /// 是否在书写
  445. /// </summary>
  446. bool isTQLPenDown = false;
  447. /// <summary>
  448. /// TQL点阵笔宽高
  449. /// </summary>
  450. static float TQLA4_WIDTH = (float)1075 / (float)150 * (float)25.4 / (float)1.524; //OID4
  451. static float TQLA4_HEIGHT = (float)1512 / (float)150 * (float)25.4 / (float)1.524;
  452. /// <summary>
  453. /// 初始化
  454. /// </summary>
  455. public void InitTQLPPen()
  456. {
  457. if (APP.TQLPenevents == null)
  458. {
  459. APP.TQLPenevents = new PenEvents();
  460. }
  461. }
  462. /// <summary>
  463. /// 笔落下
  464. /// </summary>
  465. public void TQLDown()
  466. {
  467. isTQLPenDown = true;
  468. if(APP.W_PracticeWindow!=null&&APP.W_PracticeWindow.Visibility==Visibility.Hidden)
  469. {
  470. PenPractice();
  471. }
  472. if (strokes != null && strokes.StylusPoints.Count > 1)
  473. {
  474. isFirst = true;
  475. }
  476. stylusPoints = new StylusPointCollection();
  477. stylusPoint = new StylusPoint();
  478. strokes = null;
  479. }
  480. /// <summary>
  481. /// 笔抬起
  482. /// </summary>
  483. public void TQLUp()
  484. {
  485. isTQLPenDown = false;
  486. if (strokes != null && strokes.StylusPoints.Count > 1)
  487. {
  488. isFirst = true;
  489. }
  490. stylusPoints = new StylusPointCollection();
  491. stylusPoint = new StylusPoint();
  492. strokes = null;
  493. }
  494. /// <summary>
  495. /// 笔书写
  496. /// </summary>
  497. /// <param name="cx"></param>
  498. /// <param name="cy"></param>
  499. /// <param name="force"></param>
  500. public void TQLPenWrite(double cx, double cy, int force)
  501. {
  502. if (this.Visibility == Visibility.Visible)
  503. {
  504. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  505. if (!isTQLPenDown)
  506. {
  507. return;
  508. }
  509. double PropW = blackboard_canvas.ActualWidth / TQLA4_HEIGHT;
  510. double PropH = blackboard_canvas.ActualHeight / TQLA4_WIDTH;
  511. double tempX = cy * PropW;
  512. double tempY = (TQLA4_WIDTH - cx) * PropH;
  513. Dispatcher.Invoke(new Action(() =>
  514. {
  515. //0~1023,亦即有 1024 阶供应用软件后续应用之用,如笔锋笔触,暂无用
  516. float Pressure = (float)force / 1023f;
  517. //添加笔画
  518. if (isFirst)
  519. {
  520. stylusPoint.X = tempX;
  521. stylusPoint.Y = tempY;
  522. stylusPoints.Add(stylusPoint);
  523. if (stylusPoints.Count > 1)
  524. {
  525. drawing = new DrawingAttributes
  526. {
  527. Color = Colour,
  528. Width = Wit * 4.5,
  529. Height = Wit * 4.5,
  530. FitToCurve = true,
  531. IgnorePressure = false
  532. };
  533. strokes = new Stroke(stylusPoints);
  534. strokes.DrawingAttributes = drawing;
  535. blackboard_canvas.Strokes.Add(strokes);
  536. isFirst = false;
  537. }
  538. }
  539. else
  540. {
  541. if (blackboard_canvas.Strokes.Count > 0)
  542. {
  543. stylusPoint.X = tempX;
  544. stylusPoint.Y = tempY;
  545. stylusPoints.Add(stylusPoint);
  546. strokes = new Stroke(stylusPoints);
  547. drawing = new DrawingAttributes
  548. {
  549. Color = Colour,
  550. Width = Wit * 4.5,
  551. Height = Wit * 4.5,
  552. FitToCurve = true,
  553. IgnorePressure = false
  554. };
  555. strokes.DrawingAttributes = drawing;
  556. //viewModel.InkStrokes.Add(strokes);
  557. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
  558. }
  559. }
  560. //设置鼠标位置
  561. if (tempX > 0 && tempY > 0)
  562. {
  563. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  564. SetCursorPos((int)((float)tempX * (PrimaryScreen.DpiX / 96f)), (int)((float)tempY * (PrimaryScreen.DpiY / 96f)));
  565. }
  566. }));
  567. }
  568. }
  569. #endregion
  570. private void Window_Loaded(object sender, RoutedEventArgs e)
  571. {
  572. Hide();
  573. }
  574. }
  575. }