WPF 多点触摸跟踪触摸点

标签 wpf drawing multi-touch

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

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

您能就此提出建议吗?

谢谢。

最佳答案

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

关于WPF 多点触摸跟踪触摸点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6651913/

相关文章:

c# - 使用 for 循环 xna 4.0 绘制多个 Sprite

c - 多线程、gtk3 和 cairo : drawing in a cairo surface

android - 获取支持的触摸点数量

android - 是否所有智能手机浏览器(Android 和 iOS)都支持多点触控/手势?

c# - 要使用 AD B2C 验证 .NET Core 3.0 WPF 桌面客户端,如何使用默认操作系统浏览器?

c# - 在动画完成时删除 FrameworkElements

c# - 如何以编程方式显示 FocusVisualStyle?

javascript - 如何生成持续多彩艺术柏林流场

javascript - 什么是跨平台免费/开源框架,可使用 HTML/CSS/JS 创建基于 Touch 的 Web 应用程序/网站?

c# - 需要捕捉按下 ListBoxItem 的事件