如何在 Metro 风格应用中不使用墨迹管理器在画布上绘图



我想在画布上画一个形状,应该被视为形状,这意味着我画完后我可以从画布中选择、移动、删除。它可以是一个字母或数字或圆圈,可以是我们可以在画布上绘制的任何内容,例如MSPaint。

而且我不想使用墨水管理器,因为不建议用于UWP(https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.inking.inkmanager.aspx)。 是否可以不使用墨迹管理。 这是画布上移动的指针上的代码

void AddPencil(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        if (PenID != 0)
        {
            PointerPoint pt = e.GetCurrentPoint(_drawCanvas);
            CurrentContactPoint = pt.Position;
            X1 = PreviousContactPoint.X;
            Y1 = PreviousContactPoint.Y;
            X2 = CurrentContactPoint.X;
            Y2 = CurrentContactPoint.Y;
            if (Distance(X1, Y1, X2, Y2) > 2.0)
            {
                Line line = new Line()
                  {
                      X1 = X1,
                      Y1 = Y1,
                      X2 = X2,
                      Y2 = Y2,
                      StrokeThickness = 5,
                      Stroke = new SolidColorBrush(Colors.Violet)
                  };
                _drawCanvas.Children.Add(line);
            }

            PathFigure pthFigure = new PathFigure();
            pthFigure.StartPoint = PreviousContactPoint;
            BezierSegment bzSeg = new BezierSegment();
            Point p1 = new Point();
            p1.X = X1;
            p1.Y = Y1;
            Point p2 = new Point();
            p2.X = X2;
            p2.Y = Y2;
            bzSeg.Point1 = p1;
              bzSeg.Point2 =p2;
              bzSeg.Point3 = CurrentContactPoint;
            PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
            myPathSegmentCollection.Add(bzSeg);
            pthFigure.Segments = myPathSegmentCollection;
            if (pthFigureCollection == null)
                pthFigureCollection = new PathFigureCollection();
            pthFigureCollection.Add(pthFigure);
            PreviousContactPoint = CurrentContactPoint;
       }
    }

以及指针释放事件中的代码。

List<UIElement> uielementscolctn = new List<UIElement>();
           foreach(var item in _drawCanvas.Children)
           {
               var x = item as Line;
               uielementscolctn.Add(x);
           }
           foreach (var uielenmt in uielementscolctn)
           {
               _drawCanvas.Children.Remove(uielenmt);
           }
            PathGeometry pthGeometry = new PathGeometry();
            pthGeometry.Figures = pthFigureCollection;
            arcPath = new Path();
           arcPath.Tapped+=arcPath_Tapped;
            translateTransform_ = new TranslateTransform();
            translateTransform_.X = 0.0;
            translateTransform_.Y = 0.0;
            scaleTransform_ = new ScaleTransform();
            scaleTransform_.ScaleX = 0.0;
            scaleTransform_.ScaleY = 0.0;


            arcPath.ManipulationMode = MarkupBase.manipulationMode_;
            arcPath.ManipulationStarted += arcPath_ManipulationStarted;
            arcPath.ManipulationDelta += arcPath_ManipulationDelta;
            arcPath.Stroke = new SolidColorBrush(Colors.Violet);
            arcPath.StrokeThickness = 5;
            arcPath.Data = pthGeometry;
            originalBounds_ = arcPath.Data.Bounds;
            _drawCanvas.Children.Add(arcPath);
             arcPath.Data.Transform = tgroup;
            PenID = 0;
            Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 1);
            pthFigureCollection = null;

无法操作路径形状,因为每当我将值更改为 arcPath.Data.Transform = tgroup 时;路径的数据边界变为零。

你有没有试过看InkCanvas?这是执行所需操作的方法,即使在 UWP 应用中也是如此。

https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.inkcanvas.aspx

使用折线段而不是贝塞尔线段。

最新更新