次の方法で共有


インク入力コントロールの作成

インクを動的および静的にレンダリングするカスタム コントロールを作成できます。 つまり、ユーザーがストロークを描画する際にインクをレンダリングすると、インクがタブレット ペンから "流れる" ように見え、タブレット ペンを介してコントロールに追加された後、クリップボードから貼り付けるか、ファイルから読み込まれた後にインクを表示します。 インクを動的にレンダリングするには、コントロールで DynamicRendererを使用する必要があります。 インクを静的にレンダリングするには、スタイラス イベント メソッド (OnStylusDownOnStylusMove、および OnStylusUp) をオーバーライドして、StylusPoint データを収集し、ストロークを作成し、InkPresenter に追加する必要があります (コントロールにインクをレンダリングします)。

このトピックには、次のサブセクションが含まれています。

方法: スタイラス ポイント データを収集し、インク ストロークを作成する

インク ストロークを収集および管理するコントロールを作成するには、次の操作を行います。

  1. Control からクラスを派生させるか、Controlから派生したクラスの 1 つ (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. クラスに InkPresenter を追加し、Content プロパティを新しい InkPresenterに設定します。

    InkPresenter ip;
    
    public InkControl()
    {
        // Add an InkPresenter for drawing.
        ip = new InkPresenter();
        this.Content = ip;
    }
    
  3. AttachVisuals メソッドを呼び出して DynamicRendererRootVisualInkPresenter にアタッチし、StylusPlugIns コレクションに DynamicRenderer を追加します。 これにより、スタイラス ポイント データがコントロールによって収集されるときに、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 メソッドをオーバーライドします。 このメソッドでは、Captureの呼び出しでスタイラスをキャプチャします。 スタイラスをキャプチャすることで、スタイラスがコントロールの境界を離れる場合でも、コントロールは引き続き StylusMove イベントと StylusUp イベントを受け取ります。 これは厳密には必須ではありませんが、ほとんどの場合、優れたユーザー エクスペリエンスを実現するために必要です。 StylusPoint データを収集するために新しい StylusPointCollection を作成します。 最後に、StylusPoint データの初期セットを StylusPointCollectionに追加します。

    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 メソッドをオーバーライドし、前に作成した StylusPointCollection オブジェクトに StylusPoint データを追加します。

    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 メソッドをオーバーライドし、StylusPointCollection データを使用して新しい Stroke を作成します。 作成した新しい StrokeInkPresenterStrokes コレクションに追加し、スタイラス キャプチャを解放します。

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

方法: コントロールがマウスからの入力を受け入れるようにする

上記のコントロールをアプリケーションに追加して実行し、マウスを入力デバイスとして使用すると、ストロークが永続化されないことに気付きます。 マウスを入力デバイスとして使用するときにストロークを保持するには、次の操作を行います。

  1. OnMouseLeftButtonDown をオーバーライドし、新しい StylusPointCollection イベントが発生したときにマウスの位置を取得し、ポイント データを使用して StylusPoint を作成し、StylusPointCollectionStylusPoint を追加します。

    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 メソッドをオーバーライドします。 イベントが発生したときのマウスの位置を取得し、ポイント データを使用して StylusPoint を作成します。 前に作成した StylusPointCollection オブジェクトに StylusPoint を追加します。

    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 メソッドをオーバーライドします。 StylusPointCollection データを使用して新しい Stroke を作成し、作成した新しい StrokeInkPresenterStrokes コレクションに追加します。

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

まとめること

次の例は、ユーザーがマウスまたはペンを使用するときにインクを収集するカスタム コントロールです。

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

追加のプラグインと DynamicRenderers の使用

InkCanvas と同様に、カスタム コントロールにはカスタム StylusPlugIn と追加の DynamicRenderer オブジェクトを含めることができます。 これらを StylusPlugIns コレクションに追加します。 StylusPlugInCollection 内の StylusPlugIn オブジェクトの順序は、レンダリング時のインクの外観に影響します。 dynamicRenderer と呼ばれる DynamicRenderer と、タブレット ペンからインクをオフセットする translatePlugin というカスタム StylusPlugIn があるとします。 translatePluginStylusPlugInCollectionの最初の StylusPlugIn で、dynamicRenderer が 2 番目の場合、ユーザーがペンを移動すると、"フロー" するインクがオフセットされます。 dynamicRenderer が最初で、translatePlugin が 2 番目の場合、ユーザーがペンを持ち上げるまでインクはオフセットされません。

結論

スタイラス イベント メソッドをオーバーライドすることで、インクを収集してレンダリングするコントロールを作成できます。 独自のコントロールを作成し、独自の StylusPlugIn クラスを派生させ、それらを StylusPlugInCollectionに挿入することで、デジタル インクで想像できるほぼすべての動作を実装できます。 生成された StylusPoint データにアクセスでき、Stylus 入力をカスタマイズし、アプリケーションに応じて画面に表示することができます。 StylusPoint データへのアクセスレベルが低いため、インク収集を実装し、アプリケーションに最適なパフォーマンスでレンダリングできます。

関連項目