[Windows Phone]A implement of Multi-Touch drawing in Windows Phone7

InkPresenter only allow on point drawing, To achieve multi-touch draw, in other words, draw with more than one finger. we'd implement the Touch.FrameReported event. here is what I have done:

public MainPage()
{
    InitializeComponent();
    Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
}
 
double[] preXArray = new double[12];
double[] preYArray = new double[12];
 
void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
    int pointsNumber=e.GetTouchPoints(drawCanvas).Count;
    TouchPointCollection pointCollection=e.GetTouchPoints(drawCanvas);
 
    for(int i=0;i<pointsNumber;i++)
    {
        if (pointCollection[i].Action == TouchAction.Down)
        {
             preXArray[i] = pointCollection[i].Position.X;
             preYArray[i] = pointCollection[i].Position.Y;
        }
        if (pointCollection[i].Action == TouchAction.Move)
        {
             Line line = new Line();
 
             line.X1 = preXArray[i];
             line.Y1 = preYArray[i];
             line.X2 = pointCollection[i].Position.X;
             line.Y2 = pointCollection[i].Position.Y;
 
             line.Stroke = new SolidColorBrush(Colors.Black);
             line.Fill = new SolidColorBrush(Colors.Black);
             drawCanvas.Children.Add(line);
 
             preXArray[i] = pointCollection[i].Position.X;
             preYArray[i] = pointCollection[i].Position.Y;
        }
    }
}

Every touch action will reported by FremeReported event. such that our code can be excuted. the basic idea is that track every points's previous position and draw a line between the previous point and current point. to make sure Points will not be interfered with each other, I use preXArray and preYArray to store the previous positions. you may know that Windows Phone 7.5 support 4 points multi-touch. but I declared a 10 lenght array 1.  for further extension. 2. if we delcare a 4 length array, put out your hand and draw with 5 points the program may crash!.that is why I use a 10 lengthh position array.

blog comments powered by Disqus