Sdílet prostřednictvím


Vytvoření ovládacího prvku vstupu inkoustu

Můžete vytvořit vlastní ovládací prvek, který dynamicky a staticky vykresluje rukopis. To znamená, že vykreslení rukopisu jako uživatel nakreslí tah, což způsobí, že rukopis bude "proudit" z pera tabletu a zobrazit rukopis po jeho přidání do ovládacího prvku, a to buď pomocí pera tabletu, vloženého ze schránky nebo načteného ze souboru. Chcete-li dynamicky vykreslit rukopis, musí ovládací prvek použít .DynamicRenderer Pokud chcete staticky vykreslit rukopis, musíte přepsat metody událostí pera (OnStylusDownOnStylusMove, a OnStylusUp) ke shromažďování StylusPoint dat, vytváření tahů a jejich přidání do InkPresenter (který vykreslí rukopis na ovládacím prvku).

Toto téma obsahuje následující pododdíly:

Postupy: Shromažďování dat bodů pera a vytváření tahů rukopisu

Pokud chcete vytvořit ovládací prvek, který shromažďuje a spravuje tahy rukopisu, postupujte takto:

  1. Odvození třídy nebo jedné z tříd odvozených z ControlControl, například Label.

    using System;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Input.StylusPlugIns;
    using System.Windows.Controls;
    using System.Windows;
    
    class InkControl : Label
    {
    
    }
    
  2. Přidejte do třídy a InkPresenter nastavte Content vlastnost na novou InkPresenter.

    InkPresenter ip;
    
    public InkControl()
    {
        // Add an InkPresenter for drawing.
        ip = new InkPresenter();
        this.Content = ip;
    }
    
  3. RootVisual Připojte k AttachVisualsInkPresenter metodě metodu DynamicRenderer a přidejte DynamicRenderer ji do StylusPlugIns kolekce. InkPresenter Díky tomu může ovládací prvek zobrazit rukopis jako data pera.

    public InkControl()
    {
    
        // Add a dynamic renderer that
        // draws ink as it "flows" from the stylus.
        dr = new DynamicRenderer();
        ip.AttachVisuals(dr.RootVisual, dr.DrawingAttributes);
        this.StylusPlugIns.Add(dr);
    }
    
  4. Přepište metodu OnStylusDown . V této metodě zachyťte pero voláním Capture. Zachycením pera bude ovládací prvek nadále přijímat StylusMove a StylusUp události i v případě, že pero opustí hranice ovládacího prvku. To není výhradně povinné, ale téměř vždy žádoucí pro dobré uživatelské prostředí. Vytvořte nové StylusPointCollection pro shromažďování StylusPoint dat. Nakonec do souboru StylusPointCollectionpřidejte počáteční sadu StylusPoint dat .

    protected override void OnStylusDown(StylusDownEventArgs e)
    {
        // Capture the stylus so all stylus input is routed to this control.
        Stylus.Capture(this);
    
        // Allocate memory for the StylusPointsCollection and
        // add the StylusPoints that have come in so far.
        stylusPoints = new StylusPointCollection();
        StylusPointCollection eventPoints =
            e.GetStylusPoints(this, stylusPoints.Description);
    
        stylusPoints.Add(eventPoints);
    }
    
  5. Přepište metodu OnStylusMove a přidejte StylusPoint data do objektu StylusPointCollection , který jste vytvořili dříve.

    protected override void OnStylusMove(StylusEventArgs e)
    {
        if (stylusPoints == null)
        {
            return;
        }
    
        // Add the StylusPoints that have come in since the
        // last call to OnStylusMove.
        StylusPointCollection newStylusPoints =
            e.GetStylusPoints(this, stylusPoints.Description);
        stylusPoints.Add(newStylusPoints);
    }
    
  6. Přepište metodu OnStylusUp a vytvořte novou Stroke s daty StylusPointCollection . Přidejte nové Stroke , které jste vytvořili, do Strokes kolekce zachycení pera InkPresenter a vydané verze.

    protected override void OnStylusUp(StylusEventArgs e)
    {
        if (stylusPoints == null)
        {
            return;
        }
    
        // Add the StylusPoints that have come in since the
        // last call to OnStylusMove.
        StylusPointCollection newStylusPoints =
            e.GetStylusPoints(this, stylusPoints.Description);
        stylusPoints.Add(newStylusPoints);
    
        // Create a new stroke from all the StylusPoints since OnStylusDown.
        Stroke stroke = new Stroke(stylusPoints);
    
        // Add the new stroke to the Strokes collection of the InkPresenter.
        ip.Strokes.Add(stroke);
    
        // Clear the StylusPointsCollection.
        stylusPoints = null;
    
        // Release stylus capture.
        Stylus.Capture(null);
    }
    

Postupy: Povolení ovládacího prvku přijímat vstup z myši

Pokud do aplikace přidáte předchozí ovládací prvek, spustíte ho a použijete myš jako vstupní zařízení, všimnete si, že tahy nejsou trvalé. Pokud chcete zachovat tahy při použití myši jako vstupní zařízení, postupujte takto:

  1. OnMouseLeftButtonDown Přepsat a vytvořit novou StylusPointCollection get pozici myši, když došlo k události, a vytvořit StylusPoint pomocí bod data a přidat StylusPoint do objektu StylusPointCollection.

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
    
        base.OnMouseLeftButtonDown(e);
    
        // If a stylus generated this event, return.
        if (e.StylusDevice != null)
        {
            return;
        }
    
        // Start collecting the points.
        stylusPoints = new StylusPointCollection();
        Point pt = e.GetPosition(this);
        stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
    }
    
  2. Přepište metodu OnMouseMove . Získejte pozici myši, když došlo k události, a vytvořte StylusPoint pomocí dat bodu. Přidejte objekt StylusPoint , StylusPointCollection který jste vytvořili dříve.

    protected override void OnMouseMove(MouseEventArgs e)
    {
    
        base.OnMouseMove(e);
    
        // If a stylus generated this event, return.
        if (e.StylusDevice != null)
        {
            return;
        }
    
        // Don't collect points unless the left mouse button
        // is down.
        if (e.LeftButton == MouseButtonState.Released ||
            stylusPoints == null)
        {
            return;
        }
    
        Point pt = e.GetPosition(this);
        stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
    }
    
  3. Přepište metodu OnMouseLeftButtonUp . Vytvořte nový Stroke s daty StylusPointCollection a přidejte nové Stroke , které jste vytvořili do Strokes kolekce objektu InkPresenter.

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
    
        base.OnMouseLeftButtonUp(e);
    
        // If a stylus generated this event, return.
        if (e.StylusDevice != null)
        {
            return;
        }
    
        if (stylusPoints == null)
        {
            return;
        }
    
        Point pt = e.GetPosition(this);
        stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
    
        // Create a stroke and add it to the InkPresenter.
        Stroke stroke = new Stroke(stylusPoints);
        stroke.DrawingAttributes = dr.DrawingAttributes;
        ip.Strokes.Add(stroke);
    
        stylusPoints = null;
    }
    

Seskupování

Následující příklad je vlastní ovládací prvek, který shromažďuje rukopis, když uživatel používá myš nebo pero.

using System;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Controls;
using System.Windows;
// A control for managing ink input
class InkControl : Label
{
    InkPresenter ip;
    DynamicRenderer dr;

    // The StylusPointsCollection that gathers points
    // before Stroke from is created.
    StylusPointCollection stylusPoints = null;

    public InkControl()
    {
        // Add an InkPresenter for drawing.
        ip = new InkPresenter();
        this.Content = ip;

        // Add a dynamic renderer that
        // draws ink as it "flows" from the stylus.
        dr = new DynamicRenderer();
        ip.AttachVisuals(dr.RootVisual, dr.DrawingAttributes);
        this.StylusPlugIns.Add(dr);
    }

    static InkControl()
    {
        // Allow ink to be drawn only within the bounds of the control.
        Type owner = typeof(InkControl);
        ClipToBoundsProperty.OverrideMetadata(owner,
            new FrameworkPropertyMetadata(true));
    }

    protected override void OnStylusDown(StylusDownEventArgs e)
    {
        // Capture the stylus so all stylus input is routed to this control.
        Stylus.Capture(this);

        // Allocate memory for the StylusPointsCollection and
        // add the StylusPoints that have come in so far.
        stylusPoints = new StylusPointCollection();
        StylusPointCollection eventPoints =
            e.GetStylusPoints(this, stylusPoints.Description);

        stylusPoints.Add(eventPoints);
    }

    protected override void OnStylusMove(StylusEventArgs e)
    {
        if (stylusPoints == null)
        {
            return;
        }

        // Add the StylusPoints that have come in since the
        // last call to OnStylusMove.
        StylusPointCollection newStylusPoints =
            e.GetStylusPoints(this, stylusPoints.Description);
        stylusPoints.Add(newStylusPoints);
    }

    protected override void OnStylusUp(StylusEventArgs e)
    {
        if (stylusPoints == null)
        {
            return;
        }

        // Add the StylusPoints that have come in since the
        // last call to OnStylusMove.
        StylusPointCollection newStylusPoints =
            e.GetStylusPoints(this, stylusPoints.Description);
        stylusPoints.Add(newStylusPoints);

        // Create a new stroke from all the StylusPoints since OnStylusDown.
        Stroke stroke = new Stroke(stylusPoints);

        // Add the new stroke to the Strokes collection of the InkPresenter.
        ip.Strokes.Add(stroke);

        // Clear the StylusPointsCollection.
        stylusPoints = null;

        // Release stylus capture.
        Stylus.Capture(null);
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {

        base.OnMouseLeftButtonDown(e);

        // If a stylus generated this event, return.
        if (e.StylusDevice != null)
        {
            return;
        }

        // Start collecting the points.
        stylusPoints = new StylusPointCollection();
        Point pt = e.GetPosition(this);
        stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {

        base.OnMouseMove(e);

        // If a stylus generated this event, return.
        if (e.StylusDevice != null)
        {
            return;
        }

        // Don't collect points unless the left mouse button
        // is down.
        if (e.LeftButton == MouseButtonState.Released ||
            stylusPoints == null)
        {
            return;
        }

        Point pt = e.GetPosition(this);
        stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {

        base.OnMouseLeftButtonUp(e);

        // If a stylus generated this event, return.
        if (e.StylusDevice != null)
        {
            return;
        }

        if (stylusPoints == null)
        {
            return;
        }

        Point pt = e.GetPosition(this);
        stylusPoints.Add(new StylusPoint(pt.X, pt.Y));

        // Create a stroke and add it to the InkPresenter.
        Stroke stroke = new Stroke(stylusPoints);
        stroke.DrawingAttributes = dr.DrawingAttributes;
        ip.Strokes.Add(stroke);

        stylusPoints = null;
    }
}

Použití dalších modulů plug-in a DynamicRenderers

Stejně jako InkCanvas může mít váš vlastní ovládací prvek vlastní StylusPlugIn a další DynamicRenderer objekty. Přidejte je do StylusPlugIns kolekce. Pořadí StylusPlugIn objektů v objektech StylusPlugInCollection má vliv na vzhled rukopisu při vykreslení. Předpokládejme, že máte volaný DynamicRendererdynamicRenderer a vlastní StylusPlugIn název translatePlugin , který odsadí rukopis z pera tabletu. Pokud translatePlugin je první StylusPlugIn v sadě StylusPlugInCollection, a dynamicRenderer je druhá, rukopis, který "toky" bude posunut, když uživatel přesune pero. Pokud dynamicRenderer je první a translatePlugin je druhý, rukopis nebude posunut, dokud uživatel nezruší pero.

Závěr

Můžete vytvořit ovládací prvek, který shromažďuje a vykresluje rukopis přepsáním metod událostí stylus. Vytvořením vlastního ovládacího prvku, odvozením vlastních StylusPlugIn tříd a jejich vložením do StylusPlugInCollection, můžete implementovat prakticky jakékoli chování, které lze imaginovat pomocí digitálního rukopisu. Máte přístup k datům StylusPoint , která se generují, takže máte možnost přizpůsobit Stylus vstup a vykreslit je na obrazovce podle potřeby pro vaši aplikaci. Vzhledem k tomu, že máte takový přístup na nízké úrovni k StylusPoint datům, můžete implementovat kolekci rukopisu a vykreslit ji s optimálním výkonem pro vaši aplikaci.

Viz také