[Windows Phone]Use InkPresenter to draw something(not game)

First, Add xaml elements page:

<Grid x:Name="ContentPanel" Margin="0,0,0,1" Background="White"
      Width="450" Height="400">
     <InkPresenter Background="Transparent" Margin="10,10,10,10" 
                   Name="MyIP"
                   MouseLeftButtonDown="MyIP_MouseLeftButtonDown" 
                   MouseMove="MyIP_MouseMove" 
                   LostMouseCapture="MyIP_LostMouseCapture" />
 </Grid>

2. Write behind code:

Stroke newStroke;
 
private void MyIP_LostMouseCapture(object sender, MouseEventArgs e)
{
    newStroke = null;
}
private void MyIP_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    MyIP.CaptureMouse();
    StylusPointCollection myStylusPointCollection = new StylusPointCollection();
    myStylusPointCollection.Add(e.StylusDevice.GetStylusPoints(MyIP));
    newStroke = new Stroke(myStylusPointCollection);
    newStroke.DrawingAttributes.Color = Colors.Blue;
    MyIP.Strokes.Add(newStroke);
}
private void MyIP_MouseMove(object sender, MouseEventArgs e)
{
    if (newStroke != null)
    {
        newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(MyIP));
    }
}

And, this is all.

blog comments powered by Disqus