Canvas c=new Canvas();
 c.Width=1200;
 c.Height=700;
 c.PointerMoved+=Moved;
 private void Moved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
 {
        PointerPoint CurrentPosition = e.GetCurrentPoint(sender as Canvas);
        Point PointerPosition = CurrentPosition.Position;

 }

Saya tidak tahu bagaimana menghitung jarak yang dipindahkan selama acara ini. PointerPosition memberikan arus (x dan y). Saya perlu menghitung jarak yang dipindahkan selama acara ini.

0
PremKumar Shanmugam 9 Juli 2020, 09:12

1 menjawab

Jawaban Terbaik

Peristiwa PointerMoved adalah peristiwa yang dipicu secara terus-menerus. Jika Anda ingin menghitung jarak pergerakan penunjuk, Anda harus memiliki "titik awal".

Point StartPoint = new Point(0, 0);

...
c.PointerEntered += Entered;

private void Entered(object sender, PointerRoutedEventArgs e)
{
    PointerPoint CurrentPosition = e.GetCurrentPoint(sender as Canvas);
    StartPoint = CurrentPosition.Position;
}

private void Moved(object sender, PointerRoutedEventArgs e)
{
    PointerPoint CurrentPosition = e.GetCurrentPoint(sender as Canvas);
    Point PointerPosition = CurrentPosition.Position;
    double xDistance = Math.Abs(PointerPosition.X - StartPoint.X);
    double yDistance = Math.Abs(PointerPosition.Y - StartPoint.Y);
    double Distance = Math.Sqrt(xDistance * xDistance + yDistance * yDistance);
}

Perbarui

Jika kita perlu menghitung perubahan posisi pointer antara dua kejadian PointerMoved, kita perlu memasukkan variabel untuk merekam posisi pointer terakhir.

Point PreviousPoint;
private void Moved(object sender, PointerRoutedEventArgs e)
{
    PointerPoint CurrentPosition = e.GetCurrentPoint(sender as Canvas);
    Point PointerPosition = CurrentPosition.Position;
    if (PreviousPoint != null)
    {
        double xDistance = Math.Abs(PointerPosition.X - PreviousPoint.X);
        double yDistance = Math.Abs(PointerPosition.Y - PreviousPoint.Y);
        double Distance = Math.Sqrt(xDistance * xDistance + yDistance * yDistance);
        // TODO use Distance
    }
    PreviousPoint = PointerPosition;
}

Terima kasih.

1
Richard Zhang - MSFT 9 Juli 2020, 08:32