DrawingVisual オブジェクトの使用

更新 : 2007 年 11 月

ここでは、WPF ビジュアル層で DrawingVisual オブジェクトを使用する方法の概要を説明します。

このトピックは、次のセクションで構成されています。

  • DrawingVisual オブジェクト

  • DrawingVisual ホスト コンテナ

  • DrawingVisual オブジェクトの作成

  • FrameworkElement メンバのオーバーライドの作成

  • ヒット テストのサポート

  • 関連トピック

DrawingVisual オブジェクト

DrawingVisual は、図形、イメージ、またはテキストの描画に使用する軽量の描画クラスです。このクラスが軽量と見なされる理由は、レイアウトやイベントの処理を実現しないため、パフォーマンスが向上するからです。このため、描画は背景やクリップ アートに適しています。

DrawingVisual ホスト コンテナ

DrawingVisual オブジェクトを使用するには、オブジェクトのホスト コンテナを作成する必要があります。ホスト コンテナ オブジェクトは、FrameworkElement クラスから継承する必要があります。このクラスは、DrawingVisual クラスがサポートしていないレイアウトやイベント処理をサポートします。ホスト コンテナ オブジェクトの主な目的は子オブジェクトを格納することなので、ホスト コンテナ オブジェクトには表示プロパティは表示されません。ただし、ホスト コンテナの Visibility プロパティは、Visible に設定する必要があります。この値に設定しない場合、その子要素のいずれも表示されなくなります。

ビジュアル オブジェクトのホスト コンテナ オブジェクトを作成する場合は、VisualCollection にビジュアル オブジェクト参照を格納する必要があります。Add メソッドを使用して、ビジュアル オブジェクトをホスト コンテナに追加します。次の例では、ホスト コンテナ オブジェクトを作成し、3 つのビジュアル オブジェクトをその VisualCollection に追加します。

// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual objects.
public class MyVisualHost : FrameworkElement
{
    // Create a collection of child visual objects.
    private VisualCollection _children;

    public MyVisualHost()
    {
        _children = new VisualCollection(this);
        _children.Add(CreateDrawingVisualRectangle());
        _children.Add(CreateDrawingVisualText());
        _children.Add(CreateDrawingVisualEllipses());

        // Add the event handler for MouseLeftButtonUp.
        this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
    }
ms742254.alert_note(ja-jp,VS.90).gifメモ :

前のコード例を含むコード サンプル全体については、「DrawingVisuals を使用したヒット テストのサンプル」を参照してください。

DrawingVisual オブジェクトの作成

DrawingVisual オブジェクトの作成時には、オブジェクトに描画内容は含まれていません。テキスト、グラフィックス、またはイメージ コンテンツを追加するには、オブジェクトの DrawingContext を取得し、そのコンテキストに描画します。DrawingContext は、DrawingVisual オブジェクトの RenderOpen メソッドを呼び出すことによって返されます。

DrawingContext に四角形を描画するには、DrawingContext オブジェクトの DrawRectangle メソッドを使用します。他の種類のコンテンツを描画するための同様のメソッドがあります。DrawingContext へのコンテンツの描画が完了したら、Close メソッドを呼び出して DrawingContext を閉じ、コンテンツを保持します。

次の例では、DrawingVisual オブジェクトを作成し、その DrawingContext に四角形を描画します。

// Create a DrawingVisual that contains a rectangle.
private DrawingVisual CreateDrawingVisualRectangle()
{
    DrawingVisual drawingVisual = new DrawingVisual();

    // Retrieve the DrawingContext in order to create new drawing content.
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    // Create a rectangle and draw it in the DrawingContext.
    Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
    drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);

    // Persist the drawing content.
    drawingContext.Close();

    return drawingVisual;
}

FrameworkElement メンバのオーバーライドの作成

ホスト コンテナ オブジェクトは、ビジュアル オブジェクトのコレクションを管理します。このため、ホスト コンテナは派生 FrameworkElement クラスのメンバ オーバーライドを実装する必要があります。

オーバーライドする必要がある 2 つのメンバを次に示します。

  • GetVisualChild : 子要素のコレクションから指定したインデックス位置にある子を返します。

  • VisualChildrenCount : この要素内の子ビジュアル要素の数を取得します。

次の例では、2 つの FrameworkElement メンバのオーバーライドを実装します。

// Provide a required override for the VisualChildrenCount property.
protected override int VisualChildrenCount
{
    get { return _children.Count; }
}

// Provide a required override for the GetVisualChild method.
protected override Visual GetVisualChild(int index)
{
    if (index < 0 || index >= _children.Count)
    {
        throw new ArgumentOutOfRangeException();
    }

    return _children[index];
}

ヒット テストのサポート

ホスト コンテナ オブジェクトは、表示プロパティが表示されない場合でもイベント処理を提供できますが、その Visibility プロパティは必ず Visible に設定する必要があります。これにより、マウスの左ボタンが離されるなどのマウス イベントをトラップできる、ホスト コンテナのイベント処理ルーチンを作成できます。作成されたイベント処理ルーチンは、HitTest メソッドを呼び出してヒット テストを実装できます。このメソッドの HitTestResultCallback パラメータは、ヒット テストの結果アクションを決定するために使用できるユーザー定義のプロシージャを参照します。

次の例では、ホスト コンテナ オブジェクトとその子に対してヒット テストのサポートを実装します。

// Capture the mouse event and hit test the coordinate point value against
// the child visual objects.
void MyVisualHost_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Retreive the coordinates of the mouse button event.
    System.Windows.Point pt = e.GetPosition((UIElement)sender);

    // Initiate the hit test by setting up a hit test result callback method.
    VisualTreeHelper.HitTest(this, null, new HitTestResultCallback(myCallback), new PointHitTestParameters(pt));
}

// If a child visual object is hit, toggle its opacity to visually indicate a hit.
public HitTestResultBehavior myCallback(HitTestResult result)
{
    if (result.VisualHit.GetType() == typeof(DrawingVisual))
    {
        if (((DrawingVisual)result.VisualHit).Opacity == 1.0)
        {
            ((DrawingVisual)result.VisualHit).Opacity = 0.4;
        }
        else
        {
            ((DrawingVisual)result.VisualHit).Opacity = 1.0;
        }
    }

    // Stop the hit test enumeration of objects in the visual tree.
    return HitTestResultBehavior.Stop;
}

参照

概念

Windows Presentation Foundation のグラフィックス レンダリングの概要

ビジュアル層でのヒット テスト

参照

DrawingVisual

HitTest