连接触摸屏时鼠标滞后,如果没有鼠标滞后

时间:2018-11-17 07:46:05

标签: c# wpf windows-10 touch

我有一个触摸屏(如果需要的话,它是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左右的Intel 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>

1 个答案:

答案 0 :(得分:0)

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

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

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

不幸的是,即使在这种简单的设置中,仍然有多个手柄/菜单/手势给摇晃的孩子的手指带来意想不到的效果,例如,取消停靠/移动工具栏,交换前景色/背景色等。但是至少尽可能接近小孩的绘画程序。