gpt4 book ai didi

c# - 在 Microsoft Surface Window 上模拟触摸输入

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:01 25 4
gpt4 key购买 nike

我正在尝试使用 Anoto-Pen 作为 TouchDeviceSurfaceInkCanvas .
这支笔使用打印在一张纸上的坐标系来推导其位置,然后将此位置数据发送到我的应用程序。我尝试将其转换为 TouchInput通过子类化 TouchDevice并使用 TouchDevice.ReportDown(); 将发送位置数据和事件转换为 .NET Touch-Events , TouchDevice.ReportMove()等搬家ScatterViewItems到目前为止,周围和处理按钮“点击”都有效。

现在的问题是,当我尝试在 InkCanvas 上写入时只绘制点。在观察被触发的事件后,似乎 InkCanvas没有收到 OnTouchMove事件。

我为 TouchDown 注册了事件处理程序, TouchMoveTouchUp在我的 SurfaceInkCanvas . TouchDown永远不会被触发。 TouchMoveTouchUp只有当我在 SurfaceInkCanvas 之外开始时然后移动到它里面的一个点。

这是我的 TouchDevice 的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Windows;
using PaperDisplay.PenInput;
using System.Windows.Media;
using System.Windows.Threading;

namespace TouchApp
{
public class PenTouchDevice : TouchDevice
{
public Point Position { get; set; }
public Person Person { get; set; }

public PenTouchDevice(Person person)
: base(person.GetHashCode())
{
Person = person;
}

public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo)
{
return new TouchPointCollection();
}

public override TouchPoint GetTouchPoint(System.Windows.IInputElement relativeTo)
{
Point point = Position;
if (relativeTo != null)
{
point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(Position);
}
return new TouchPoint(this, point, new Rect(point, new Size(2.0, 2.0)), TouchAction.Move);
}

public void PenDown(PenPointInputArgs args, Dispatcher dispatcher)
{
dispatcher.BeginInvoke((Action)(() =>
{
SetActiveSource(PresentationSource.FromVisual(Person.Window));
Position = GetPosition(args);
if (!IsActive)
{
Activate();
}
ReportDown();
}));
}

public void PenUp(PenPointInputArgs args, Dispatcher dispatcher)
{
dispatcher.BeginInvoke((Action)(() =>
{
Position = GetPosition(args);
if (IsActive)
{
ReportUp();
Deactivate();
}
}));

}

public void PenMove(PenPointInputArgs args, Dispatcher dispatcher)
{
dispatcher.BeginInvoke((Action)(() =>
{
if (IsActive)
{
Position = GetPosition(args);
ReportMove();
}
}));
}

public Point GetPosition(PenPointInputArgs args)
{
double adaptedX = args.Y - 0.01;
double adaptedY = (1 - args.X) - 0.005;
return new Point(adaptedX * Person.Window.ActualWidth, adaptedY * Person.Window.ActualHeight);
}
}
}

我的 App.xaml.cs 中有以下代码每次发生笔输入时都会调用它:

public void HandleEvent(object sender, EventArgs args)
{
if (typeof(PointInputArgs).IsAssignableFrom(args.GetType()))
{
PenPointInputArgs pointArgs = (PenPointInputArgs)args;
switch (pointArgs.EventType)
{
case InputEvent.Down: touchDevice1.PenDown(pointArgs, this.Dispatcher); break;
case InputEvent.Up: touchDevice1.PenUp(pointArgs, this.Dispatcher); break;
case InputEvent.Move:
case InputEvent.MoveDown: touchDevice1.PenMove(pointArgs, this.Dispatcher); break;
}

}
}

提前谢谢你。

最佳答案

GetIntermediateTouchPoints 不能返回一个空数组,它应该至少包含一个点

public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo)
{
TouchPointCollection refCollection = new TouchPointCollection();
refCollection.Add(GetTouchPoint(relativeTo));
return refCollection;
}

关于c# - 在 Microsoft Surface Window 上模拟触摸输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com