123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- using Common.system;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using TStudyDigitalPen.HID;
-
- namespace XHPZ.Desktop
- {
- /// <summary>
- /// PracticeWindow.xaml 的交互逻辑
- /// </summary>
- public partial class PracticeWindow : Window
- {
- private DrawingAttributes drawingAttributes;
- private DrawingAttributes drawing;
- /// <summary>
- /// 屏幕宽
- /// </summary>
- internal double pwidth = SystemParameters.PrimaryScreenWidth;
- /// <summary>
- /// 屏幕高
- /// </summary>
- internal double pHeight = SystemParameters.PrimaryScreenHeight;
- /// <summary>
- /// 当前画笔颜色
- /// </summary>
- private Color Colour = Colors.Red;
- /// <summary>
- /// 当前画笔宽
- /// </summary>
- private int Wit = 2;
- /// <summary>
- /// A4点阵纸点阵宽度
- /// </summary>
- private const int A4_WIDTH = 5600;
-
- /// <summary>
- /// A4点阵纸点阵高度
- /// </summary>
- private const int A4_HEIGHT = 7920;
- /// <summary>
- /// 引用user32.dll动态链接库(windows api),
- /// 使用库中定义 API:SetCursorPos
- /// 设置光标位置
- /// </summary>
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- private static extern int SetCursorPos(int x, int y);
- public PracticeWindow()
- {
- InitializeComponent();
- InitPen();
- InitTQLPPen();
- }
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="_imgPath"></param>
- public void Initialize(string _imgPath = null)
- {
- blackboard_canvas.Strokes.Clear();
- blackboard_canvas.UseCustomCursor = true;
- //创建 DrawingAttributes 类的一个实例
- drawingAttributes = new DrawingAttributes();
- blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
- Pen();
- }
- public void InitPen()
- {
- //stroke = new List<System.Drawing.Point>();
- //获取点阵笔实例,并绑定点阵笔事件
- //将授权文件内容传入,获取点阵笔对象实例
- APP.digitalPen = DigitalPenHID.GetInstance(certificates.MyLicense.Bytes);
- //绑定笔连接事件
- APP.digitalPen.PenConnected += OnPenConnect;
- //绑定笔断开事件
- APP.digitalPen.PenDisconnect += OnPenDisconnect;
- //绑定笔书写输出坐标事件
- APP.digitalPen.PenCoordinate += OnPenCoordinate;
- //绑定抬笔事件
- APP.digitalPen.PenUp += OnPenUp;
- //绑定落笔事件
- APP.digitalPen.PenDown += OnPenDown;
- APP.digitalPen.PenBatteryCapacity += OnBatteryCapacity;
- APP.digitalPen.PenMemoryFillLevel += OnMemoryFillLevel;
- //完成初始化点阵笔,开始与点阵笔通信
- ERROR_CODE ER = APP.digitalPen.Start();
- //判断是否成功
- //if (ER != ERROR_CODE.ERROR_OK)
- //{
- // MessageWindow.Show("初始化失败,授权过期,返回值:" + ER.ToString());
- //}
- }
- /// <summary>
- /// 画笔
- /// </summary>
- public void Pen()
- {
- blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
- blackboard_canvas.UseCustomCursor = true;
- drawingAttributes.FitToCurve = true;
- drawingAttributes.IgnorePressure = false;
- blackboard_canvas.Cursor = Cursors.Pen;
- }
- /// <summary>
- /// 笔连接
- /// </summary>
- /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
- /// <param name="penSerial">点阵笔序列号</param>
- /// <param name="penType">点阵笔型号编号</param>
- private void OnPenConnect(ulong time, string penSerial, int penType)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenConnect);
- Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
- }
- else
- {
- APP.PenSerial = penSerial;
- APP.PenStatus = true;
- this.penSerial = penSerial;
- //连接后,在获取笔数据前,可以清除笔内的历史数据
- //APP.digitalPen.ClearMemory(penSerial);
-
- //开始接收笔数据
- APP.digitalPen.GetPenData(penSerial);
- //UpdateDevStatus();
- ////Dispatcher.Invoke(new Action(() =>
- ////{
- //// txbNotConnected.Text = "已连接";
- //// txbNotConnecteds.Text = "已连接";
- ////}));
-
- }
- }
- /// <summary>
- /// 笔断开
- /// </summary>
- /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
- /// <param name="penSerial">点阵笔序列号</param>
- /// <param name="penType">点阵笔型号编号</param>
- private void OnPenDisconnect(ulong time, string penSerial, int penType)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDisconnect);
- Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
- }
- else
- {
- APP.PenSerial = penSerial;
- APP.PenStatus = false;
- //UpdateDevStatus();
- ////Dispatcher.Invoke(new Action(() =>
- ////{
- //// txbNotConnected.Text = "未连接";
- //// txbNotConnecteds.Text = "未连接";
- ////}));
- }
- }
- /// <summary>
- /// 笔序列号
- /// </summary>
- private string penSerial;
-
- /// <summary>
- /// 笔是否在点
- /// </summary>
- private bool isPenDown;
- /// <summary>
- /// 笔书写,收到坐标
- /// </summary>
- /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
- /// <param name="penSerial">点阵笔序列号</param>
- /// <param name="penType">点阵笔型号编号</param>
- /// <param name="pageSerial">点阵地址</param>
- /// <param name="cx">x坐标</param>
- /// <param name="cy">y坐标</param>
- /// <param name="force">压力值</param>
- private void OnPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
- {
- if (this.Visibility == Visibility.Visible)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int, string, int, int, byte> ac = new Action<ulong, string, int, string, int, int, byte>(OnPenCoordinate);
- Dispatcher.Invoke(ac, new object[] { time, pageSerial, penType, pageSerial, cx, cy, force });
- }
- else
- {
- //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
- if (!isPenDown)
- {
- return;
- }
- //stroke.Add(new System.Drawing.Point(cx, cy));
-
- double PropW = blackboard_canvas.ActualWidth / A4_HEIGHT;
- double PropH = blackboard_canvas.ActualHeight / A4_WIDTH;
- //点
- double testX = cy * PropW;
- double testY = (A4_WIDTH - cx) * PropH;
- //pageSerial //点阵IP地址 与打印的页面关联
- if (true)
- {
- Dispatcher.Invoke(new Action(() =>
- {
- float Pressure = force / 100f;
- ////添加笔画
- //myblackboard.changepages(testX, testY, false, Color, PenSize, APP.pageData.currpage - 1, Pressure);
-
- if (isFirst)
- {
- stylusPoint.X = testX;
- stylusPoint.Y = testY;
- //_color.A = (byte)(Pressure * 255f);
- //stylusPoint.PressureFactor = Pressure;
-
- //for(int i=0;i<100;i++)
- //{
- // stylusPoint.X--;
- // stylusPoint.Y--;
- // stylusPoints.Add(stylusPoint);
- //}
- stylusPoints.Add(stylusPoint);
- if (stylusPoints.Count > 1)
- {
- drawing = new DrawingAttributes
- {
- Color = Colour,
- Width = Wit * 4.5,
- Height = Wit * 4.5,
- FitToCurve = true,
- IgnorePressure = false
- };
-
- //blackboard_canvas.Strokes = new StrokeCollection();
- strokes = new Stroke(stylusPoints);
-
-
- strokes.DrawingAttributes = drawing;
- //blackboard_canvas.Strokes.Add(strokes);
-
- blackboard_canvas.Strokes.Add(strokes);
- isFirst = false;
- }
- }
- else
- {
- if (blackboard_canvas.Strokes.Count > 0)
- {
- stylusPoint.X = testX;
- stylusPoint.Y = testY;
- stylusPoints.Add(stylusPoint);
- strokes = new Stroke(stylusPoints);
- drawing = new DrawingAttributes
- {
- Color = Colour,
- Width = Wit * 4.5,
- Height = Wit * 4.5,
- FitToCurve = true,
- IgnorePressure = false
- };
- strokes.DrawingAttributes = drawing;
-
- //viewModel.InkStrokes.Add(strokes);
- blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
- }
- }
-
- //设置鼠标位置
- if (testX > 0 && testY > 0)
- {
- //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
- SetCursorPos((int)((float)testX * (PrimaryScreen.DpiX / 96f)), (int)((float)testY * (PrimaryScreen.DpiY / 96f)));
- }
- }));
- }
-
- }
- }
- }
- bool isFirst = true;
- private StylusPointCollection stylusPoints = new StylusPointCollection();
- private StylusPoint stylusPoint = new StylusPoint();
- private Stroke strokes;
-
-
-
- /// <summary>
- /// 抬笔
- /// </summary>
- /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
- /// <param name="penSerial">点阵笔序列号</param>
- /// <param name="penType">点阵笔型号编号</param>
- private void OnPenUp(ulong time, string penSerial, int penType)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenUp);
- Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
- }
- else
- {
- isPenDown = false;
- APP.PenSerial = penSerial;
-
- //stroke.Clear();
- }
- //if (APP.pageData.currpage > 0)
- //{
- // Dispatcher.Invoke(new Action(() =>
- // {
- // myblackboard.changepages(0, 0, true, Color, PenSize, APP.pageData.currpage - 1, 0);
- // }));
- //}
-
-
- if (strokes != null && strokes.StylusPoints.Count > 1)
- {
- isFirst = true;
- }
- stylusPoints = new StylusPointCollection();
- stylusPoint = new StylusPoint();
- strokes = null;
- }
- /// <summary>
- /// 落笔
- /// </summary>
- /// <param name="time">时间戳,1970年1月1日到现在的总毫秒数</param>
- /// <param name="penSerial">点阵笔序列号</param>
- /// <param name="penType">点阵笔型号编号</param>
- private void OnPenDown(ulong time, string penSerial, int penType)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int> action = new Action<ulong, string, int>(OnPenDown);
- Dispatcher.Invoke(action, new object[] { time, penSerial, penType });
- }
- else
- {
- isPenDown = true;
- if (APP.W_PracticeWindow != null && APP.W_PracticeWindow.Visibility == Visibility.Hidden)
- {
- PenPractice();
- }
- }
- }
-
- void PenPractice()
- {
- Dispatcher.Invoke(() =>
- {
- if (APP.W_PracticeWindow == null)
- {
- APP.W_PracticeWindow = new PracticeWindow
- {
- Width = pwidth,
- Height = pHeight,
- Left = 0,
- Top = 0
- };
- }
- APP.W_PracticeWindow.Topmost = true;
- APP.W_PracticeWindow.Initialize();
- APP.W_PracticeWindow.Show();
- });
- }
- /// <summary>
- /// 电池电量
- /// </summary>
- /// <param name="time"></param>
- /// <param name="penSerial"></param>
- /// <param name="penType"></param>
- /// <param name="capacity"></param>
- private void OnBatteryCapacity(ulong time, string penSerial, int penType, byte capacity)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnBatteryCapacity);
- Dispatcher.Invoke(action, new object[] { time, penSerial, penType, capacity });
- }
- else
- {
- //System.Windows.MessageWindow.Show("电池电量:" + capacity.ToString());
- }
- }
- /// <summary>
- /// 已用存储
- /// </summary>
- /// <param name="time"></param>
- /// <param name="penSerial"></param>
- /// <param name="penType"></param>
- /// <param name="fillLevel"></param>
- private void OnMemoryFillLevel(ulong time, string penSerial, int penType, byte fillLevel)
- {
- if (CheckAccess())
- {
- Action<ulong, string, int, byte> action = new Action<ulong, string, int, byte>(OnMemoryFillLevel);
- Dispatcher.Invoke(action, new object[] { time, penSerial, penType, fillLevel });
- }
- else
- {
- //System.Windows.MessageWindow.Show("存储:" + fillLevel.ToString());
- }
- }
- /// <summary>
- /// 退出批注
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- ReturnPractice();
- SwitchPages();
- }
- /// <summary>
- /// 退出批注并清空画板
- /// </summary>
- public void ReturnPractice()
- {
- blackboard_canvas.Strokes.Clear();
- new Thread(new ThreadStart(new Action(() =>
- {
-
- Thread.Sleep(500);
- Dispatcher.Invoke(() =>
- {
- Owner = null;
- APP.W_PracticeWindow.Hide();
- });
- }))).Start();
- }
- /// <summary>
- /// 鼠标中建点击
- /// </summary>
- void SwitchPages()
- {
- try
- {
- new Thread(() =>
- {
- Thread.Sleep(500);
- MouseEventCommon.MouseMiddleClickEvent(0);
- }).Start();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
-
-
- #region 腾千里手写笔
- /// <summary>
- /// 是否在书写
- /// </summary>
- bool isTQLPenDown = false;
- /// <summary>
- /// TQL点阵笔宽高
- /// </summary>
- static float TQLA4_WIDTH = (float)1075 / (float)150 * (float)25.4 / (float)1.524; //OID4
- static float TQLA4_HEIGHT = (float)1512 / (float)150 * (float)25.4 / (float)1.524;
- /// <summary>
- /// 初始化
- /// </summary>
- public void InitTQLPPen()
- {
- if (APP.TQLPenevents == null)
- {
- APP.TQLPenevents = new PenEvents();
- }
- }
- /// <summary>
- /// 笔落下
- /// </summary>
- public void TQLDown()
- {
- isTQLPenDown = true;
- if(APP.W_PracticeWindow!=null&&APP.W_PracticeWindow.Visibility==Visibility.Hidden)
- {
- PenPractice();
- }
-
- if (strokes != null && strokes.StylusPoints.Count > 1)
- {
- isFirst = true;
- }
- stylusPoints = new StylusPointCollection();
- stylusPoint = new StylusPoint();
- strokes = null;
- }
- /// <summary>
- /// 笔抬起
- /// </summary>
- public void TQLUp()
- {
- isTQLPenDown = false;
-
- if (strokes != null && strokes.StylusPoints.Count > 1)
- {
- isFirst = true;
- }
- stylusPoints = new StylusPointCollection();
- stylusPoint = new StylusPoint();
- strokes = null;
- }
- /// <summary>
- /// 笔书写
- /// </summary>
- /// <param name="cx"></param>
- /// <param name="cy"></param>
- /// <param name="force"></param>
- public void TQLPenWrite(double cx, double cy, int force)
- {
- if (this.Visibility == Visibility.Visible)
- {
- //判断是否是落笔后输出的坐标,在设置悬浮模式下,落笔前的悬浮坐标不绘制
- if (!isTQLPenDown)
- {
- return;
- }
- double PropW = blackboard_canvas.ActualWidth / TQLA4_HEIGHT;
- double PropH = blackboard_canvas.ActualHeight / TQLA4_WIDTH;
-
- double tempX = cy * PropW;
- double tempY = (TQLA4_WIDTH - cx) * PropH;
- Dispatcher.Invoke(new Action(() =>
- {
- //0~1023,亦即有 1024 阶供应用软件后续应用之用,如笔锋笔触,暂无用
- float Pressure = (float)force / 1023f;
- //添加笔画
- if (isFirst)
- {
- stylusPoint.X = tempX;
- stylusPoint.Y = tempY;
- stylusPoints.Add(stylusPoint);
- if (stylusPoints.Count > 1)
- {
- drawing = new DrawingAttributes
- {
- Color = Colour,
- Width = Wit * 4.5,
- Height = Wit * 4.5,
- FitToCurve = true,
- IgnorePressure = false
- };
-
- strokes = new Stroke(stylusPoints);
-
- strokes.DrawingAttributes = drawing;
-
- blackboard_canvas.Strokes.Add(strokes);
- isFirst = false;
- }
- }
- else
- {
- if (blackboard_canvas.Strokes.Count > 0)
- {
- stylusPoint.X = tempX;
- stylusPoint.Y = tempY;
- stylusPoints.Add(stylusPoint);
- strokes = new Stroke(stylusPoints);
- drawing = new DrawingAttributes
- {
- Color = Colour,
- Width = Wit * 4.5,
- Height = Wit * 4.5,
- FitToCurve = true,
- IgnorePressure = false
- };
- strokes.DrawingAttributes = drawing;
-
- //viewModel.InkStrokes.Add(strokes);
- blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
- }
- }
-
- //设置鼠标位置
- if (tempX > 0 && tempY > 0)
- {
- //System.Windows.Point getP = scroMain.PointToScreen(new System.Windows.Point(testX, testY - scroMain.VerticalOffset));
- SetCursorPos((int)((float)tempX * (PrimaryScreen.DpiX / 96f)), (int)((float)tempY * (PrimaryScreen.DpiY / 96f)));
- }
- }));
- }
- }
- #endregion
-
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- Hide();
- }
- }
- }
|