首页 > 解决方案 > 连接触摸屏时鼠标滞后,没有时几乎没有滞后

问题描述

我有一个触摸屏(LG 23ET83V,如果重要的话),我想为我 3 岁的女儿编写一个非常基本的绘画应用程序:没有广泛的工具调色板和她不关心的豪华工具(我不喜欢 Tuxpaint对于这个问题)。只是简单的绘画。我的动力就是这么多。

但是使用下面的代码,我观察到以下奇怪的行为:

  1. 在目标机器(Windows 10 Pro,2014 年左右的 6 核 AMD)上,用我的手指绘画时,我的手指位置后面的绘画有一个小的延迟(~0.5 秒?)。只有在我的手指休息之后,绘图位置才能赶上我的手指位置。

  2. 当我用鼠标绘图时,延迟会变得令人印象深刻(~ 2-4 秒!)对于更长的动作。如果我停止鼠标移动,绘图位置最终会赶上。看起来鼠标在项目符号 1 中移动手指时产生的 MOUSE_MOVE 事件的发生率并不比触摸控制器更高。

  3. 当我断开屏幕触摸控制器的USB线时,鼠标延迟几乎消失了。好吧,它仍然很明显但可以接受(比如~0.25s)

  4. 当我在开发机器(大约 2008 年左右的英特尔 Q6600 四核)上使用鼠标绘图时,我没有看到任何鼠标延迟。

如果它与硬件或驱动程序相关,我可能不走运。但它在我看来好像 Windows 试图通过平均几个连续的鼠标位置来平滑触摸移动。如果这是真的,我可以控制平均吗?关于问题原因的任何其他想法?

PS:当我在出现上述问题的机器上使用 MS Paint 时,我看不到任何延迟。所以问题一定是由与 C# 相关或与我使用 API 的特定方式相关的东西引起的。

public partial class MainWindow : Window
{
    WriteableBitmap bitmap;

    public MainWindow()
    {
        InitializeComponent();

        // create bitmap the size of whole screen
        int bitmapWidth = Convert.ToInt32(SystemParameters.PrimaryScreenWidth);
        int bitmapHeight = Convert.ToInt32(SystemParameters.PrimaryScreenHeight);

        bitmap = new WriteableBitmap(bitmapWidth, bitmapHeight, 96, 96, PixelFormats.Bgra32, null);

        canvasImage.Source = bitmap;
    }

    void plot(int x, int y, uint value)
    {
        // just draw one pixel for barebones testing
        uint[] valueArray = new uint[1];
        valueArray[0] = value;
        bitmap.WritePixels(new Int32Rect(0, 0, 1, 1), valueArray, 4, x, y);
    }

    Point previousPosition;

    void imageMouseMove(object sender, MouseEventArgs e)
    {
        Point currentPosition = e.GetPosition(canvasImage);

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            int x2 = (int)Math.Floor(currentPosition.X * canvasImage.Source.Width / canvasImage.ActualWidth);
            int y2 = (int)Math.Floor(currentPosition.Y * canvasImage.Source.Height / canvasImage.ActualHeight);
            int x1 = (int)Math.Floor(previousPosition.X * canvasImage.Source.Width / canvasImage.ActualWidth);
            int y1 = (int)Math.Floor(previousPosition.Y * canvasImage.Source.Height / canvasImage.ActualHeight);

            plot(x2,y2, 0xFF800000);
        }

        previousPosition = currentPosition;
    }

    void button1_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }
}

标记:

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="Paintuition.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Paintuition"
    ResizeMode="NoResize"
    WindowState="Maximized"
    WindowStyle="None">
    <Grid>
        <Image
            x:Name="canvasImage"
            MouseMove="imageMouseMove" />
        <Button
            Content="X"
            x:Name="button1"
            Click="button1_Click"
            Background="Red"
            BorderBrush="#FFFFBFBF"
            FontFamily="Arial"
            Foreground="White"
            FontWeight="ExtraBold"
            FontSize="54"
            Width="66"
            Height="65"
            Grid.Column="0"
            Grid.Row="0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Margin="0,42,34,0" />
    </Grid>
</Window>

标签: c#wpfwindows-10touch

解决方案


太糟糕了,没有人知道这个问题的原因。虽然我没有真正的解决方案,但我使用了以下解决方法(为了任何想要和我一样的人的利益,即为小孩子画画):

我现在求助于 Gimp,它允许或多或少对儿童友好的自定义:

  1. 可以使主工具箱隐藏除画笔和橡皮擦之外的所有工具
  2. 设置为色轮的“颜色”可停靠工具栏非常适合儿童使用,否则使用相同的工具栏可以访问自定义创建的带有一些彩虹色的调色板,更加儿童友好;将浮动工具栏调整为合理的大小
  3. 我唯一使用的其他工具栏是画笔工具栏,我删除了除了最基本的圆形高斯画笔之外的所有画笔(奇怪的是,其他画笔只能在文件系统中删除)
  4. 以全屏模式启动应用程序

不幸的是,即使在这个简单的设置中,仍然有几个手柄/菜单/手势会给摇晃的儿童手指带来意想不到的效果,例如取消停靠/移动工具栏、交换前景色/背景色等。但它至少与小孩子的绘图程序,它可以得到。


推荐阅读