WPF Could Not Get Contact Position From MouseDown Event

I have a WPF project written before touch support was added to .NET (v 4.0), so only mouse events were handled. I encounter this problem when testing a project on a touch screen with my fingers.

The problem is that the position (X, Y) is correctly extracted the first time you touch, but the values ​​(X, Y) remain unchanged the next time you touch, no matter where I touch, and even if I touch the image, the MouseDown event is fired. making it weirder.

It can be played using .NET 3.0 / 3.5 / 4.0, tested on Win7 / Win8, both of which are 64 bits. And it seems that this MouseDown event is wrong, MouseUp works fine.

Update:

This is a mistake with a long history, and MS has not yet fixed it (even in 4.5), so you need to update the code if you encounter the same symptom - get the touch position from the Touch event, not the mouse event. Fortunately, this error is not subtle, so it takes some time for it to be found and fixed.

Code to reproduce the problem:

XAML:

<Window x:Class="WpfApplication1.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>
        <Image Height="60" Width="80" x:Name="Image" MouseDown="Image_MouseDown" 
                Source="/WpfApplication1;component/Images/Desert.jpg" />
    </Grid> 
</Window>

Code behind:

using System;
using System.Collections.Generic;
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;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(Image);
            MessageBox.Show(p.X.ToString() + " " + p.Y.ToString());
        }
    }
}
+4
source share
2 answers

WPF, Touch , . , , Mouse , (, ).

MouseDown , , , , StylusDevice:

if (e.StylusDevice != null)
    point = e.StylusDevice.GetPosition(sender as Image);

TouchDown , :

<Image TouchDown="UIElement_OnTouchDown"/>

 private void UIElement_OnTouchDown(object sender, TouchEventArgs e)
 {
      var touchPoint = e.GetTouchPoint(sender as Image);
      // more processing using touchPoint.Position
 }
+1

.NET 4.5.0, . 4.5.1. , - MS , .

0

Source: https://habr.com/ru/post/1527251/


All Articles