我在 InkCanvas (.Net Framework) 上的 Cancel touch event 中問了一個問題,但沒有得到答案,所以我嘗試將 to 設定InkCanvas為HitTestVisibletofalse并StylusPoints通過驅動程式的 API獲取。這些點現在手動放入Strokes ,然后放入 bound StrokeCollection。這樣,我只能從觸控筆而不是從觸摸或滑鼠獲得積分(我知道 UWPInkCanvas可以默認執行此操作,但遺憾的是 WPF 太舊了,并且沒有得到 Microsoft 的正確支持)。
但是我對這種方法的速度有疑問。我Stroke從每個點(以及之前的點)中提取出來并將它們放入StrokeCollection. 這會導致“緩慢” Stroke。如果我寫了一些東西,當我已經用筆完成它時,它只會顯示大約 3/4,然后在半秒后完全顯示。你有什么想法可以讓這個更快,使它看起來像一個正常的使用InkCanvas?或者,可以回答之前的問題;)
這是我的代碼:
private void TabLib_DataReceived(object sender, EventArgs e)
{
float pressureFactor = e.pressure / 1024f;
StylusPoint newPoint = new StylusPoint()
{
PressureFactor = pressureFactor,
X = e.xPos,
Y = e.yPos
};
if (pressureFactor == 0) // that means the pen has left or entered the tablet
{
_oldPressurePoint = newPoint;
return;
}
StylusPointCollection pointList = new StylusPointCollection();
pointList.Add(_oldPressurePoint);
pointList.Add(newPoint);
Stroke stroke = new Stroke(pointList, _penDrawingAttributes);
// used a dispatcher begininvoke because of DrawnStrokes are bound to the UI thread.
_tabletWindow.Dispatcher.BeginInvoke(new Action<Stroke>((newStroke) =>
{
DrawnStrokes.Add(newStroke); // DrawnStrokes is the bound StrokeCollection
}), stroke);
_oldPressurePoint = newPoint;
}
我也想過創建一筆并只添加點,但這似乎行不通。如果你已經添加了一個筆畫,你不能在這個筆畫上添加點,對嗎?
uj5u.com熱心網友回復:
您應該只在必要時創建一個新的 Stroke,并將點添加到當前 Stroke 的 StylusPoints 集合中。
這是一個作業順利的示例,它根據滑鼠輸入在 InkCanvas 上的 Canvas 疊加層上創建筆畫資料。
<Grid>
<InkCanvas x:Name="inkCanvas" IsHitTestVisible="False"/>
<Canvas Background="Transparent"
MouseLeftButtonDown="CanvasMouseLeftButtonDown"
MouseLeftButtonUp="CanvasMouseLeftButtonUp"
MouseMove="CanvasMouseMove"/>
</Grid>
后面有這個代碼:
private Stroke stroke;
private void CanvasMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var pos = e.GetPosition(inkCanvas);
var points = new StylusPointCollection
{
new StylusPoint(pos.X, pos.Y)
};
stroke = new Stroke(points, inkCanvas.DefaultDrawingAttributes);
inkCanvas.Strokes.Add(stroke);
}
private void CanvasMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
stroke = null;
}
private void CanvasMouseMove(object sender, MouseEventArgs e)
{
if (stroke != null)
{
var pos = e.GetPosition(inkCanvas);
stroke.StylusPoints.Add(new StylusPoint(pos.X, pos.Y));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/338988.html
上一篇:Nodejs并行請求越來越慢
