用手写笔绘制画布 - Windows 10 Universal Application C#

时间:2016-05-05 12:09:34

标签: c# win-universal-app

我正在使用C#中的Windows 10 Universal Application。我有一个画布控件,用户可以在其上绘制签名。我附加了使用canvas控件附加的PointerPressed,PointerMoved,PointerReleased,PointerExited事件,我使用下面的代码来绘制签名。

     private void InkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
            {
                try
                {
                    if (e.Pointer.PointerId == _penID)
                    {
                        PointerPoint pt = e.GetCurrentPoint(sender as Canvas);

                        // Render a red line on the canvas as the pointer moves. 
                        // Distance() is an application-defined function that tests
                        // whether the pointer has moved far enough to justify 
                        // drawing a new line.
                        currentContactPt = pt.Position;
                        x1 = _previousContactPt.X;
                        y1 = _previousContactPt.Y;
                        x2 = currentContactPt.X;
                        y2 = currentContactPt.Y;

                        if (Distance(x1, y1, x2, y2) > 2.0)
                        {
                            Line line = new Line()
                            {
                                X1 = x1,
                                Y1 = y1,
                                X2 = x2,
                                Y2 = y2,
                                StrokeThickness = 4.0,
                                Stroke = new SolidColorBrush(Colors.Blue)
                            };

                            _previousContactPt = currentContactPt;

                            // Draw the line on the canvas by adding the Line object as
                            // a child of the Canvas object.
                            ((Canvas)(sender)).Children.Add(line);

                            // Pass the pointer information to the InkManager.
                            _inkManager.ProcessPointerUpdate(pt);
                        }
                    }

                    else if (e.Pointer.PointerId == _touchID)
                    {
                        // Process touch input
                    }
                }
                catch (Exception ex)
                {
                    LogUtility.Log("InkCanvas_PointerMoved Exception: " + ex.Message + "\nInnerException: " + ex.InnerException);
                }

            }

public void InkCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            try
            {
                // Get information about the pointer location.
                PointerPoint pt = e.GetCurrentPoint(((Canvas)(sender)));
                _previousContactPt = pt.Position;

                // Accept input only from a pen or mouse with the left button pressed. 
                PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
                if (pointerDevType == PointerDeviceType.Pen ||
                        pointerDevType == PointerDeviceType.Mouse &&
                        pt.Properties.IsLeftButtonPressed)
                {
                    // Pass the pointer information to the InkManager.
                    _inkManager.ProcessPointerDown(pt);
                    _penID = pt.PointerId;

                    e.Handled = true;
                }

                else if (pointerDevType == PointerDeviceType.Touch)
                {
                    // Process touch input
                }
            }
            catch (Exception ex)
            {
                LogUtility.Log("InkCanvas_PointerPressed Exception: " + ex.Message + "\nInnerException: " + ex.InnerException);
            }
        }
public void InkCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            try
            {
                if (e.Pointer.PointerId == _penID)
                {
                    Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(sender as Canvas);

                    // Pass the pointer information to the InkManager. 
                    _inkManager.ProcessPointerUp(pt);
                }

                else if (e.Pointer.PointerId == _touchID)
                {
                    // Process touch input
                }

                _touchID = 0;
                _penID = 0;

                // Call an application-defined function to render the ink strokes.


                e.Handled = true;
            }
            catch (Exception ex)
            {
                LogUtility.Log("InkCanvas_PointerReleased Exception: " + ex.Message + "\nInnerException: " + ex.InnerException);
            }
        }

            private double Distance(double x1, double y1, double x2, double y2)
            {
                double d = 0;
                d = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
                return d;
            }

它适用于鼠标,但不适用于手写笔。我无法找到任何其他方法来做到这一点。 你能告诉我修理手写笔。

2 个答案:

答案 0 :(得分:0)

我相信InkCanvas是执行这些操作的最佳控件,请查看here了解更多详情。

答案 1 :(得分:0)

我发现it很有帮助。它解决了手写笔触摸画问题。