如何使用Metro风格应用程序中的墨水管理器在画布上绘图

时间:2016-04-22 08:38:35

标签: winrt-xaml windows-8.1-universal

我想在画布上绘制一个形状,应该被视为形状,意味着在绘制之后我可以从画布中选择,移动,删除。它可以是字母或数字或圆圈,这可以是我们的任何东西可以像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时;路径的数据边界变为零。

2 个答案:

答案 0 :(得分:0)

您是否尝试过看看InkCanvas?即使在UWP应用程序中,这也是您想要的方式。

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

答案 1 :(得分:0)

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