【问题标题】:WPF Multi-touch tracking touch pointsWPF 多点触控跟踪触摸点
【发布时间】:2011-07-11 14:44:25
【问题描述】:

我正在尝试做一个简单的应用程序,当用户触摸屏幕时,应用程序会创建简单的点、椭圆或 2d 对象,并且当用户移动手指时它应该跟随,而且当有 scond touch 同时还必须创建新对象,并针对用户的移动执行相同的操作。每当用户手指抬起时,对象就会被删除。

为此,我正在尝试从此链接http://www.cookingwithxaml.com/recipes/wpf4/wpf4touch.zip 更改触摸绘图代码,但我不知道应该更改哪种方法?

您能就此提出建议吗?

谢谢。

【问题讨论】:

    标签: wpf drawing multi-touch


    【解决方案1】:

    这是一些示例 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);
        }
      }
    }
    

    【讨论】:

    • 太好了,谢谢。尽管调用 CaptureTouch() 会阻止报告操作。不过,似乎没有必要捕捉触摸。
    猜你喜欢
    • 2014-01-12
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多