From 63d95c935cfc02d913748b3a56a518567b691678 Mon Sep 17 00:00:00 2001 From: kriastans Date: Sun, 23 Jun 2024 13:40:43 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20=E5=AF=AB=E4=BA=86=E4=B8=80=E9=BB=9E?= =?UTF-8?q?ICCX=E7=9A=84=E4=BB=A3=E7=A2=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CvtePaintDemo/CvtePaintDemo.csproj | 99 +------------ InkCanvasForClassX/InkCanvasForClassX.csproj | 99 ++----------- InkCanvasForClassX/Libraries/InkProjector.cs | 45 ++++++ .../Libraries/PerfectFreehand.cs | 15 ++ InkCanvasForClassX/Libraries/Vector.cs | 137 ++++++++++++++++++ InkCanvasForClassX/MainWindow.xaml | 6 +- InkCanvasForClassX/MainWindow.xaml.cs | 3 + 7 files changed, 220 insertions(+), 184 deletions(-) create mode 100644 InkCanvasForClassX/Libraries/InkProjector.cs create mode 100644 InkCanvasForClassX/Libraries/PerfectFreehand.cs create mode 100644 InkCanvasForClassX/Libraries/Vector.cs diff --git a/CvtePaintDemo/CvtePaintDemo.csproj b/CvtePaintDemo/CvtePaintDemo.csproj index 9a0a565..dd7f24a 100644 --- a/CvtePaintDemo/CvtePaintDemo.csproj +++ b/CvtePaintDemo/CvtePaintDemo.csproj @@ -1,98 +1,13 @@ - - - + - Debug - AnyCPU - {26F0E9DD-A9DC-45D1-97B9-337C63863837} + net472 WinExe - CvtePaintDemo - CvtePaintDemo - v4.7.2 - 512 - {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 4 - true - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + false + true + true - - - - - - - - - - 4.0 - - - - + + - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - App.xaml - Code - - - MainWindow.xaml - Code - - - - - Code - - - True - True - Resources.resx - - - True - Settings.settings - True - - - ResXFileCodeGenerator - Resources.Designer.cs - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - - - - \ No newline at end of file diff --git a/InkCanvasForClassX/InkCanvasForClassX.csproj b/InkCanvasForClassX/InkCanvasForClassX.csproj index d8fbcc5..38425cb 100644 --- a/InkCanvasForClassX/InkCanvasForClassX.csproj +++ b/InkCanvasForClassX/InkCanvasForClassX.csproj @@ -1,98 +1,17 @@ - - - + - Debug - AnyCPU - {98DF6AA1-DD4D-4C70-A0A2-4B2974D97D51} + net472 WinExe - InkCanvasForClassX - InkCanvasForClassX - v4.7.2 - 512 - {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 4 - true - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + false + true + true - - - - - - - - - - 4.0 - - - - + + + - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - App.xaml - Code - - - MainWindow.xaml - Code - + - - - Code - - - True - True - Resources.resx - - - True - Settings.settings - True - - - ResXFileCodeGenerator - Resources.Designer.cs - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - - - - \ No newline at end of file diff --git a/InkCanvasForClassX/Libraries/InkProjector.cs b/InkCanvasForClassX/Libraries/InkProjector.cs new file mode 100644 index 0000000..cf1ee1c --- /dev/null +++ b/InkCanvasForClassX/Libraries/InkProjector.cs @@ -0,0 +1,45 @@ +using System; +using System.Windows; +using System.Windows.Ink; +using System.Windows.Media; + +namespace InkCanvasForClassX.Libraries +{ + /// + /// 該控件提供一個基於DrawingVisual的最小的StrokeCollection渲染控件,用以替代重量級的InkCanvas控件 + /// + public class InkProjector : FrameworkElement { + private VisualCollection _children; + private DrawingVisual _layer = new DrawingVisual(); + private StrokeCollection _strokes; + + public InkProjector() + { + _children = new VisualCollection(this) { + _layer // 初始化DrawingVisual + }; + } + + public StrokeCollection Strokes { + get => _strokes; + set { + _strokes = value; + DrawInk(); + } + } + + protected override int VisualChildrenCount => _children.Count; + + protected override Visual GetVisualChild(int index) { + if (index < 0 || index >= _children.Count) throw new ArgumentOutOfRangeException(); + return _children[index]; + } + + private void DrawInk() { + DrawingContext context = _layer.RenderOpen(); + _strokes.Draw(context); + context.Close(); + } + + } +} diff --git a/InkCanvasForClassX/Libraries/PerfectFreehand.cs b/InkCanvasForClassX/Libraries/PerfectFreehand.cs new file mode 100644 index 0000000..6d0e2f7 --- /dev/null +++ b/InkCanvasForClassX/Libraries/PerfectFreehand.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InkCanvasForClassX.Libraries +{ + /// + /// 提供對steveruizok/perfect-freehand的C#包裝 + /// + public class PerfectFreehand { + + } +} diff --git a/InkCanvasForClassX/Libraries/Vector.cs b/InkCanvasForClassX/Libraries/Vector.cs new file mode 100644 index 0000000..497093f --- /dev/null +++ b/InkCanvasForClassX/Libraries/Vector.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InkCanvasForClassX.Libraries +{ + /// + /// 提供了對平面向量的坐標運算 + /// + public class Vector { + + private double _x = 0; + private double _y = 0; + + public Vector(double x, double y) { + _x = x; + _y = y; + } + + public double X { + get => _x; + set => _x = value; + } + + public double Y { + get => _y; + set => _y = value; + } + + /// + /// 將該向量改變為其相反向量 + /// + public void Negate() { + _x = -_x; + _y = -_y; + } + + /// + /// 提供一個Vector,返回該Vector的相反向量 + /// + public static Vector NegateVector(Vector vec) { + return new Vector(-vec.X, -vec.Y); + } + + /// + /// Csharp中的Math.Hypot實現 + /// + private static double Hypot(params double[] values) + { + double sum = 0; + foreach (var value in values) { + sum += Math.Pow(value, 2); + } + return Math.Sqrt(sum); + } + + /// + /// 獲取該向量的長度 + /// + public double Length => Vector.Hypot(_x,_y); + + /// + /// 獲取該向量的未開平方的長度 + /// + public double LengthSquared => _x * _x + _y * _y; + + /// + /// 將該向量和另一個向量相加 + /// + public void Add(Vector vec) + { + _x = _x + vec.X; + _y = _y + vec.Y; + } + + /// + /// 提供兩個Vector,返回相加後的Vector + /// + public static Vector AddVectors(Vector vec1, Vector vec2) + { + return new Vector(vec1.X + vec2.X, vec1.Y + vec2.Y); + } + + /// + /// 將該向量減去另一個向量 + /// + public void Subtract(Vector vec) + { + _x = _x - vec.X; + _y = _y - vec.Y; + } + + /// + /// 提供兩個Vector,返回-後的Vector + /// + public static Vector SubtractVectors(Vector vec1, Vector vec2) + { + return new Vector(vec1.X - vec2.X, vec1.Y - vec2.Y); + } + + /// + /// 將該向量除以一個數值 + /// + public void DivideBy(double n) + { + _x = _x / n; + _y = _y / n; + } + + /// + /// 提供兩個Vector,返回/後的Vector + /// + public static Vector DivideByVector(Vector vec, double n) + { + return new Vector(vec.X / n, vec.Y / n); + } + + /// + /// 將該向量乘以一個數值 + /// + public void Multiply(double n) + { + _x = _x * n; + _y = _y * n; + } + + /// + /// 提供兩個Vector,返回*後的Vector + /// + public static Vector MultiplyVector(Vector vec, double n) + { + return new Vector(vec.X * n, vec.Y * n); + } + } +} diff --git a/InkCanvasForClassX/MainWindow.xaml b/InkCanvasForClassX/MainWindow.xaml index 7536b78..f60adcd 100644 --- a/InkCanvasForClassX/MainWindow.xaml +++ b/InkCanvasForClassX/MainWindow.xaml @@ -4,9 +4,11 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:InkCanvasForClassX" + xmlns:libraries="clr-namespace:InkCanvasForClassX.Libraries" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="MainWindow" Height="1300" Width="1300"> - + + diff --git a/InkCanvasForClassX/MainWindow.xaml.cs b/InkCanvasForClassX/MainWindow.xaml.cs index 6a06a65..aadf517 100644 --- a/InkCanvasForClassX/MainWindow.xaml.cs +++ b/InkCanvasForClassX/MainWindow.xaml.cs @@ -23,6 +23,9 @@ namespace InkCanvasForClassX public MainWindow() { InitializeComponent(); + InkC.StrokeCollected += (object sender, InkCanvasStrokeCollectedEventArgs e) => { + InkP.Strokes = InkC.Strokes; + }; } } }