123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858 |
- using Common.system;
-
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
-
- using TStudyDigitalPen.HID;
-
- using XHWK.Model;
-
- namespace XHWK.WKTool
- {
-
-
-
- public partial class PracticeWindow : Window
- {
-
- private DrawingAttributes drawingAttributes;
- private DrawingAttributes drawing;
- private ViewModel viewModel;
-
-
-
- private int flg = 0;
-
-
-
-
- private Color Colour = Colors.Red;
-
-
-
- private int Wit = 2;
-
-
-
- private int Hei = 2;
- public PracticeWindow()
- {
- InitializeComponent();
- InitPen();
- }
- public void Initialize(string _imgPath = null)
- {
- flg = 0;
- blackboard_canvas.Strokes.Clear();
- blackboard_canvas.UseCustomCursor = true;
-
-
-
-
-
-
-
-
-
-
-
- drawingAttributes = new DrawingAttributes();
-
-
-
-
-
-
-
-
-
- blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
- Pen();
-
-
-
-
-
-
-
- viewModel = new ViewModel
- {
- InkStrokes = new StrokeCollection(),
- };
-
- DataContext = viewModel;
-
-
- }
-
-
-
-
-
- public void White()
- {
-
- drawingAttributes.Color = Colors.White;
- Colour = Colors.White;
- }
-
-
-
-
-
- public void Red()
- {
- drawingAttributes.Color = Colors.Red;
- Colour = Colors.Red;
- }
-
-
-
-
-
- public void Gray()
- {
-
- drawingAttributes.Color = Colors.Black;
- Colour = Colors.Black;
- }
-
-
-
-
-
- public void CyanBlue()
- {
-
- drawingAttributes.Color = Colors.LimeGreen;
- Colour = Colors.LimeGreen;
- }
-
-
-
-
-
- public void Yellow()
- {
-
- drawingAttributes.Color = Colors.Gold;
- Colour = Colors.Gold;
- }
-
-
-
-
-
- public void Blue()
- {
-
- drawingAttributes.Color = Colors.DeepSkyBlue;
- Colour = Colors.DeepSkyBlue;
- }
-
-
-
-
-
- public void Fine()
- {
-
- drawingAttributes.Width = 1;
- drawingAttributes.Height = 1;
- Wit = 1;
- Hei = 1;
- }
-
-
-
-
-
- public void In()
- {
-
- drawingAttributes.Width = 3;
- drawingAttributes.Height = 3;
- Wit = 3;
- Hei = 3;
-
- }
-
-
-
-
-
- public void Crude()
- {
-
- drawingAttributes.Width = 5;
- drawingAttributes.Height = 5;
- Wit = 5;
- Hei = 5;
- }
-
-
-
- public void Eraser()
- {
- flg = 0;
-
- blackboard_canvas.UseCustomCursor = false;
- blackboard_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
- blackboard_canvas.EraserShape = new EllipseStylusShape(64, 64, 0);
- }
-
-
-
- public void Pen()
- {
- flg = 0;
- blackboard_canvas.EditingMode = InkCanvasEditingMode.Ink;
- blackboard_canvas.UseCustomCursor = true;
- drawingAttributes.FitToCurve = true;
- drawingAttributes.IgnorePressure = false;
-
- blackboard_canvas.Cursor = Cursors.Pen;
- }
-
-
-
- public void Round()
- {
- flg = 2;
- drawingAttributes = new DrawingAttributes
- {
- Color = Colour,
- Width = Wit,
- Height = Hei,
- StylusTip = StylusTip.Rectangle,
-
- IsHighlighter = false,
- IgnorePressure = true,
-
- };
- blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
- blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
- blackboard_canvas.Cursor = Cursors.Cross;
- }
-
-
-
- public void Rectangle()
- {
- flg = 1;
- drawingAttributes = new DrawingAttributes
- {
- Color = Colour,
- Width = Wit,
- Height = Hei,
- StylusTip = StylusTip.Rectangle,
-
- IsHighlighter = false,
- IgnorePressure = true,
-
- };
- blackboard_canvas.DefaultDrawingAttributes = drawingAttributes;
- blackboard_canvas.EditingMode = InkCanvasEditingMode.None;
- blackboard_canvas.Cursor = Cursors.Cross;
- }
- private System.Windows.Point iniP;
- private void blackboard_canvas_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (flg != 0)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- iniP = e.GetPosition(blackboard_canvas);
- }
- }
- }
-
- private Stroke StrokeRound;
- private Stroke StrokeRectangle;
- private void blackboard_canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (flg != 0)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
-
- if (flg == 1)
- {
- System.Windows.Point endP = e.GetPosition(blackboard_canvas);
- List<System.Windows.Point> pointList = new List<System.Windows.Point>
- {
- new System.Windows.Point(iniP.X, iniP.Y),
- new System.Windows.Point(iniP.X, endP.Y),
- new System.Windows.Point(endP.X, endP.Y),
- new System.Windows.Point(endP.X, iniP.Y),
- new System.Windows.Point(iniP.X, iniP.Y),
- };
-
- StylusPointCollection point = new StylusPointCollection(pointList);
- Stroke stroke = new Stroke(point)
- {
- DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
- };
- if (StrokeRectangle != null)
- {
- viewModel.InkStrokes.Remove(StrokeRectangle);
- }
- StrokeRectangle = stroke;
-
- viewModel.InkStrokes.Add(stroke);
- }
-
- else if (flg == 2)
- {
- System.Windows.Point endP = e.GetPosition(blackboard_canvas);
- List<System.Windows.Point> pointList = GenerateEclipseGeometry(iniP, endP);
- StylusPointCollection point = new StylusPointCollection(pointList);
- Stroke stroke = new Stroke(point)
- {
- DrawingAttributes = blackboard_canvas.DefaultDrawingAttributes.Clone()
- };
-
- if (StrokeRound != null)
- {
- viewModel.InkStrokes.Remove(StrokeRound);
- }
- StrokeRound = stroke;
- viewModel.InkStrokes.Add(stroke);
- }
- }
- }
- }
- private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed)
- {
- double a = 0.5 * (ed.X - st.X);
- double b = 0.5 * (ed.Y - st.Y);
- List<System.Windows.Point> pointList = new List<System.Windows.Point>();
- for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
- {
- pointList.Add(new System.Windows.Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
- }
- return pointList;
- }
-
-
-
-
-
- private void blackboard_canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- StrokeRound = null;
- StrokeRectangle = null;
- }
-
-
-
-
-
- private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
- {
- APP.W_ScreenRecordingToolbarWindow.flg = 0;
- }
- else
- {
- APP.W_ScreenRecordingToolbarWindow.flg = 1;
- }
- Hide();
- }
-
-
-
- void SwitchPages()
- {
- try
- {
- new Thread(() =>
- {
- Thread.Sleep(500);
- MouseEventCommon.MouseMiddleClickEvent(0);
- }).Start();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
-
-
- private void blackboard_canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
- {
- APP.W_ScreenRecordingToolbarWindow.flg = 0;
- }
- else
- {
- APP.W_ScreenRecordingToolbarWindow.flg = 1;
- }
- Hide();
- }
-
- private void blackboard_canvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (APP.W_ScreenRecordingToolbarWindow.flg == 10)
- {
- APP.W_ScreenRecordingToolbarWindow.flg = 0;
- }
- else
- {
- APP.W_ScreenRecordingToolbarWindow.flg = 1;
- }
- ReturnPractice();
-
- SwitchPages();
-
- }
-
-
-
- public void ReturnPractice()
- {
- blackboard_canvas.Strokes.Clear();
- new Thread(new ThreadStart(new Action(() =>
- {
- Dispatcher.Invoke(() =>
- {
- APP.W_ScreenRecordingToolbarWindow.ModifyState();
- });
- flg = 0;
- Thread.Sleep(500);
- Dispatcher.Invoke(() =>
- {
- Owner = null;
- APP.W_PracticeWindow.Hide();
- });
- }))).Start();
- }
-
-
- #region 点阵笔相关
- #region 值初始化
-
-
-
-
-
- private const int A4_WIDTH = 5600;
-
-
-
-
- private const int A4_HEIGHT = 7920;
-
-
-
-
-
-
-
-
-
- private List<System.Drawing.Point> stroke;
-
-
-
-
- private string penSerial;
-
-
-
-
- private bool isPenDown;
-
-
- private string currentPageSerial = string.Empty;
-
-
-
- private Dictionary<string, System.Drawing.Image> pagesDic = new Dictionary<string, System.Drawing.Image>();
- #endregion
- 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());
- }
- }
-
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
- 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 (strokes != null && strokes.StylusPoints.Count > 1)
- {
- isFirst = true;
- }
- stylusPoints = new StylusPointCollection();
- stylusPoint = new StylusPoint();
- strokes = null;
- }
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
- }
- }
-
-
-
-
-
-
-
- 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.GetPenData(penSerial);
-
-
-
-
-
-
-
- }
- }
-
-
-
-
-
-
-
- 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
- {
-
- }
- }
-
-
-
-
-
-
-
-
- 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
- {
-
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- private void OnPenCoordinate(ulong time, string penSerial, int penType, string pageSerial, int cx, int cy, byte force)
- {
- 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;
-
- if (true)
- {
- Dispatcher.Invoke(new Action(() =>
- {
- float Pressure = force / 100f;
-
-
-
- if (isFirst)
- {
- stylusPoint.X = testX;
- stylusPoint.Y = testY;
-
-
-
-
-
-
-
-
-
- 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 = 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;
-
-
- blackboard_canvas.Strokes[blackboard_canvas.Strokes.Count - 1] = strokes;
- }
- }
-
-
- if (testX > 0 && testY > 0)
- {
-
- 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;
-
-
-
- public void StopDigitalPen()
- {
-
- APP.digitalPen.Stop();
- }
-
-
-
- public void ClearPenStorage()
- {
- if (!string.IsNullOrEmpty(penSerial))
- {
- APP.digitalPen.ClearMemory(penSerial);
- }
- }
-
-
-
- public void GetPenElectricityQuantity()
- {
- if (!string.IsNullOrEmpty(penSerial))
- {
- APP.digitalPen.GetBatteryCapacity(penSerial);
- }
- }
-
-
-
-
- public void GetUsedStorage()
- {
- if (!string.IsNullOrEmpty(penSerial))
- {
- APP.digitalPen.GetMemoryFillLevel(penSerial);
- }
- }
-
-
-
-
- public void 开启悬浮()
- {
- if (!string.IsNullOrEmpty(penSerial))
- {
- APP.digitalPen.SetPenHoverMode(true, penSerial);
- }
- }
-
-
-
-
- public void 关闭悬浮()
- {
- if (!string.IsNullOrEmpty(penSerial))
- {
- APP.digitalPen.SetPenHoverMode(false, penSerial);
- }
- }
-
-
-
-
-
-
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- private static extern int SetCursorPos(int x, int y);
-
- #endregion
-
- }
- }
|