インク入力コントロールの作成
インクを動的および静的にレンダリングするカスタム コントロールを作成できます。 つまり、ユーザーがストロークを描画する際にインクをレンダリングすると、インクがタブレット ペンから "流れる" ように見え、タブレット ペンを介してコントロールに追加された後、クリップボードから貼り付けるか、ファイルから読み込まれた後にインクを表示します。 インクを動的にレンダリングするには、コントロールで DynamicRendererを使用する必要があります。 インクを静的にレンダリングするには、スタイラス イベント メソッド (OnStylusDown、OnStylusMove、および OnStylusUp) をオーバーライドして、StylusPoint データを収集し、ストロークを作成し、InkPresenter に追加する必要があります (コントロールにインクをレンダリングします)。
このトピックには、次のサブセクションが含まれています。
方法: コントロールがマウス からの入力を受け入れるようにする
方法: スタイラス ポイント データを収集し、インク ストロークを作成する
インク ストロークを収集および管理するコントロールを作成するには、次の操作を行います。
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 {
}
クラスに InkPresenter を追加し、Content プロパティを新しい InkPresenterに設定します。
InkPresenter ip; public InkControl() { // Add an InkPresenter for drawing. ip = new InkPresenter(); this.Content = ip; }
AttachVisuals メソッドを呼び出して DynamicRenderer の RootVisual を InkPresenter にアタッチし、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); }
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); }
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); }
OnStylusUp メソッドをオーバーライドし、StylusPointCollection データを使用して新しい Stroke を作成します。 作成した新しい Stroke を InkPresenter の Strokes コレクションに追加し、スタイラス キャプチャを解放します。
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); }
方法: コントロールがマウスからの入力を受け入れるようにする
上記のコントロールをアプリケーションに追加して実行し、マウスを入力デバイスとして使用すると、ストロークが永続化されないことに気付きます。 マウスを入力デバイスとして使用するときにストロークを保持するには、次の操作を行います。
OnMouseLeftButtonDown をオーバーライドし、新しい StylusPointCollection イベントが発生したときにマウスの位置を取得し、ポイント データを使用して StylusPoint を作成し、StylusPointCollectionに StylusPoint を追加します。
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)); }
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)); }
OnMouseLeftButtonUp メソッドをオーバーライドします。 StylusPointCollection データを使用して新しい Stroke を作成し、作成した新しい Stroke を InkPresenterの Strokes コレクションに追加します。
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 があるとします。 translatePlugin
が StylusPlugInCollectionの最初の StylusPlugIn で、dynamicRenderer
が 2 番目の場合、ユーザーがペンを移動すると、"フロー" するインクがオフセットされます。 dynamicRenderer
が最初で、translatePlugin
が 2 番目の場合、ユーザーがペンを持ち上げるまでインクはオフセットされません。
結論
スタイラス イベント メソッドをオーバーライドすることで、インクを収集してレンダリングするコントロールを作成できます。 独自のコントロールを作成し、独自の StylusPlugIn クラスを派生させ、それらを StylusPlugInCollectionに挿入することで、デジタル インクで想像できるほぼすべての動作を実装できます。 生成された StylusPoint データにアクセスでき、Stylus 入力をカスタマイズし、アプリケーションに応じて画面に表示することができます。 StylusPoint データへのアクセスレベルが低いため、インク収集を実装し、アプリケーションに最適なパフォーマンスでレンダリングできます。
関連項目
.NET Desktop feedback