星火微课系统客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PracticeWindow.xaml.cs 15KB

4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
4年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using Common.system;
  2. using System;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using XHWK.Model;
  10. namespace XHWK.WKTool
  11. {
  12. /// <summary>
  13. /// 录屏画板
  14. /// </summary>
  15. public partial class PracticeWindow
  16. {
  17. //声明一个 DrawingAttributes 类型的变量
  18. private DrawingAttributes _drawingAttributes;
  19. private DrawingAttributes _drawing;
  20. private ViewModel _viewModel;
  21. /// <summary>
  22. /// 0 画笔 1 矩形 2 圆形
  23. /// </summary>
  24. #pragma warning disable CS0414
  25. private int _flg;
  26. #pragma warning restore CS0414
  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. InitTqlpPen();
  43. }
  44. public void Initialize(string imgPath = null)
  45. {
  46. _flg = 0;
  47. blackboard_canvas.Strokes.Clear();
  48. blackboard_canvas.UseCustomCursor = true;
  49. //blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  50. //加背景 废弃更利于讲解ppt
  51. //if (File.Exists(_imgPath))
  52. //{
  53. // imgCanvas.Source = new BitmapImage(new Uri(_imgPath));
  54. //}
  55. //创建 DrawingAttributes 类的一个实例
  56. _drawingAttributes = new DrawingAttributes();
  57. //drawingAttributes.StylusTip = StylusTip.Rectangle;
  58. //drawingAttributes.IsHighlighter = false;
  59. //drawingAttributes.IgnorePressure = true;
  60. ////将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用
  61. ////InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型
  62. blackboard_canvas.DefaultDrawingAttributes = _drawingAttributes;
  63. Pen();
  64. //blackboard_canvas.Cursor = Cursors.Pen;
  65. //Cursor cus = new Cursor(@"G:\06.cur");
  66. //blackboard_canvas.Cursor = cus;
  67. _viewModel = new ViewModel
  68. {
  69. InkStrokes = new StrokeCollection(),
  70. };
  71. DataContext = _viewModel;
  72. //APP.W_ScreenRecordingToolbarWindow.Owner = this;
  73. //APP.W_ScreenRecordingToolbarWindow.Topmost = true;
  74. }
  75. /// <summary>
  76. /// 画笔颜色事件 白色
  77. /// </summary>
  78. public void White()
  79. {
  80. //flg = 0;
  81. _drawingAttributes.Color = Colors.White;
  82. _colour = Colors.White;
  83. }
  84. /// <summary>
  85. /// 画笔颜色事件 红色
  86. /// </summary>
  87. public void Red()
  88. {
  89. _drawingAttributes.Color = Colors.Red;
  90. _colour = Colors.Red;
  91. }
  92. /// <summary>
  93. /// 画笔颜色事件 黑色
  94. /// </summary>
  95. public void Gray()
  96. {
  97. //flg = 0;
  98. _drawingAttributes.Color = Colors.Black;
  99. _colour = Colors.Black;
  100. }
  101. /// <summary>
  102. /// 画笔颜色事件 青色
  103. /// </summary>
  104. public void CyanBlue()
  105. {
  106. //flg = 0;
  107. _drawingAttributes.Color = Colors.LimeGreen;
  108. _colour = Colors.LimeGreen;
  109. }
  110. /// <summary>
  111. /// 画笔颜色事件 黄色
  112. /// </summary>
  113. public void Yellow()
  114. {
  115. //flg = 0;
  116. _drawingAttributes.Color = Colors.Gold;
  117. _colour = Colors.Gold;
  118. }
  119. /// <summary>
  120. /// 画笔颜色事件 蓝色
  121. /// </summary>
  122. public void Blue()
  123. {
  124. //flg = 0;
  125. _drawingAttributes.Color = Colors.DeepSkyBlue;
  126. _colour = Colors.DeepSkyBlue;
  127. }
  128. /// <summary>
  129. /// 画笔粗细事件 细
  130. /// </summary>
  131. public void Fine()
  132. {
  133. //flg = 0;
  134. _drawingAttributes.Width = 1;
  135. _drawingAttributes.Height = 1;
  136. _wit = 1;
  137. _hei = 1;
  138. }
  139. /// <summary>
  140. /// 画笔粗细事件 中
  141. /// </summary>
  142. public void In()
  143. {
  144. //flg = 0;
  145. _drawingAttributes.Width = 3;
  146. _drawingAttributes.Height = 3;
  147. _wit = 3;
  148. _hei = 3;
  149. }
  150. /// <summary>
  151. /// 画笔粗细事件 粗
  152. /// </summary>
  153. public void Crude()
  154. {
  155. //flg = 0;
  156. _drawingAttributes.Width = 5;
  157. _drawingAttributes.Height = 5;
  158. _wit = 5;
  159. _hei = 5;
  160. }
  161. /// <summary>
  162. /// 橡皮
  163. /// </summary>
  164. public void Eraser()
  165. {
  166. _flg = 0;
  167. //this.type = ZPenType.Erase;
  168. blackboard_canvas.UseCustomCursor = false;
  169. blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  170. blackboard_canvas.EraserShape = new EllipseStylusShape(64, 64, 0);
  171. }
  172. /// <summary>
  173. /// 画笔
  174. /// </summary>
  175. public void Pen()
  176. {
  177. _flg = 0;
  178. blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
  179. blackboard_canvas.UseCustomCursor = true;
  180. _drawingAttributes.FitToCurve = true;
  181. _drawingAttributes.IgnorePressure = false;
  182. blackboard_canvas.Cursor = Cursors.Pen;
  183. }
  184. /// <summary>
  185. /// 圆
  186. /// </summary>
  187. public void Round()
  188. {
  189. _flg = 2;
  190. _drawingAttributes = new DrawingAttributes
  191. {
  192. Color = _colour,
  193. Width = _wit,
  194. Height = _hei,
  195. StylusTip = StylusTip.Rectangle,
  196. //FitToCurve = true,
  197. IsHighlighter = false,
  198. IgnorePressure = true,
  199. };
  200. blackboard_canvas.DefaultDrawingAttributes = _drawingAttributes;
  201. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  202. blackboard_canvas.Cursor = Cursors.Cross;
  203. }
  204. /// <summary>
  205. /// 矩形
  206. /// </summary>
  207. public void Rectangle()
  208. {
  209. _flg = 1;
  210. _drawingAttributes = new DrawingAttributes
  211. {
  212. Color = _colour,
  213. Width = _wit,
  214. Height = _hei,
  215. StylusTip = StylusTip.Rectangle,
  216. //FitToCurve = true,
  217. IsHighlighter = false,
  218. IgnorePressure = true,
  219. };
  220. blackboard_canvas.DefaultDrawingAttributes = _drawingAttributes;
  221. blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
  222. blackboard_canvas.Cursor = Cursors.Cross;
  223. }
  224. /// <summary>
  225. /// 鼠标中键点击
  226. /// </summary>
  227. private void SwitchPages()
  228. {
  229. try
  230. {
  231. new Thread(() =>
  232. {
  233. Thread.Sleep(500);
  234. MouseEventCommon.MouseMiddleClickEvent(0);
  235. }).Start();
  236. }
  237. catch (Exception ex)
  238. {
  239. MessageBox.Show(ex.Message);
  240. }
  241. }
  242. private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  243. {
  244. if (App.W_ScreenRecordingToolbarWindow.flg == 10)
  245. {
  246. App.W_ScreenRecordingToolbarWindow.flg = 0;
  247. }
  248. else
  249. {
  250. App.W_ScreenRecordingToolbarWindow.flg = 1;
  251. }
  252. ReturnPractice();
  253. //模拟切到ppt
  254. SwitchPages();
  255. //Hide();
  256. }
  257. /// <summary>
  258. /// 退出批注并清空画板
  259. /// </summary>
  260. public void ReturnPractice()
  261. {
  262. blackboard_canvas.Strokes.Clear();
  263. new Thread(() =>
  264. {
  265. Dispatcher.Invoke(() => { App.W_ScreenRecordingToolbarWindow.ModifyState(); });
  266. _flg = 0;
  267. Thread.Sleep(500);
  268. Dispatcher.Invoke(() =>
  269. {
  270. Owner = null;
  271. App.W_PracticeWindow.Hide();
  272. });
  273. }).Start();
  274. }
  275. #region 点阵笔相关
  276. #region 值初始化
  277. // 不同尺寸点阵纸点阵宽高尺寸计算方法为:纸张物理尺寸(毫米)/0.3 *8,详见 开发必读.pdf 文档
  278. ///// <summary>
  279. ///// 画板
  280. ///// </summary>
  281. //private Graphics graphics;
  282. #endregion 值初始化
  283. private bool _isFirst = true;
  284. private StylusPointCollection _stylusPoints = new StylusPointCollection();
  285. private StylusPoint _stylusPoint;
  286. private Stroke _strokes;
  287. /// <summary>
  288. /// 引用user32.dll动态链接库(windows api),
  289. /// 使用库中定义 API:SetCursorPos
  290. /// 设置光标位置
  291. /// </summary>
  292. [System.Runtime.InteropServices.DllImport("user32.dll")]
  293. private static extern int SetCursorPos(int x, int y);
  294. #endregion 点阵笔相关
  295. #region 腾千里手写笔
  296. /// <summary>
  297. /// 是否在书写
  298. /// </summary>
  299. private bool _isTqlPenDown;
  300. /// <summary>
  301. /// TQL点阵笔宽高
  302. /// </summary>
  303. //static float TQLA4_WIDTH = (float)1075 / (float)150 * (float)25.4 / (float)1.524; //OID4
  304. //static float TQLA4_HEIGHT = (float)1512 / (float)150 * (float)25.4 / (float)1.524;
  305. private const float Tqla4Width = 2480 / (float)300 * (float)25.4 / (float)1.524;
  306. private const float Tqla4Height = 3509 / (float)300 * (float)25.4 / (float)1.524;
  307. /// <summary>
  308. /// 初始化
  309. /// </summary>
  310. public void InitTqlpPen()
  311. {
  312. if (App.TQLPenevents == null)
  313. {
  314. App.TQLPenevents = new PenEvents(2);
  315. }
  316. else
  317. {
  318. App.TQLPenevents.InToType = 2;
  319. }
  320. }
  321. /// <summary>
  322. /// 笔落下
  323. /// </summary>
  324. public void TqlDown()
  325. {
  326. _isTqlPenDown = true;
  327. App.W_ScreenRecordingToolbarWindow.PenPractice();
  328. if (_strokes != null && _strokes.StylusPoints.Count > 1)
  329. {
  330. _isFirst = true;
  331. }
  332. _stylusPoints = new StylusPointCollection();
  333. _stylusPoint = new StylusPoint();
  334. _strokes = null;
  335. }
  336. /// <summary>
  337. /// 笔抬起
  338. /// </summary>
  339. public void TqlUp()
  340. {
  341. _isTqlPenDown = false;
  342. if (_strokes != null && _strokes.StylusPoints.Count > 1)
  343. {
  344. _isFirst = true;
  345. }
  346. _stylusPoints = new StylusPointCollection();
  347. _stylusPoint = new StylusPoint();
  348. _strokes = null;
  349. }
  350. /// <summary>
  351. /// 笔书写
  352. /// </summary>
  353. /// <param name="cx"></param>
  354. /// <param name="cy"></param>
  355. /// <param name="force"></param>
  356. public void TqlPenWrite(double cx, double cy, int force)
  357. {
  358. if (this.Visibility == Visibility.Visible)
  359. {
  360. //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
  361. if (!_isTqlPenDown)
  362. {
  363. return;
  364. }
  365. double propW = blackboard_canvas.ActualWidth / Tqla4Height;
  366. double propH = blackboard_canvas.ActualHeight / Tqla4Width;
  367. double tempX = cy * propW;
  368. double tempY = (Tqla4Width - cx) * propH;
  369. Dispatcher.Invoke(() =>
  370. {
  371. //0~1023,亦即有 1024 阶供应用软件后续应用之用,如笔锋笔触,暂无用
  372. //添加笔画
  373. if (_isFirst)
  374. {
  375. _stylusPoint.X = tempX;
  376. _stylusPoint.Y = tempY;
  377. _stylusPoints.Add(_stylusPoint);
  378. if (_stylusPoints.Count > 1)
  379. {
  380. _drawing = new DrawingAttributes
  381. {
  382. Color = _colour,
  383. Width = _wit * 4.5,
  384. Height = _wit * 4.5,
  385. FitToCurve = true,
  386. IgnorePressure = false
  387. };
  388. _strokes = new Stroke(_stylusPoints);
  389. _strokes.DrawingAttributes = _drawing;
  390. blackboard_canvas.Strokes.Add(_strokes);
  391. _isFirst = false;
  392. }
  393. }
  394. else
  395. {
  396. if (blackboard_canvas.Strokes.Count > 0)
  397. {
  398. _stylusPoint.X = tempX;
  399. _stylusPoint.Y = tempY;
  400. _stylusPoints.Add(_stylusPoint);
  401. _strokes = new Stroke(_stylusPoints);
  402. _drawing = new DrawingAttributes
  403. {
  404. Color = _colour,
  405. Width = _wit * 4.5,
  406. Height = _wit * 4.5,
  407. FitToCurve = true,
  408. IgnorePressure = false
  409. };
  410. _strokes.DrawingAttributes = _drawing;
  411. //viewModel.InkStrokes.Add(strokes);
  412. blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = _strokes;
  413. }
  414. }
  415. //设置鼠标位置
  416. if (tempX > 0 && tempY > 0)
  417. {
  418. //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
  419. SetCursorPos((int)((float)tempX * (PrimaryScreen.DpiX / 96f)),
  420. (int)((float)tempY * (PrimaryScreen.DpiY / 96f)));
  421. }
  422. });
  423. }
  424. }
  425. #endregion 腾千里手写笔
  426. }
  427. }