星火微课系统客户端
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.

README.md 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # 拓思德点阵笔
  2. 引用
  3. ```csharp
  4. using TStudyDigitalPen.HID;
  5. ```
  6. 代码
  7. ```csharp
  8. #region 拓思德点阵笔
  9. #region 值初始化
  10. // 不同尺寸点阵纸点阵宽高尺寸计算方法为:纸张物理尺寸(毫米)/0.3 *8,详见 开发必读.pdf 文档
  11. /// <summary>
  12. /// A4点阵纸点阵宽度
  13. /// </summary>
  14. private const int A4_WIDTH = 5600;
  15. /// <summary>
  16. /// A4点阵纸点阵高度
  17. /// </summary>
  18. private const int A4_HEIGHT = 7920;
  19. ///// <summary>
  20. ///// 画板
  21. ///// </summary>
  22. //private Graphics graphics;
  23. /// <summary>
  24. /// 笔画坐标数组
  25. /// </summary>
  26. //private List<System.Drawing.Point> stroke;
  27. /// <summary>
  28. /// 笔序列号
  29. /// </summary>
  30. private string penSerial;
  31. /// <summary>
  32. /// 笔是否在点
  33. /// </summary>
  34. private bool isPenDown;
  35. //当前点阵地址
  36. private string currentPageSerial = string.Empty;
  37. //不同点阵地址对应的笔迹绘制图片,用于实现在不同点阵地址书写切换时,显示书写内容自动切换
  38. //本例图片放在内存中存储,对于大量或者需要在多个点阵地址对应图片进行切换演示,建议将图片存储到文件,以免内存溢出
  39. private Dictionary<string, System.Drawing.Image> pagesDic = new Dictionary<string, System.Drawing.Image>();
  40. #endregion 值初始化
  41. public void InitTSDPen()
  42. {
  43. //stroke = new List<System.Drawing.Point>();
  44. //获取点阵笔实例,并绑定点阵笔事件
  45. //将授权文件内容传入,获取点阵笔对象实例
  46. APP.digitalPen = DigitalPenHID.GetInstance(certificates.MyLicense.Bytes);
  47. //绑定笔连接事件
  48. APP.digitalPen.PenConnected += OnTSDPenConnect;
  49. //绑定笔断开事件
  50. APP.digitalPen.PenDisconnect += OnTSDPenDisconnect;
  51. //绑定笔书写输出坐标事件
  52. APP.digitalPen.PenCoordinate += OnTSDPenCoordinate;
  53. //绑定抬笔事件
  54. APP.digitalPen.PenUp += OnTSDPenUp;
  55. //绑定落笔事件
  56. APP.digitalPen.PenDown += OnTSDPenDown;
  57. APP.digitalPen.PenBatteryCapacity += OnTSDBatteryCapacity;
  58. APP.digitalPen.PenMemoryFillLevel += OnTSDMemoryFillLevel;
  59. //完成初始化点阵笔,开始与点阵笔通信
  60. ERROR_CODE ER = APP.digitalPen.Start();
  61. //启动接收笔数据,完成初始化工作
  62. ERROR_CODE rc = APP.digitalPen.Start();
  63. //判断是否成功
  64. if (ER != ERROR_CODE.ERROR_OK)
  65. {
  66. MessageWindow.Show("初始化失败,授权过期,返回值:" + ER.ToString());
  67. }
  68. }
  69. /// <summary>
  70. /// 落笔
  71. /// </summary>
  72. /// <param name="time">
  73. /// 时间戳,1970年1月1日到现在的总毫秒数
  74. /// </param>
  75. /// <param name="penSerial">
  76. /// 点阵笔序列号
  77. /// </param>
  78. /// <param name="penType">
  79. /// 点阵笔型号编号
  80. /// </param>
  81. private void OnTSDPenDown(ulong time, string penSerial, int penType)
  82. {
  83. if (CheckAccess())
  84. {
  85. Action<ulong, string, int> action = new Action<ulong, string, int>(OnTSDPenDown);
  86. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  87. }
  88. else
  89. {
  90. isPenDown = true;
  91. }
  92. }
  93. /// <summary>
  94. /// 抬笔
  95. /// </summary>
  96. /// <param name="time">
  97. /// 时间戳,1970年1月1日到现在的总毫秒数
  98. /// </param>
  99. /// <param name="penSerial">
  100. /// 点阵笔序列号
  101. /// </param>
  102. /// <param name="penType">
  103. /// 点阵笔型号编号
  104. /// </param>
  105. private void OnTSDPenUp(ulong time, string penSerial, int penType)
  106. {
  107. if (CheckAccess())
  108. {
  109. Action<ulong, string, int> action = new Action<ulong, string, int>(OnTSDPenUp);
  110. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  111. }
  112. else
  113. {
  114. isPenDown = false;
  115. APP.PenSerial = penSerial;
  116. }
  117. if (APP.PageContextData.currpage > 0)
  118. {
  119. Dispatcher.Invoke(new Action(() =>
  120. {
  121. myblackboard.changepages(0, 0, true, Color, PenSize, APP.PageContextData.currpage - 1, 0);
  122. }));
  123. }
  124. }
  125. /// <summary>
  126. /// 笔断开
  127. /// </summary>
  128. /// <param name="time">
  129. /// 时间戳,1970年1月1日到现在的总毫秒数
  130. /// </param>
  131. /// <param name="penSerial">
  132. /// 点阵笔序列号
  133. /// </param>
  134. /// <param name="penType">
  135. /// 点阵笔型号编号
  136. /// </param>
  137. private void OnTSDPenDisconnect(ulong time, string penSerial, int penType)
  138. {
  139. if (CheckAccess())
  140. {
  141. Action<ulong, string, int> action = new Action<ulong, string, int>(OnTSDPenDisconnect);
  142. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  143. }
  144. else
  145. {
  146. APP.PenSerial = penSerial;
  147. APP.PenStatus = false;
  148. UpdateDevStatus();
  149. }
  150. }
  151. /// <summary>
  152. /// 笔连接
  153. /// </summary>
  154. /// <param name="time">
  155. /// 时间戳,1970年1月1日到现在的总毫秒数
  156. /// </param>
  157. /// <param name="penSerial">
  158. /// 点阵笔序列号
  159. /// </param>
  160. /// <param name="penType">
  161. /// 点阵笔型号编号
  162. /// </param>
  163. private void OnTSDPenConnect(ulong time, string penSerial, int penType)
  164. {
  165. if (CheckAccess())
  166. {
  167. Action<ulong, string, int> action = new Action<ulong, string, int>(OnTSDPenConnect);
  168. Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
  169. }
  170. else
  171. {
  172. APP.PenSerial = penSerial;
  173. APP.PenStatus = true;
  174. this.penSerial = penSerial;
  175. //连接后,在获取笔数据前,可以清除笔内的历史数据
  176. //APP.digitalPen.ClearMemory(penSerial);
  177. //开始接收笔数据
  178. APP.digitalPen.GetPenData(penSerial);
  179. UpdateDevStatus();
  180. }
  181. }
  182. /// <summary>
  183. /// 电池电量
  184. /// </summary>
  185. /// <param name="time">
  186. /// </param>
  187. /// <param name="penSerial">
  188. /// </param>
  189. /// <param name="penType">
  190. /// </param>
  191. /// <param name="capacity">
  192. /// </param>
  193. private void OnTSDBatteryCapacity(ulong time, string penSerial, int penType, byte capacity)
  194. {
  195. if (CheckAccess())
  196. {
  197. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnTSDBatteryCapacity);
  198. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, capacity });
  199. }
  200. else
  201. {
  202. //System.Windows.MessageWindow.Show("电池电量:" + capacity.ToString());
  203. }
  204. }
  205. /// <summary>
  206. /// 已用存储
  207. /// </summary>
  208. /// <param name="time">
  209. /// </param>
  210. /// <param name="penSerial">
  211. /// </param>
  212. /// <param name="penType">
  213. /// </param>
  214. /// <param name="fillLevel">
  215. /// </param>
  216. private void OnTSDMemoryFillLevel(ulong time, string penSerial, int penType, byte fillLevel)
  217. {
  218. if (CheckAccess())
  219. {
  220. Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnTSDMemoryFillLevel);
  221. Dispatcher.Invoke(action, new object[] { time, penSerial, penType, fillLevel });
  222. }
  223. else
  224. {
  225. }
  226. }
  227. /// <summary>
  228. /// 笔书写,收到坐标
  229. /// </summary>
  230. /// <param name="time">
  231. /// 时间戳,1970年1月1日到现在的总毫秒数
  232. /// </param>
  233. /// <param name="penSerial">
  234. /// 点阵笔序列号
  235. /// </param>
  236. /// <param name="penType">
  237. /// 点阵笔型号编号
  238. /// </param>
  239. /// <param name="pageSerial">
  240. /// 点阵地址
  241. /// </param>
  242. /// <param name="cx">
  243. /// x坐标
  244. /// </param>
  245. /// <param name="cy">
  246. /// y坐标
  247. /// </param>
  248. /// <param name="force">
  249. /// 压力值
  250. /// </param>
  251. private void OnTSDPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
  252. {
  253. if (CheckAccess())
  254. {
  255. Action<ulong, string, int, string, int, int, byte> ac = new Action<ulong, string, int, string, int, int, byte>(OnTSDPenCoordinate);
  256. Dispatcher.Invoke(ac, new object[] { time, pageSerial, penType, pageSerial, cx, cy, force });
  257. }
  258. else
  259. {
  260. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  261. if (!isPenDown)
  262. {
  263. return;
  264. }
  265. //stroke.Add(new System.Drawing.Point(cx, cy));
  266. double PropW = blackboard_canvas.ActualWidth / A4_WIDTH;
  267. double PropH = blackboard_canvas.ActualHeight / A4_HEIGHT;
  268. //点
  269. double tempX = cx * PropW;
  270. double tempY = cy * PropH;
  271. //pageSerial //点阵IP地址 与打印的页面关联
  272. if (APP.PageContextData.currpage > 0)
  273. {
  274. Dispatcher.Invoke(new Action(() =>
  275. {
  276. float Pressure = force / 100f;
  277. //myblackboard.changepages(testX, testY,false);
  278. myblackboard.changepages(tempX, tempY, false, Color, PenSize, APP.PageContextData.currpage - 1, Pressure);
  279. #region 设置滚动条位置
  280. //点在显示页面上方
  281. if (tempY < ScroMain.VerticalOffset)
  282. {
  283. //滚动条当前位置
  284. double RollCurrentLocation = ScroMain.VerticalOffset;
  285. //向上滚动至以点为中心需要滚动的距离
  286. double UpRoll = (RollCurrentLocation - tempY) + (ScroMain.ActualHeight / 2);
  287. //如果小于0则等于0
  288. double RollLocation = RollCurrentLocation - UpRoll;
  289. if (RollLocation < 0)
  290. {
  291. RollLocation = 0;
  292. }
  293. ////滚动条实际偏移量
  294. //double RollOffset = RollCurrentLocation - RollLocation;
  295. ScroMain.ScrollToVerticalOffset(RollLocation);
  296. }
  297. //点在显示页面下方
  298. if (tempY > ScroMain.VerticalOffset + ScroMain.ActualHeight)
  299. {
  300. //滚动条当前位置
  301. double RollCurrentLocation = ScroMain.VerticalOffset;
  302. //向下滚动至以点为中心需要滚动的距离
  303. double DownRoll = (tempY - RollCurrentLocation) - (ScroMain.ActualHeight / 2);
  304. //如果小于0则等于0
  305. double RollLocation = RollCurrentLocation + DownRoll;
  306. //滚动条最大滚动值
  307. double ScrollbarMaxNum = GridM.ActualHeight - ScroMain.ActualHeight;
  308. if (RollLocation > ScrollbarMaxNum)
  309. {
  310. RollLocation = ScrollbarMaxNum;
  311. }
  312. ////滚动条实际偏移量
  313. //double RollOffset = RollLocation-RollCurrentLocation;
  314. ScroMain.ScrollToVerticalOffset(RollLocation);
  315. }
  316. #endregion 设置滚动条位置
  317. if (tempX > 0 && tempY > 0)
  318. {
  319. //System.Windows.Point getP = blackboard_canvas.PointToScreen(new System.Windows.Point(testX, testY));
  320. System.Windows.Point getP = ScroMain.PointToScreen(new System.Windows.Point(tempX, tempY - ScroMain.VerticalOffset));
  321. SetCursorPos((int)getP.X, (int)getP.Y);
  322. }
  323. }));
  324. }
  325. }
  326. }
  327. /// <summary>
  328. /// 清空笔内存储
  329. /// </summary>
  330. public void ClearPenStorage()
  331. {
  332. if (!string.IsNullOrEmpty(penSerial))
  333. {
  334. APP.digitalPen.ClearMemory(penSerial);
  335. }
  336. }
  337. /// <summary>
  338. /// 获取剩余电量
  339. /// </summary>
  340. public void GetPenElectricityQuantity()
  341. {
  342. if (!string.IsNullOrEmpty(penSerial))
  343. {
  344. APP.digitalPen.GetBatteryCapacity(penSerial);
  345. }
  346. }
  347. /// <summary>
  348. /// 获取存储空间
  349. /// </summary>
  350. public void GetUsedStorage()
  351. {
  352. if (!string.IsNullOrEmpty(penSerial))
  353. {
  354. APP.digitalPen.GetMemoryFillLevel(penSerial);
  355. }
  356. }
  357. /// <summary>
  358. /// 开启悬浮
  359. /// </summary>
  360. public void 开启悬浮()
  361. {
  362. if (!string.IsNullOrEmpty(penSerial))
  363. {
  364. APP.digitalPen.SetPenHoverMode(true, penSerial);
  365. }
  366. }
  367. /// <summary>
  368. /// 关闭悬浮
  369. /// </summary>
  370. public void 关闭悬浮()
  371. {
  372. if (!string.IsNullOrEmpty(penSerial))
  373. {
  374. APP.digitalPen.SetPenHoverMode(false, penSerial);
  375. }
  376. }
  377. /// <summary>
  378. /// 引用user32.dll动态链接库(windows api), 使用库中定义 API:SetCursorPos 设置光标位置
  379. /// </summary>
  380. [System.Runtime.InteropServices.DllImport("user32.dll")]
  381. private static extern int SetCursorPos(int x, int y);
  382. #endregion 拓思德点阵笔
  383. ```