我正在使用C#中的Windows 10 Universal Application。我有一个画布控件,用户可以在其上绘制签名。我附加了使用canvas控件附加的PointerPressed,PointerMoved,PointerReleased,PointerExited事件,我使用下面的代码来绘制签名。
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;
}
它适用于鼠标,但不适用于手写笔。我无法找到任何其他方法来做到这一点。 你能告诉我修理手写笔。