簡體   English   中英

使用手寫筆繪制畫布-Windows 10 Universal Application C#

[英]Canvas drawing with stylus - Windows 10 Universal Application C#

我正在使用C#開發Windows 10通用應用程序。 我有一個畫布控件,用戶可以在其上繪制簽名。 我已經附加了畫布控件附帶的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;
            }

它適用於鼠標,但不適用於手寫筆。 我找不到其他方法來做。 你能告訴我修理手寫筆嗎?

我相信InkCanvas是執行這些操作的最佳控件,請在此處查看更多信息。

我發現很有幫助。 它解決了手寫筆觸摸繪制問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM