2011-07-11 114 views
4

我正試圖做一個簡單的應用程序,當用戶觸摸屏幕時,應用程序創建簡單的點,橢圓或2d對象,並且當用戶移動他的手指時,它應該遵循,而且當有第二次觸摸的同時,還必須創建新的對象並對用戶移動做同樣的事情。無論何時用戶fingerup,對象將被刪除。WPF多點觸摸跟蹤點

要做到這一點,我試圖改變從這個鏈接http://www.cookingwithxaml.com/recipes/wpf4/wpf4touch.zip touchdrawing代碼,但我不知道我需要改變哪種方法?

您可以給點建議嗎?

謝謝。

回答

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); 
    } 
    } 
} 
+0

優秀的,謝謝。雖然調用CaptureTouch()將阻止操作被報告。不過,似乎並不需要捕捉觸摸。 – ulatekh