这个示例项目包含用C#/XAML编写的Win 8 Store应用程序屏幕绘制代码:
http://code.msdn.microsoft.com/windowsapps/Drawing-on-a-Canvas-with-33510ae6
这是相关的 C# 文件:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Input.Inking;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
namespace DrawingOnCanvasWithInkPen
{
public sealed partial class MainPage : Page
{
InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager();
private uint _penID;
private uint _touchID;
private Point _previousContactPt;
private Point currentContactPt;
private double x1;
private double y1;
private double x2;
private double y2;
public MainPage()
{
this.InitializeComponent();
MyCanvas.PointerPressed += new PointerEventHandler(MyCanvas_PointerPressed);
MyCanvas.PointerMoved += new PointerEventHandler(MyCanvas_PointerMoved);
MyCanvas.PointerReleased += new PointerEventHandler(MyCanvas_PointerReleased);
MyCanvas.PointerExited += new PointerEventHandler(MyCanvas_PointerReleased);
}
#region PointerEvents
private void MyCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerId == _penID)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(MyCanvas);
_inkKhaled.ProcessPointerUp(pt);
}
else if (e.Pointer.PointerId == _touchID)
{
}
_touchID = 0;
_penID = 0;
e.Handled = true;
}
private void MyCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerId == _penID)
{
PointerPoint pt = e.GetCurrentPoint(MyCanvas);
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.Green)
};
_previousContactPt = currentContactPt;
MyCanvas.Children.Add(line);
_inkKhaled.ProcessPointerUpdate(pt);
}
}
else if (e.Pointer.PointerId == _touchID)
{
}
}
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;
}
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
PointerPoint pt = e.GetCurrentPoint(MyCanvas);
_previousContactPt = pt.Position;
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
_inkKhaled.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
}
}
#endregion
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
}
和 xaml 文件:
<Page
x:Class="DrawingOnCanvasWithInkPen.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DrawingOnCanvasWithInkPen"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Canvas Name="MyCanvas" Background="White" HorizontalAlignment="Left" Height="513" Margin="83,102,0,0" VerticalAlignment="Top" Width="1056"/>
</Grid>
在当前状态下,它只处理笔或鼠标输入 - 但我也进行了轻微修改,使其适用于触摸输入。