2016-05-05 20 views
0

私はC#でWindows 10 Universal Applicationを開発しています。私はユーザーが署名を描くことができるキャンバスコントロールを持っています。 PointerPressed、PointerMoved、PointerReleased、PointerExitedイベントをキャンバスコントロールに添付して添付しました。私は以下のコードを使って署名を描きます。スタイラスで描くキャンバス - Windows 10 Universal Application C#

 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; 
      } 

これはマウスでうまく動作しますが、スタイラスでは機能しません。私はそれを行うための他の方法を見つけることができません。 スタイラスの修正を教えてください。

+0

[InkCanvasクラス](https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.inkcanvas.aspx)で試したことがありますか?ペンとスタイラスの相互作用をサポートしています。詳細については、[UWPアプリケーションのペンとスタイラスのやりとり](https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/pen-and-stylus-interactions)を参照してください。 –

答えて

0

私はInkCanvasがこれらの操作を行うための最良のコントロールだと考えています。詳細はhereをご覧ください。

0

itが役に立ちました。これは、スタイラスタッチ描画の問題を解決しました。

関連する問題