2025-05-25 09:29:48 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Ink;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
namespace Ink_Canvas.Helpers
|
|
|
|
|
{
|
|
|
|
|
public class VisualCanvas : FrameworkElement
|
|
|
|
|
{
|
|
|
|
|
protected override Visual GetVisualChild(int index)
|
|
|
|
|
{
|
|
|
|
|
return Visual;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override int VisualChildrenCount => 1;
|
|
|
|
|
|
|
|
|
|
public VisualCanvas(DrawingVisual visual)
|
|
|
|
|
{
|
|
|
|
|
Visual = visual;
|
|
|
|
|
AddVisualChild(visual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DrawingVisual Visual { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-13 21:38:03 +08:00
|
|
|
/// 用于显示笔迹的类
|
2025-05-25 09:29:48 +08:00
|
|
|
/// </summary>
|
|
|
|
|
public class StrokeVisual : DrawingVisual
|
|
|
|
|
{
|
2025-09-13 21:38:03 +08:00
|
|
|
private bool _needsRedraw = true;
|
2025-10-01 00:53:22 +08:00
|
|
|
private int _lastPointCount = 0;
|
2025-10-03 17:08:46 +08:00
|
|
|
private const int REDRAW_THRESHOLD = 3;
|
2025-09-13 21:38:03 +08:00
|
|
|
|
2025-05-25 09:29:48 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 创建显示笔迹的类
|
|
|
|
|
/// </summary>
|
2025-07-28 14:40:44 +08:00
|
|
|
public StrokeVisual() : this(new DrawingAttributes
|
2025-05-25 09:29:48 +08:00
|
|
|
{
|
|
|
|
|
Color = Colors.Red,
|
|
|
|
|
//FitToCurve = true,
|
|
|
|
|
Width = 3,
|
|
|
|
|
Height = 3
|
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建显示笔迹的类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="drawingAttributes"></param>
|
|
|
|
|
public StrokeVisual(DrawingAttributes drawingAttributes)
|
|
|
|
|
{
|
|
|
|
|
_drawingAttributes = drawingAttributes;
|
2025-10-03 17:08:46 +08:00
|
|
|
|
2025-09-13 21:38:03 +08:00
|
|
|
// 启用硬件加速
|
|
|
|
|
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
|
|
|
|
|
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
|
|
|
|
|
RenderOptions.SetCachingHint(this, CachingHint.Cache);
|
2025-05-25 09:29:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-13 21:38:03 +08:00
|
|
|
/// 设置或获取显示的笔迹
|
2025-05-25 09:29:48 +08:00
|
|
|
/// </summary>
|
|
|
|
|
public Stroke Stroke { set; get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-13 21:38:03 +08:00
|
|
|
/// 在笔迹中添加点
|
2025-05-25 09:29:48 +08:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="point"></param>
|
|
|
|
|
public void Add(StylusPoint point)
|
|
|
|
|
{
|
|
|
|
|
if (Stroke == null)
|
|
|
|
|
{
|
|
|
|
|
var collection = new StylusPointCollection { point };
|
|
|
|
|
Stroke = new Stroke(collection) { DrawingAttributes = _drawingAttributes };
|
2025-09-13 21:38:03 +08:00
|
|
|
_lastPointCount = 1;
|
2025-05-25 09:29:48 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Stroke.StylusPoints.Add(point);
|
2025-09-13 21:38:03 +08:00
|
|
|
_lastPointCount++;
|
2025-05-25 09:29:48 +08:00
|
|
|
}
|
2025-10-03 17:08:46 +08:00
|
|
|
|
2025-09-13 21:38:03 +08:00
|
|
|
// 标记需要重绘
|
|
|
|
|
_needsRedraw = true;
|
2025-05-25 09:29:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-13 21:38:03 +08:00
|
|
|
/// 重新画出笔迹
|
2025-05-25 09:29:48 +08:00
|
|
|
/// </summary>
|
|
|
|
|
public void Redraw()
|
|
|
|
|
{
|
2025-09-13 21:38:03 +08:00
|
|
|
if (!_needsRedraw || Stroke == null) return;
|
2025-10-03 17:08:46 +08:00
|
|
|
|
2025-09-13 21:38:03 +08:00
|
|
|
if (_lastPointCount % REDRAW_THRESHOLD != 0 && _lastPointCount > REDRAW_THRESHOLD)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 09:29:48 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var dc = RenderOpen())
|
|
|
|
|
{
|
|
|
|
|
Stroke.Draw(dc);
|
|
|
|
|
}
|
2025-09-13 21:38:03 +08:00
|
|
|
_needsRedraw = false;
|
2025-05-25 09:29:48 +08:00
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-13 21:38:03 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 强制重绘
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void ForceRedraw()
|
|
|
|
|
{
|
|
|
|
|
_needsRedraw = true;
|
|
|
|
|
Redraw();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 09:29:48 +08:00
|
|
|
private readonly DrawingAttributes _drawingAttributes;
|
|
|
|
|
|
|
|
|
|
public static implicit operator Stroke(StrokeVisual v)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|