WPF多点触控跟踪触摸点

时间:2011-07-11 14:44:26

标签: wpf drawing multi-touch

我正在尝试做一个简单的应用程序,当用户触摸屏幕时,app会创建简单的点,椭圆或sth 2d对象,当用户移动他的手指时,它应该跟随,但是当有一个秒时同时触摸新对象也必须创建并对用户移动做同样的事情。每当用户指责时,对象将被删除。

要做到这一点,我正在尝试更改此链接http://www.cookingwithxaml.com/recipes/wpf4/wpf4touch.zip中的触摸绘制代码,但我无法确定应该更改哪种方法?

你可以提出建议吗?

感谢。

1 个答案:

答案 0 :(得分:2)

以下是一些示例xaml / C#代码,可以执行我认为您想要的内容:

MainWindow.xaml:

<Window x:Class="MultitouchExperiments.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas
            x:Name="TouchCanvas"
            TouchDown="TouchCanvas_TouchDown" TouchUp="TouchCanvas_TouchUp"
            TouchMove="TouchCanvas_TouchMove" TouchLeave="TouchCanvas_TouchLeave"
            TouchEnter="TouchCanvas_TouchEnter"
            VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
            Background="Black"
            IsManipulationEnabled="True" />
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace MultitouchExperiments
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    Dictionary<TouchDevice, Ellipse> _Followers = new Dictionary<TouchDevice, Ellipse>();

    public MainWindow()
    {
      InitializeComponent();
    }

    private void TouchCanvas_TouchDown(object sender, TouchEventArgs e)
    {
      TouchCanvas.CaptureTouch(e.TouchDevice);

      Ellipse follower = new Ellipse();
      follower.Width = follower.Height = 50;
      follower.Fill = Brushes.White;
      follower.Stroke = Brushes.White;

      TouchPoint point = e.GetTouchPoint(TouchCanvas);

      follower.RenderTransform = new TranslateTransform(point.Position.X, point.Position.Y);

      _Followers[e.TouchDevice] = follower;

      TouchCanvas.Children.Add(follower);
    }

    private void TouchCanvas_TouchUp(object sender, TouchEventArgs e)
    {
      TouchCanvas.ReleaseTouchCapture(e.TouchDevice);

      TouchCanvas.Children.Remove(_Followers[e.TouchDevice]);
      _Followers.Remove(e.TouchDevice);
    }

    private void TouchCanvas_TouchMove(object sender, TouchEventArgs e)
    {
      if (e.TouchDevice.Captured == TouchCanvas)
      {
        Ellipse follower = _Followers[e.TouchDevice];
        TranslateTransform transform = follower.RenderTransform as TranslateTransform;

        TouchPoint point = e.GetTouchPoint(TouchCanvas);

        transform.X = point.Position.X;
        transform.Y = point.Position.Y;
      }
    }

    private void TouchCanvas_TouchLeave(object sender, TouchEventArgs e)
    {
      //Debug.WriteLine("leave " + e.TouchDevice.Id);
    }

    private void TouchCanvas_TouchEnter(object sender, TouchEventArgs e)
    {
      //Debug.WriteLine("enter " + e.TouchDevice.Id);
    }
  }
}