wpf 多点触控

6/6/2016 2:54:17 PM

添加触控标签:

<Canvas x:Name="canvas1" Height="1080" Width="1920" Background="Black" />

事件初始化:

触控事件:按下,移动,松开

public partial class MainWindow : Window
    {
        // Variables to track the first two touch points  
        // and the ID of the first touch point. 
        Point pt1;
        Point pt2;
        Int32 firstId;

        public MainWindow()
        {
            firstId = -1;
            pt2 = new Point();
            InitializeComponent();
            TouchDown += MainWindow_TouchDown;
            TouchUp += MainWindow_TouchUp;
            TouchMove += MainWindow_TouchMove;
        }

        private void MainWindow_TouchDown(object sender, TouchEventArgs e)
        {
            if (canvas1 != null)
            {
                canvas1.Children.Clear();
                e.TouchDevice.Capture(canvas1);

                // Record the ID of the first touch point if it hasn't been recorded. 
                if (firstId == -1)
                    firstId = e.TouchDevice.Id;
            }
        }

        private void MainWindow_TouchMove(object sender, TouchEventArgs e)
        {
            if (canvas1 != null)
            {
                var tp = e.GetTouchPoint(canvas1);

                if (e.TouchDevice.Id == firstId)
                {
                    pt1.X = tp.Position.X;
                    pt1.Y = tp.Position.Y;
                }
                else if (e.TouchDevice.Id != firstId)
                {
                    pt2.X = tp.Position.X;
                    pt2.Y = tp.Position.Y;

                    canvas1.Children.Add(new Line
                    {
                        Stroke = new RadialGradientBrush(Colors.White, Colors.Black),
                        X1 = pt1.X,
                        X2 = pt2.X,
                        Y1 = pt1.Y,
                        Y2 = pt2.Y,
                        StrokeThickness = 2
                    });
                }
            }
        }

        void MainWindow_TouchUp(object sender, TouchEventArgs e)
        {
            if (canvas1 != null && e.TouchDevice.Captured == canvas1)
                canvas1.ReleaseTouchCapture(e.TouchDevice);
        }
    }