我正在使用C#中的Windows 10通用應用程序。我有一個用戶可以繪製簽名的畫布控件。我附加了PointerPressed,PointerMoved,PointerReleased,附有畫布控件的PointerExited事件,我使用下面的代碼來繪製簽名。使用手寫筆繪製畫布 - Windows 10通用應用程序C#
private void InkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
try
{
if (e.Pointer.PointerId == _penID)
{
PointerPoint pt = e.GetCurrentPoint(sender as Canvas);
// Render a red line on the canvas as the pointer moves.
// Distance() is an application-defined function that tests
// whether the pointer has moved far enough to justify
// drawing a new line.
currentContactPt = pt.Position;
x1 = _previousContactPt.X;
y1 = _previousContactPt.Y;
x2 = currentContactPt.X;
y2 = currentContactPt.Y;
if (Distance(x1, y1, x2, y2) > 2.0)
{
Line line = new Line()
{
X1 = x1,
Y1 = y1,
X2 = x2,
Y2 = y2,
StrokeThickness = 4.0,
Stroke = new SolidColorBrush(Colors.Blue)
};
_previousContactPt = currentContactPt;
// Draw the line on the canvas by adding the Line object as
// a child of the Canvas object.
((Canvas)(sender)).Children.Add(line);
// Pass the pointer information to the InkManager.
_inkManager.ProcessPointerUpdate(pt);
}
}
else if (e.Pointer.PointerId == _touchID)
{
// Process touch input
}
}
catch (Exception ex)
{
LogUtility.Log("InkCanvas_PointerMoved Exception: " + ex.Message + "\nInnerException: " + ex.InnerException);
}
}
public void InkCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
try
{
// Get information about the pointer location.
PointerPoint pt = e.GetCurrentPoint(((Canvas)(sender)));
_previousContactPt = pt.Position;
// Accept input only from a pen or mouse with the left button pressed.
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
// Pass the pointer information to the InkManager.
_inkManager.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
// Process touch input
}
}
catch (Exception ex)
{
LogUtility.Log("InkCanvas_PointerPressed Exception: " + ex.Message + "\nInnerException: " + ex.InnerException);
}
}
public void InkCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
try
{
if (e.Pointer.PointerId == _penID)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(sender as Canvas);
// Pass the pointer information to the InkManager.
_inkManager.ProcessPointerUp(pt);
}
else if (e.Pointer.PointerId == _touchID)
{
// Process touch input
}
_touchID = 0;
_penID = 0;
// Call an application-defined function to render the ink strokes.
e.Handled = true;
}
catch (Exception ex)
{
LogUtility.Log("InkCanvas_PointerReleased Exception: " + ex.Message + "\nInnerException: " + ex.InnerException);
}
}
private double Distance(double x1, double y1, double x2, double y2)
{
double d = 0;
d = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
return d;
}
它適用於鼠標,但不適用於手寫筆。我無法找到任何其他方式來做到這一點。 你能告訴我修理手寫筆嗎?
您是否試過[InkCanvas class](https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.inkcanvas.aspx)?它支持筆和手寫筆的交互。有關更多信息,請參閱[UWP應用程序中的筆和手寫筆交互](https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/pen-and-stylus-interactions)。 –