Mürekkep Giriş Denetimi Oluşturma

Mürekkepleri dinamik ve statik olarak işleyen özel bir denetim oluşturabilirsiniz. Başka bir ifadeyle, kullanıcı olarak mürekkebi işlemek bir vuruş çizerek mürekkebi tablet kaleminden "akıyor" gibi görünmesini sağlar ve tablet kalemi aracılığıyla, Pano'dan yapıştırılan veya bir dosyadan yüklenen denetime eklendikten sonra mürekkebi görüntüler. Mürekkepleri dinamik olarak işlemek için denetiminizin bir DynamicRendererkullanması gerekir. Mizeyi statik olarak işlemek için, ekran kalemi olay yöntemlerini (OnStylusDown, ve OnStylusUp) geçersiz kılarak veri toplamanızStylusPoint, OnStylusMovevuruşlar oluşturmanız ve bunları bir InkPresenter öğeye eklemeniz gerekir (denetimdeki milenk işlenir).

Bu konu aşağıdaki alt bölümler içerir:

Nasıl yapılır: Ekran Kalemi Noktası Verilerini Toplama ve Mürekkep Vuruşları Oluşturma

Mürekkep vuruşlarını toplayan ve yöneten bir denetim oluşturmak için aşağıdakileri yapın:

  1. veya gibi öğesinden Control türetilen sınıflardan ControlLabelbirini türetin.

    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. sınıfına bir InkPresenter ekleyin ve özelliğini yeni InkPresenterolarak ayarlayınContent.

    InkPresenter ip;
    
    public InkControl()
    {
        // Add an InkPresenter for drawing.
        ip = new InkPresenter();
        this.Content = ip;
    }
    
  3. RootVisualInkPresenterDynamicRenderer yöntemini çağırarak AttachVisuals öğesini öğesine ekleyin ve öğesini koleksiyona StylusPlugIns ekleyinDynamicRenderer. Bu, ekran kalemi noktası verileri denetiminiz tarafından toplanırken marak görüntülemesini sağlar InkPresenter .

    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. OnStylusDown yöntemini geçersiz kılın. Bu yöntemde, bir çağrısı ile ekran kalemini yakalayın Capture. Ekran kalemi yakalanarak, ekran kalemi denetimin sınırlarını terk etse bile denetiminiz ve StylusUp olayları almaya StylusMove devam eder. Bu kesinlikle zorunlu değildir, ancak iyi bir kullanıcı deneyimi için neredeyse her zaman istenir. Veri toplamak StylusPoint için yeni StylusPointCollection bir oluşturma. Son olarak, ilk veri kümesini StylusPoint öğesine StylusPointCollectionekleyin.

    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. OnStylusMove yöntemini geçersiz kılın ve verileri daha önce oluşturduğunuz nesneye StylusPointCollection ekleyinStylusPoint.

    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. OnStylusUp yöntemini geçersiz kılın ve verilerle StylusPointCollection yeni Stroke bir oluşturun. Oluşturduğunuz Strokes yeni Stroke sürümü ve yayın ekran kalemi yakalama koleksiyonuna InkPresenter ekleyin.

    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);
    }
    

Nasıl yapılır: Fareden Girişi Kabul Etmek için Denetiminizi Etkinleştirme

Önceki denetimi uygulamanıza ekler, çalıştırır ve fareyi giriş cihazı olarak kullanırsanız vuruşların kalıcı olmadığını fark edeceksiniz. Fare giriş cihazı olarak kullanıldığında vuruşları kalıcı hale getirmek için aşağıdakileri yapın:

  1. OnMouseLeftButtonDown öğesini geçersiz kılın ve yeni StylusPointCollection bir oluşturun Olay gerçekleştiğinde farenin konumunu alın ve nokta verilerini kullanarak bir StylusPoint oluşturun ve öğesine ekleyin StylusPointStylusPointCollection.

    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. OnMouseMove yöntemini geçersiz kılın. Olay gerçekleştiğinde farenin konumunu alın ve nokta verilerini kullanarak bir StylusPoint oluşturun. öğesini StylusPoint daha önce oluşturduğunuz nesneye StylusPointCollection ekleyin.

    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. OnMouseLeftButtonUp yöntemini geçersiz kılın. Verilerle StylusPointCollection yeni Stroke bir oluşturun ve oluşturduğunuz yeniyi Stroke koleksiyonuna StrokesInkPresenterekleyin.

    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;
    }
    

Bir araya getirmek

Aşağıdaki örnek, kullanıcı fareyi veya kalemi kullandığında mürekkep toplayan özel bir denetimdir.

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;
    }
}

Ek Eklentiler ve DynamicRenderers Kullanma

InkCanvas gibi, özel denetiminiz de özel StylusPlugIn ve ek DynamicRenderer nesnelere sahip olabilir. Bunları koleksiyona StylusPlugIns ekleyin. içindeki StylusPlugInCollection nesnelerin sırasıStylusPlugIn, işlendiğinde mürekkep görünümünü etkiler. Mürekkepten tablet kalemine uzaklık sağlayan bir çağrınız dynamicRenderer ve StylusPlugIn özel bir çağrınız DynamicRenderertranslatePlugin olduğunu varsayalım. translatePlugin içinde ilk StylusPlugInStylusPlugInCollectionve dynamicRenderer ikinci ise, kullanıcı kalemi taşırken "akan" mürekkep kaydırılır. İlk ve translatePlugin ikinci isedynamicRenderer, kullanıcı kalemi kaldırana kadar mürekkep kaydırılmaz.

Sonuç

Ekran kalemi olay yöntemlerini geçersiz kılarak mürekkep toplayan ve işleyen bir denetim oluşturabilirsiniz. Kendi denetiminizi oluşturarak, kendi StylusPlugIn sınıflarınızı türeterek ve sınıflarını içine StylusPlugInCollectionekleyerek, dijital mürekkeple hayal edilebilen hemen her davranışı uygulayabilirsiniz. Verilere oluşturulduklarında StylusPoint erişebilirsiniz, böylece girişi özelleştirme Stylus ve uygulamanıza uygun şekilde ekranda işleme fırsatı elde edebilirsiniz. Verilere bu kadar düşük düzeyde erişiminiz StylusPoint olduğundan, mürekkep koleksiyonunu uygulayabilir ve uygulamanız için en iyi performansla işleyebilirsiniz.

Ayrıca bkz.