WPF inkcanvas选中笔迹缩放、旋转,放大缩小画布、移动画布

源码_gitee
源码_github
使用装饰层实现
WPF  inkcanvas选中笔迹缩放、旋转,放大缩小画布、移动画布

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

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

在MoveRotateAdorner类中的OnRender绘制装饰层

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

      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();
}
上一篇:Invalid bound statement (not found) 无效的绑定语句(未找到)


下一篇:导弹拦截系统