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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. //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. //UpdateDevStatus();
  162. ////Dispatcher.Invoke(new Action(() =>
  163. ////{
  164. //// txbNotConnected.Text = "未连接";
  165. //// txbNotConnecteds.Text = "未连接";
  166. ////}));
  167. }
  168. }
  169. /// <summary>
  170. /// 笔序列号
  171. /// </summary>
  172. private string penSerial;
  173. /// <summary>
  174. /// 笔是否在点
  175. /// </summary>
  176. private bool isPenDown;
  177. /// <summary>
  178. /// 笔书写,收到坐标
  179. /// </summary>
  180. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  181. /// <param name="penSerial">点阵笔序列号</param>
  182. /// <param name="penType">点阵笔型号编号</param>
  183. /// <param name="pageSerial">点阵地址</param>
  184. /// <param name="cx">x坐标</param>
  185. /// <param name="cy">y坐标</param>
  186. /// <param name="force">压力值</param>
  187. private void OnPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
  188. {
  189. if (this.Visibility == Visibility.Visible)
  190. {
  191. if (CheckAccess())
  192. {
  193. Action<ulong, string, int, string, int, int, byte> ac = new Action<ulong, string, int, string, int, int, byte>(OnPenCoordinate);
  194. Dispatcher.Invoke(ac, new object[] { time, pageSerial, penType, pageSerial, cx, cy, force });
  195. }
  196. else
  197. {
  198. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  199. if (!isPenDown)
  200. {
  201. return;
  202. }
  203. //stroke.Add(new System.Drawing.Point(cx, cy));
  204. double PropW = blackboard_canvas.ActualWidth / A4_HEIGHT;
  205. double PropH = blackboard_canvas.ActualHeight / A4_WIDTH;
  206. //点
  207. double testX = cy * PropW;
  208. double testY = (A4_WIDTH - cx) * PropH;
  209. //pageSerial //点阵IP地址 与打印的页面关联
  210. if (true)
  211. {
  212. Dispatcher.Invoke(new Action(() =>
  213. {
  214. float Pressure = force / 100f;
  215. ////添加笔画
  216. //myblackboard.changepages(testX, testY, false, Color, PenSize, APP.pageData.currpage - 1, Pressure);
  217. if (isFirst)
  218. {
  219. stylusPoint.X = testX;
  220. stylusPoint.Y = testY;
  221. //_color.A = (byte)(Pressure * 255f);
  222. //stylusPoint.PressureFactor = Pressure;
  223. //for(int i=0;i<100;i++)
  224. //{
  225. // stylusPoint.X--;
  226. // stylusPoint.Y--;
  227. // stylusPoints.Add(stylusPoint);
  228. //}
  229. stylusPoints.Add(stylusPoint);
  230. if (stylusPoints.Count > 1)
  231. {
  232. drawing = new DrawingAttributes
  233. {
  234. Color = Colour,
  235. Width = Wit * 4.5,
  236. Height = Wit * 4.5,
  237. FitToCurve = true,
  238. IgnorePressure = false
  239. };
  240. //blackboard_canvas.Strokes = new StrokeCollection();
  241. strokes = new Stroke(stylusPoints);
  242. strokes.DrawingAttributes = drawing;
  243. //blackboard_canvas.Strokes.Add(strokes);
  244. blackboard_canvas.Strokes.Add(strokes);
  245. isFirst = false;
  246. }
  247. }
  248. else
  249. {
  250. if (blackboard_canvas.Strokes.Count > 0)
  251. {
  252. stylusPoint.X = testX;
  253. stylusPoint.Y = testY;
  254. stylusPoints.Add(stylusPoint);
  255. strokes = new Stroke(stylusPoints);
  256. drawing = new DrawingAttributes
  257. {
  258. Color = Colour,
  259. Width = Wit * 4.5,
  260. Height = Wit * 4.5,
  261. FitToCurve = true,
  262. IgnorePressure = false
  263. };
  264. strokes.DrawingAttributes = drawing;
  265. //viewModel.InkStrokes.Add(strokes);
  266. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
  267. }
  268. }
  269. //设置鼠标位置
  270. if (testX > 0 && testY > 0)
  271. {
  272. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  273. SetCursorPos((int)((float)testX * (PrimaryScreen.DpiX / 96f)), (int)((float)testY * (PrimaryScreen.DpiY / 96f)));
  274. }
  275. }));
  276. }
  277. }
  278. }
  279. }
  280. bool isFirst = true;
  281. private StylusPointCollection stylusPoints = new StylusPointCollection();
  282. private StylusPoint stylusPoint = new StylusPoint();
  283. private Stroke strokes;
  284. /// <summary>
  285. /// 抬笔
  286. /// </summary>
  287. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  288. /// <param name="penSerial">点阵笔序列号</param>
  289. /// <param name="penType">点阵笔型号编号</param>
  290. private void OnPenUp(ulong time, string penSerial, int penType)
  291. {
  292. if (CheckAccess())
  293. {
  294. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenUp);
  295. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  296. }
  297. else
  298. {
  299. isPenDown = false;
  300. APP.PenSerial = penSerial;
  301. //stroke.Clear();
  302. }
  303. //if (APP.pageData.currpage > 0)
  304. //{
  305. // Dispatcher.Invoke(new Action(() =>
  306. // {
  307. // myblackboard.changepages(0, 0, true, Color, PenSize, APP.pageData.currpage - 1, 0);
  308. // }));
  309. //}
  310. if (strokes != null && strokes.StylusPoints.Count > 1)
  311. {
  312. isFirst = true;
  313. }
  314. stylusPoints = new StylusPointCollection();
  315. stylusPoint = new StylusPoint();
  316. strokes = null;
  317. }
  318. /// <summary>
  319. /// 落笔
  320. /// </summary>
  321. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  322. /// <param name="penSerial">点阵笔序列号</param>
  323. /// <param name="penType">点阵笔型号编号</param>
  324. private void OnPenDown(ulong time, string penSerial, int penType)
  325. {
  326. if (CheckAccess())
  327. {
  328. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDown);
  329. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  330. }
  331. else
  332. {
  333. isPenDown = true;
  334. if (APP.W_PracticeWindow != null && APP.W_PracticeWindow.Visibility == Visibility.Hidden)
  335. {
  336. PenPractice();
  337. }
  338. }
  339. }
  340. void PenPractice()
  341. {
  342. Dispatcher.Invoke(() =>
  343. {
  344. if (APP.W_PracticeWindow == null)
  345. {
  346. APP.W_PracticeWindow = new PracticeWindow
  347. {
  348. Width = pwidth,
  349. Height = pHeight,
  350. Left = 0,
  351. Top = 0
  352. };
  353. }
  354. APP.W_PracticeWindow.Topmost = true;
  355. APP.W_PracticeWindow.Initialize();
  356. APP.W_PracticeWindow.Show();
  357. });
  358. }
  359. /// <summary>
  360. /// 电池电量
  361. /// </summary>
  362. /// <param name="time"></param>
  363. /// <param name="penSerial"></param>
  364. /// <param name="penType"></param>
  365. /// <param name="capacity"></param>
  366. private void OnBatteryCapacity(ulong time, string penSerial, int penType, byte capacity)
  367. {
  368. if (CheckAccess())
  369. {
  370. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnBatteryCapacity);
  371. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, capacity });
  372. }
  373. else
  374. {
  375. //System.Windows.MessageWindow.Show("电池电量:" + capacity.ToString());
  376. }
  377. }
  378. /// <summary>
  379. /// 已用存储
  380. /// </summary>
  381. /// <param name="time"></param>
  382. /// <param name="penSerial"></param>
  383. /// <param name="penType"></param>
  384. /// <param name="fillLevel"></param>
  385. private void OnMemoryFillLevel(ulong time, string penSerial, int penType, byte fillLevel)
  386. {
  387. if (CheckAccess())
  388. {
  389. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnMemoryFillLevel);
  390. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, fillLevel });
  391. }
  392. else
  393. {
  394. //System.Windows.MessageWindow.Show("存储:" + fillLevel.ToString());
  395. }
  396. }
  397. /// <summary>
  398. /// 退出批注
  399. /// </summary>
  400. /// <param name="sender"></param>
  401. /// <param name="e"></param>
  402. private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  403. {
  404. ReturnPractice();
  405. SwitchPages();
  406. }
  407. /// <summary>
  408. /// 退出批注并清空画板
  409. /// </summary>
  410. public void ReturnPractice()
  411. {
  412. blackboard_canvas.Strokes.Clear();
  413. new Thread(new ThreadStart(new Action(() =>
  414. {
  415. Thread.Sleep(500);
  416. Dispatcher.Invoke(() =>
  417. {
  418. Owner = null;
  419. APP.W_PracticeWindow.Hide();
  420. });
  421. }))).Start();
  422. }
  423. /// <summary>
  424. /// 鼠标中建点击
  425. /// </summary>
  426. void SwitchPages()
  427. {
  428. try
  429. {
  430. new Thread(() =>
  431. {
  432. Thread.Sleep(500);
  433. MouseEventCommon.MouseMiddleClickEvent(0);
  434. }).Start();
  435. }
  436. catch (Exception ex)
  437. {
  438. MessageBox.Show(ex.Message);
  439. }
  440. }
  441. #region 腾千里手写笔
  442. /// <summary>
  443. /// 是否在书写
  444. /// </summary>
  445. bool isTQLPenDown = false;
  446. /// <summary>
  447. /// TQL点阵笔宽高
  448. /// </summary>
  449. static float TQLA4_WIDTH = (float)1075 / (float)150 * (float)25.4 / (float)1.524; //OID4
  450. static float TQLA4_HEIGHT = (float)1512 / (float)150 * (float)25.4 / (float)1.524;
  451. /// <summary>
  452. /// 初始化
  453. /// </summary>
  454. public void InitTQLPPen()
  455. {
  456. if (APP.TQLPenevents == null)
  457. {
  458. APP.TQLPenevents = new PenEvents();
  459. }
  460. }
  461. /// <summary>
  462. /// 笔落下
  463. /// </summary>
  464. public void TQLDown()
  465. {
  466. isTQLPenDown = true;
  467. if(APP.W_PracticeWindow!=null&&APP.W_PracticeWindow.Visibility==Visibility.Hidden)
  468. {
  469. PenPractice();
  470. }
  471. if (strokes != null && strokes.StylusPoints.Count > 1)
  472. {
  473. isFirst = true;
  474. }
  475. stylusPoints = new StylusPointCollection();
  476. stylusPoint = new StylusPoint();
  477. strokes = null;
  478. }
  479. /// <summary>
  480. /// 笔抬起
  481. /// </summary>
  482. public void TQLUp()
  483. {
  484. isTQLPenDown = false;
  485. if (strokes != null && strokes.StylusPoints.Count > 1)
  486. {
  487. isFirst = true;
  488. }
  489. stylusPoints = new StylusPointCollection();
  490. stylusPoint = new StylusPoint();
  491. strokes = null;
  492. }
  493. /// <summary>
  494. /// 笔书写
  495. /// </summary>
  496. /// <param name="cx"></param>
  497. /// <param name="cy"></param>
  498. /// <param name="force"></param>
  499. public void TQLPenWrite(double cx, double cy, int force)
  500. {
  501. if (this.Visibility == Visibility.Visible)
  502. {
  503. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  504. if (!isTQLPenDown)
  505. {
  506. return;
  507. }
  508. double PropW = blackboard_canvas.ActualWidth / TQLA4_HEIGHT;
  509. double PropH = blackboard_canvas.ActualHeight / TQLA4_WIDTH;
  510. double tempX = cy * PropW;
  511. double tempY = (TQLA4_WIDTH - cx) * PropH;
  512. Dispatcher.Invoke(new Action(() =>
  513. {
  514. //0~1023,亦即有 1024 阶供应用软件后续应用之用,如笔锋笔触,暂无用
  515. float Pressure = (float)force / 1023f;
  516. //添加笔画
  517. if (isFirst)
  518. {
  519. stylusPoint.X = tempX;
  520. stylusPoint.Y = tempY;
  521. stylusPoints.Add(stylusPoint);
  522. if (stylusPoints.Count > 1)
  523. {
  524. drawing = new DrawingAttributes
  525. {
  526. Color = Colour,
  527. Width = Wit * 4.5,
  528. Height = Wit * 4.5,
  529. FitToCurve = true,
  530. IgnorePressure = false
  531. };
  532. strokes = new Stroke(stylusPoints);
  533. strokes.DrawingAttributes = drawing;
  534. blackboard_canvas.Strokes.Add(strokes);
  535. isFirst = false;
  536. }
  537. }
  538. else
  539. {
  540. if (blackboard_canvas.Strokes.Count > 0)
  541. {
  542. stylusPoint.X = tempX;
  543. stylusPoint.Y = tempY;
  544. stylusPoints.Add(stylusPoint);
  545. strokes = new Stroke(stylusPoints);
  546. drawing = new DrawingAttributes
  547. {
  548. Color = Colour,
  549. Width = Wit * 4.5,
  550. Height = Wit * 4.5,
  551. FitToCurve = true,
  552. IgnorePressure = false
  553. };
  554. strokes.DrawingAttributes = drawing;
  555. //viewModel.InkStrokes.Add(strokes);
  556. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
  557. }
  558. }
  559. //设置鼠标位置
  560. if (tempX > 0 && tempY > 0)
  561. {
  562. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  563. SetCursorPos((int)((float)tempX * (PrimaryScreen.DpiX / 96f)), (int)((float)tempY * (PrimaryScreen.DpiY / 96f)));
  564. }
  565. }));
  566. }
  567. }
  568. #endregion
  569. private void Window_Loaded(object sender, RoutedEventArgs e)
  570. {
  571. Hide();
  572. }
  573. }
  574. }