Capturing User Input

,

The touch related events, MouseLeftButtonDown, MouseMove, and MouseLeftButtonUp, are used to collect and display strokes in the InkPresenter while the user interacts with the control. The following excerpt shows an InkPresenter with subscriptions to the three mouse events:

<Grid x:Name="ContentPanel" Grid.Row="1">
    <InkPresenter Strokes="{Binding Strokes}"
        VerticalAlignment="Stretch"
        Background="Black"
        MouseMove="InkPresenter_MouseMove"
        MouseLeftButtonDown="InkPresenter_MouseLeftButtonDown"
        MouseLeftButtonUp="InkPresenter_MouseLeftButtonUp" />
</Grid>


Note

The Background property of the InkPresenter must be assigned to register Mouse events.


Listing 7.1 shows how the mouse event handlers are used to add Stroke objects to the InkPresenter element’s Strokes collection. When the user touches the display, the MouseLeftButtonDown event is raised; at that time a new Stroke is created and added to the Strokes collection. When the user moves her finger, the MouseMove event is raised and the handler adds new StylusPoints to the Stroke. The stylus points are accessed through the MouseEventArgs of both the MouseLeftButtonDown and MouseMove events. When the user lifts her finger off the display, the MouseLeftButtonUp handler completes the current Stroke by setting it to null.

LISTING 7.1. InkPresenterView Class—Non-MVVM (excerpt)


public partial class InkPresenterView : PhoneApplicationPage
{
    public InkPresenterView()
    {
        InitializeComponent();
    }

    readonly StrokeCollection strokes = new StrokeCollection();
    Stroke stroke;

    void InkPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        InkPresenter inkPresenter = (InkPresenter)sender;
        stroke = new Stroke();
        StylusPointCollection points
            = e.StylusDevice.GetStylusPoints(inkPresenter);
        stroke.StylusPoints.Add(points);
        stroke.DrawingAttributes.Color = Colors.White;
        strokes.Add(stroke);
        inkPresenter.Strokes = strokes;
    }

    void InkPresenter_MouseMove(object sender, MouseEventArgs e)
    {
        InkPresenter inkPresenter = (InkPresenter)sender;
        if (stroke != null)
        {
            stroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter));
        }
    }

    void InkPresenter_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        stroke = null;
    }
}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.187.207