inkcanvas根据鼠标、触摸的时长自动调整笔迹粗细

前台

<Window x:Class="FinalTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FinalTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <InkCanvas Name="inkCanvas" IsManipulationEnabled="True"/>
    </Grid>
</Window>

后台

 public partial class MainWindow : Window
 {
     private DateTime _strokeStartTime;
     private Stroke _currentStroke;
     private bool _isMouseDrawing;

     public MainWindow()
     {
         InitializeComponent();
         InitializeInkCanvas();
     }


     private void InitializeInkCanvas()
     {
         // 基础配置
         inkCanvas.DefaultDrawingAttributes = new DrawingAttributes
         {
             Color = Colors.Black,
             Width = 2,
             Height = 2
         };

         // 触控笔事件
         inkCanvas.StylusDown += Input_Start;
         inkCanvas.StylusUp += Input_End;
         inkCanvas.StylusMove += Stylus_Move;

         // 鼠标事件
         inkCanvas.PreviewMouseDown += Input_Start;
         inkCanvas.MouseUp += Input_End;
         inkCanvas.MouseMove += Mouse_Move;
     }


     // 统一输入开始处理
     private void Input_Start(object sender, InputEventArgs e)
     {
         // 防止重复触发
         if (_currentStroke != null) return;

         _strokeStartTime = DateTime.Now;

         // 创建新笔迹
         _currentStroke = CreateNewStroke(e);
         inkCanvas.Strokes.Add(_currentStroke);

         // 标记鼠标绘制状态
         _isMouseDrawing = e is MouseButtonEventArgs;
     }

     // 统一输入结束处理
     private void Input_End(object sender, InputEventArgs e)
     {
         if (_currentStroke == null) return;

         // 计算持续时间
         var duration = DateTime.Now - _strokeStartTime;
         AdjustStrokeWidth(_currentStroke, duration);

         // 重置状态
         _currentStroke = null;
         _isMouseDrawing = false;
     }

     // 处理鼠标移动时的笔迹更新
     private void Mouse_Move(object sender, MouseEventArgs e)
     {
         if (!_isMouseDrawing || _currentStroke == null) return;

         // 添加当前鼠标位置到笔迹
         var point = e.GetPosition(inkCanvas);
         _currentStroke.StylusPoints.Add(
             new StylusPoint(point.X, point.Y, 0.5f) // 默认压力值
         );
     }


     private void Stylus_Move(object sender, StylusEventArgs e)
     {
         if (_isMouseDrawing || _currentStroke == null) return;

         // 添加当前鼠标位置到笔迹
         var stylusPos = e.GetStylusPoints(inkCanvas);
         _currentStroke.StylusPoints.Add(stylusPos);
     }

     // 创建新笔迹的通用方法
     private Stroke CreateNewStroke(InputEventArgs e)
     {
         StylusPointCollection points = new StylusPointCollection();

         switch (e)
         {
             case StylusDownEventArgs stylusEvent:
                 points = stylusEvent.GetStylusPoints(inkCanvas);
                 break;

             case MouseButtonEventArgs mouseEvent:
                 var pos = mouseEvent.GetPosition(inkCanvas);
                 points = new StylusPointCollection
                 {
                     new StylusPoint(pos.X, pos.Y, 0.5f) // 模拟压力值
                 };
                 break;
             default:
                 throw new NotSupportedException("Unsupported input type");
         }

         return new Stroke(points)
         {
             DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
         };
     }
     // 调整笔迹宽度(保持不变)
     private void AdjustStrokeWidth(Stroke stroke, TimeSpan duration)
     {
         double baseWidth = 2;
         double maxWidth = 10;
         double timeFactor = Math.Min(duration.TotalSeconds, 2);

         // 使用二次曲线调整
         double ratio = Math.Pow(timeFactor / 2, 2);
         double newWidth = baseWidth + (maxWidth - baseWidth) * ratio;

         stroke.DrawingAttributes.Width = newWidth;
         stroke.DrawingAttributes.Height = newWidth;
     }
 }
posted @ 2025-02-23 21:33  Hey,Coder!  阅读(3)  评论(0编辑  收藏  举报