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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. public PracticeWindow()
  27. {
  28. InitializeComponent();
  29. InitPen();
  30. }
  31. /// <summary>
  32. /// 初始化
  33. /// </summary>
  34. /// <param name="_imgPath"></param>
  35. public void Initialize(string _imgPath = null)
  36. {
  37. blackboard_canvas.Strokes.Clear();
  38. blackboard_canvas.UseCustomCursor = true;
  39. //创建 DrawingAttributes 类的一个实例
  40. drawingAttributes = new DrawingAttributes();
  41. blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
  42. Pen();
  43. }
  44. public void InitPen()
  45. {
  46. //stroke = new List<System.Drawing.Point>();
  47. //获取点阵笔实例,并绑定点阵笔事件
  48. //将授权文件内容传入,获取点阵笔对象实例
  49. APP.digitalPen = DigitalPenHID.GetInstance(certificates.MyLicense.Bytes);
  50. //绑定笔连接事件
  51. APP.digitalPen.PenConnected += OnPenConnect;
  52. //绑定笔断开事件
  53. APP.digitalPen.PenDisconnect += OnPenDisconnect;
  54. //绑定笔书写输出坐标事件
  55. APP.digitalPen.PenCoordinate += OnPenCoordinate;
  56. //绑定抬笔事件
  57. APP.digitalPen.PenUp += OnPenUp;
  58. //绑定落笔事件
  59. APP.digitalPen.PenDown += OnPenDown;
  60. APP.digitalPen.PenBatteryCapacity += OnBatteryCapacity;
  61. APP.digitalPen.PenMemoryFillLevel += OnMemoryFillLevel;
  62. //完成初始化点阵笔,开始与点阵笔通信
  63. ERROR_CODE ER = APP.digitalPen.Start();
  64. //判断是否成功
  65. //if (ER != ERROR_CODE.ERROR_OK)
  66. //{
  67. // MessageWindow.Show("初始化失败,授权过期,返回值:" + ER.ToString());
  68. //}
  69. }
  70. /// <summary>
  71. /// 画笔
  72. /// </summary>
  73. public void Pen()
  74. {
  75. blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
  76. blackboard_canvas.UseCustomCursor = true;
  77. drawingAttributes.FitToCurve = true;
  78. drawingAttributes.IgnorePressure = false;
  79. blackboard_canvas.Cursor = Cursors.Pen;
  80. }
  81. /// <summary>
  82. /// 笔连接
  83. /// </summary>
  84. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  85. /// <param name="penSerial">点阵笔序列号</param>
  86. /// <param name="penType">点阵笔型号编号</param>
  87. private void OnPenConnect(ulong time, string penSerial, int penType)
  88. {
  89. if (CheckAccess())
  90. {
  91. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenConnect);
  92. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  93. }
  94. else
  95. {
  96. APP.PenSerial = penSerial;
  97. APP.PenStatus = true;
  98. this.penSerial = penSerial;
  99. //连接后,在获取笔数据前,可以清除笔内的历史数据
  100. //APP.digitalPen.ClearMemory(penSerial);
  101. //开始接收笔数据
  102. APP.digitalPen.GetPenData(penSerial);
  103. //UpdateDevStatus();
  104. ////Dispatcher.Invoke(new Action(() =>
  105. ////{
  106. //// txbNotConnected.Text = "已连接";
  107. //// txbNotConnecteds.Text = "已连接";
  108. ////}));
  109. }
  110. }
  111. /// <summary>
  112. /// 笔断开
  113. /// </summary>
  114. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  115. /// <param name="penSerial">点阵笔序列号</param>
  116. /// <param name="penType">点阵笔型号编号</param>
  117. private void OnPenDisconnect(ulong time, string penSerial, int penType)
  118. {
  119. if (CheckAccess())
  120. {
  121. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDisconnect);
  122. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  123. }
  124. else
  125. {
  126. APP.PenSerial = penSerial;
  127. APP.PenStatus = false;
  128. //UpdateDevStatus();
  129. ////Dispatcher.Invoke(new Action(() =>
  130. ////{
  131. //// txbNotConnected.Text = "未连接";
  132. //// txbNotConnecteds.Text = "未连接";
  133. ////}));
  134. }
  135. }
  136. /// <summary>
  137. /// 笔序列号
  138. /// </summary>
  139. private string penSerial;
  140. /// <summary>
  141. /// 笔是否在点
  142. /// </summary>
  143. private bool isPenDown;
  144. /// <summary>
  145. /// 笔书写,收到坐标
  146. /// </summary>
  147. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  148. /// <param name="penSerial">点阵笔序列号</param>
  149. /// <param name="penType">点阵笔型号编号</param>
  150. /// <param name="pageSerial">点阵地址</param>
  151. /// <param name="cx">x坐标</param>
  152. /// <param name="cy">y坐标</param>
  153. /// <param name="force">压力值</param>
  154. private void OnPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
  155. {
  156. if (this.Visibility == Visibility.Visible)
  157. {
  158. if (CheckAccess())
  159. {
  160. Action<ulong, string, int, string, int, int, byte> ac = new Action<ulong, string, int, string, int, int, byte>(OnPenCoordinate);
  161. Dispatcher.Invoke(ac, new object[] { time, pageSerial, penType, pageSerial, cx, cy, force });
  162. }
  163. else
  164. {
  165. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  166. if (!isPenDown)
  167. {
  168. return;
  169. }
  170. stroke.Add(new System.Drawing.Point(cx, cy));
  171. double PropW = blackboard_canvas.ActualWidth / A4_HEIGHT;
  172. double PropH = blackboard_canvas.ActualHeight / A4_WIDTH;
  173. //点
  174. double testX = cy * PropW;
  175. double testY = (A4_WIDTH - cx) * PropH;
  176. //pageSerial //点阵IP地址 与打印的页面关联
  177. if (true)
  178. {
  179. Dispatcher.Invoke(new Action(() =>
  180. {
  181. float Pressure = force / 100f;
  182. ////添加笔画
  183. //myblackboard.changepages(testX, testY, false, Color, PenSize, APP.pageData.currpage - 1, Pressure);
  184. if (isFirst)
  185. {
  186. stylusPoint.X = testX;
  187. stylusPoint.Y = testY;
  188. //_color.A = (byte)(Pressure * 255f);
  189. //stylusPoint.PressureFactor = Pressure;
  190. //for(int i=0;i<100;i++)
  191. //{
  192. // stylusPoint.X--;
  193. // stylusPoint.Y--;
  194. // stylusPoints.Add(stylusPoint);
  195. //}
  196. stylusPoints.Add(stylusPoint);
  197. if (stylusPoints.Count > 1)
  198. {
  199. drawing = new DrawingAttributes
  200. {
  201. Color = Colour,
  202. Width = Wit * 4.5,
  203. Height = Wit * 4.5,
  204. FitToCurve = true,
  205. IgnorePressure = false
  206. };
  207. //blackboard_canvas.Strokes = new StrokeCollection();
  208. strokes = new Stroke(stylusPoints);
  209. strokes.DrawingAttributes = drawing;
  210. //blackboard_canvas.Strokes.Add(strokes);
  211. blackboard_canvas.Strokes.Add(strokes);
  212. isFirst = false;
  213. }
  214. }
  215. else
  216. {
  217. if (blackboard_canvas.Strokes.Count > 0)
  218. {
  219. stylusPoint.X = testX;
  220. stylusPoint.Y = testY;
  221. stylusPoints.Add(stylusPoint);
  222. strokes = new Stroke(stylusPoints);
  223. drawing = new DrawingAttributes
  224. {
  225. Color = Colour,
  226. Width = Wit * 4.5,
  227. Height = Wit * 4.5,
  228. FitToCurve = true,
  229. IgnorePressure = false
  230. };
  231. strokes.DrawingAttributes = drawing;
  232. //viewModel.InkStrokes.Add(strokes);
  233. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
  234. }
  235. }
  236. //设置鼠标位置
  237. if (testX > 0 && testY > 0)
  238. {
  239. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  240. SetCursorPos((int)((float)testX * (PrimaryScreen.DpiX / 96f)), (int)((float)testY * (PrimaryScreen.DpiY / 96f)));
  241. }
  242. }));
  243. }
  244. }
  245. }
  246. }
  247. bool isFirst = true;
  248. private StylusPointCollection stylusPoints = new StylusPointCollection();
  249. private StylusPoint stylusPoint = new StylusPoint();
  250. private Stroke strokes;
  251. /// <summary>
  252. /// 抬笔
  253. /// </summary>
  254. /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
  255. /// <param name="penSerial">点阵笔序列号</param>
  256. /// <param name="penType">点阵笔型号编号</param>
  257. private void OnPenUp(ulong time, string penSerial, int penType)
  258. {
  259. if (CheckAccess())
  260. {
  261. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenUp);
  262. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  263. }
  264. else
  265. {
  266. isPenDown = false;
  267. APP.PenSerial = penSerial;
  268. stroke.Clear();
  269. }
  270. //if (APP.pageData.currpage > 0)
  271. //{
  272. // Dispatcher.Invoke(new Action(() =>
  273. // {
  274. // myblackboard.changepages(0, 0, true, Color, PenSize, APP.pageData.currpage - 1, 0);
  275. // }));
  276. //}
  277. if (strokes != null && strokes.StylusPoints.Count > 1)
  278. {
  279. isFirst = true;
  280. }
  281. stylusPoints = new StylusPointCollection();
  282. stylusPoint = new StylusPoint();
  283. strokes = null;
  284. }
  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 OnPenDown(ulong time, string penSerial, int penType)
  292. {
  293. if (CheckAccess())
  294. {
  295. Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDown);
  296. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  297. }
  298. else
  299. {
  300. isPenDown = true;
  301. APP.W_ScreenRecordingToolbarWindow.PenPractice();
  302. }
  303. }
  304. /// <summary>
  305. /// 电池电量
  306. /// </summary>
  307. /// <param name="time"></param>
  308. /// <param name="penSerial"></param>
  309. /// <param name="penType"></param>
  310. /// <param name="capacity"></param>
  311. private void OnBatteryCapacity(ulong time, string penSerial, int penType, byte capacity)
  312. {
  313. if (CheckAccess())
  314. {
  315. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnBatteryCapacity);
  316. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, capacity });
  317. }
  318. else
  319. {
  320. //System.Windows.MessageWindow.Show("电池电量:" + capacity.ToString());
  321. }
  322. }
  323. /// <summary>
  324. /// 已用存储
  325. /// </summary>
  326. /// <param name="time"></param>
  327. /// <param name="penSerial"></param>
  328. /// <param name="penType"></param>
  329. /// <param name="fillLevel"></param>
  330. private void OnMemoryFillLevel(ulong time, string penSerial, int penType, byte fillLevel)
  331. {
  332. if (CheckAccess())
  333. {
  334. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnMemoryFillLevel);
  335. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, fillLevel });
  336. }
  337. else
  338. {
  339. //System.Windows.MessageWindow.Show("存储:" + fillLevel.ToString());
  340. }
  341. }
  342. /// <summary>
  343. /// 退出批注
  344. /// </summary>
  345. /// <param name="sender"></param>
  346. /// <param name="e"></param>
  347. private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  348. {
  349. ReturnPractice();
  350. SwitchPages();
  351. }
  352. /// <summary>
  353. /// 退出批注并清空画板
  354. /// </summary>
  355. public void ReturnPractice()
  356. {
  357. blackboard_canvas.Strokes.Clear();
  358. new Thread(new ThreadStart(new Action(() =>
  359. {
  360. Thread.Sleep(500);
  361. Dispatcher.Invoke(() =>
  362. {
  363. Owner = null;
  364. APP.W_PracticeWindow.Hide();
  365. });
  366. }))).Start();
  367. }
  368. /// <summary>
  369. /// 鼠标中建点击
  370. /// </summary>
  371. void SwitchPages()
  372. {
  373. try
  374. {
  375. new Thread(() =>
  376. {
  377. Thread.Sleep(500);
  378. MouseEventCommon.MouseMiddleClickEvent(0);
  379. }).Start();
  380. }
  381. catch (Exception ex)
  382. {
  383. MessageBox.Show(ex.Message);
  384. }
  385. }
  386. }
  387. }