zoukankan      html  css  js  c++  java
  • WPF inkcanvas选中笔迹缩放、旋转,放大缩小画布、移动画布

    源码_gitee
    源码_github
    使用装饰层实现

    支持蚁行线
    可以直接用inkcanvas的GestureOnly模式实现

    预览绘制形状

    使用说明
    按住ctrl+鼠标滚轮缩放画布
    按住空格+按住鼠标左键+移动鼠标位置拖动画布(需要放大才能使用)

    不使用inkcanvas的select即可屏蔽默认的装饰层

    在MoveRotateAdorner类中的OnRender绘制装饰层

    预览的逻辑是添加一个装饰层绘制预览效果,在mouseup事件进行最后实际效果的绘制

    注意重绘的代码将自身传入,以获取相关属性,同时保证引用不发生变化
    与控制相关的部分代码

          private void Rotate(double angle)
            {
                if (selectionStrokes.Count == 0)
                {
                    return;
                }
                //var bound = selectionStrokes.GetBounds();
    
                Matrix matrix = new Matrix();
                matrix.RotateAt(angle, bound.Left + bound.Width / 2, bound.Top + bound.Height / 2);
                selectionStrokes.Transform(matrix, false);
    
    
                bound = selectionStrokes.GetBounds();
    
            }
    
            //double lastScaleX = 1;
            //double lastScaleY = 1;
            /// <summary>
            /// 缩放
            /// </summary>
            /// <param name="scaleX">缩放比例</param>
            /// <param name="scaleY">缩放比例</param>
            /// <param name="middleX">缩放中心点</param>
            /// <param name="middleY">缩放中心点</param>
            private void ReSize(double scaleX, double scaleY, double middleX, double middleY)
            {
                if ((selectionStrokes?.Count ?? 0) == 0)
                {
                    return;
                }
    
                Matrix matrix = new Matrix();
    
                matrix.ScaleAt(scaleX, scaleY, middleX, middleY);
    
                Trace.WriteLine($"scale x:{ scaleX}, scale y:{ scaleY}");
                selectionStrokes.Transform(matrix, false);
                scaleRound = selectionStrokes.GetBounds();
    
                bound = selectionStrokes.GetBounds();
                ReDraw(this);
            }
            public void Move(double x, double y)
            {
                if ((selectionStrokes?.Count ?? 0) == 0)
                {
                    return;
                }
    
                Matrix matrix = new Matrix();
    
                matrix.OffsetX = x;
                matrix.OffsetY = y;
    
                selectionStrokes.Transform(matrix, false);
                scaleRound = selectionStrokes.GetBounds();
    
                bound = selectionStrokes.GetBounds();
                ReDraw(this);
            }
    
            public void ReDraw(MoveRotateAdorner adorner = null)
            {
                ClearAdorner();
                ReDrawAdorner(adorner);
            }
    
            public void ClearAdorner()
            {
                var layer = AdornerLayer.GetAdornerLayer(adornedElement);
                var arr = layer.GetAdorners(adornedElement);//获取该控件上所有装饰器,返回一个数组
    
                if (arr != null)
                {
                    for (int i = arr.Length - 1; i >= 0; i--)
                    {
                        layer.Remove(arr[i]);
                    }
                }
            }
            public MoveRotateAdorner ReDrawAdorner(MoveRotateAdorner adorner = null)
            {
                var layer = AdornerLayer.GetAdornerLayer(adornedElement);
    
                adorner = adorner ?? new MoveRotateAdorner(adornedElement, selectionStrokes);
                layer.Add(adorner);
                return adorner;
            }
    

    选择笔迹

    //在画布位置处理点击事件
    //mouse up
    //选中笔迹
     foreach (var item in writeBorad.Strokes)
    {
      var result = item.HitTest(paths, 1);
      Trace.WriteLine(result);
      if (result)
      {
        selectionStrokes.Add(item);
      }
    }
    adorner = new MoveRotateAdorner(writeBorad, selectionStrokes);
    if (selectionStrokes.Count == 0)
    {
      adorner?.ClearAdorner();
    }
    else
    {
      adorner?.ReDraw();
    }
    
    留待后查,同时方便他人
    联系我:renhanlinbsl@163.com
  • 相关阅读:
    MYSQL查询表信息
    认识WCF
    asp.net mvc 模型验证注解,表单提交
    asp.net mvc 防止开放重定向
    asp.net webForm登录授权
    C# 压缩文件与字节互转
    C#将字节流加密解密
    获取数据库表详细信息、存储过程、视图、的sql
    Mvc4学习笔记一(Ajax.ActionLink)
    java开发之提高java和mysql代码性能和质量
  • 原文地址:https://www.cnblogs.com/ives/p/14951091.html
Copyright © 2011-2022 走看看