XPS OM でグラフィックスを描画する

このページでは、XPS OM でグラフィックスを描画する方法について説明します。

ページにベクター ベースのグラフィックスを描画するには、IXpsOMPath インターフェイスをインスタンス化し、これに必要なコンテンツを入力し、ページまたはキャンバスのビジュアル オブジェクトの一覧に追加します。 IXpsOMPath インターフェイスには、アウトライン、塗りつぶしの色、線のスタイル、線の色などのベクター ベースの図形のプロパティが含まれています。 パスの図形は、IXpsOMGeometryFigure インターフェイスのコレクションと、必要に応じて IXpsOMMatrixTransform インターフェイスを含む IXpsOMGeometry インターフェイスによって指定されます。 IXpsOMBrush インターフェイスを継承する任意のインターフェイスを使用して、パスの境界と、パスによって記述される図形の内部を塗りつぶすことができます。

次のコード例をプログラムで使用する前に、「一般的な XPS ドキュメント プログラミング タスク」の免責事項をお読みください。

コード例

次のコード例では、単一の色で塗りつぶされた四角形を記述する単純なパスを作成します。

ストロークと塗りつぶしブラシを作成する

コード例の最初のセクションでは、パス オブジェクトの塗りつぶしに使用される IXpsOMSolidColorBrush を作成します。

    HRESULT               hr = S_OK;

    XPS_COLOR             xpsColor;
    IXpsOMSolidColorBrush *xpsFillBrush = NULL;
    IXpsOMSolidColorBrush *xpsStrokeBrush = NULL;

    // Set the fill brush color to RED.
    xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
    xpsColor.value.sRGB.alpha = 0xFF;
    xpsColor.value.sRGB.red = 0xFF;
    xpsColor.value.sRGB.green = 0x00;
    xpsColor.value.sRGB.blue = 0x00;

    // Use the object factory to create the brush.
    hr = xpsFactory->CreateSolidColorBrush( 
        &xpsColor,
        NULL,          // color profile resource
        &xpsFillBrush);
    // The color profile resource parameter is NULL because
    //  this color type does not use a color profile resource.

    // Set the stroke brush color to BLACK.
    xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
    xpsColor.value.sRGB.alpha = 0xFF;
    xpsColor.value.sRGB.red = 0x00;
    xpsColor.value.sRGB.green = 0x00;
    xpsColor.value.sRGB.blue = 0x00;

    // Use the object factory to create the brush.
    hr = xpsFactory->CreateSolidColorBrush( 
            &xpsColor,
            NULL, // This color type does not use a color profile resource.
            &xpsStrokeBrush);

    // The brushes are released below after they have been used.

図形を定義する

コード例の 2 番目のセクションでは、IXpsOMGeometry インターフェイスを作成します。 次に、図の図形を指定する IXpsOMGeometryFigure インターフェイスを作成し、その図を IXpsOMGeometry インターフェイスに追加します。 この例では、rect で指定された四角形を作成します。 セグメントは、四角形の 4 つの辺のうち 3 つのみ定義する必要があります。 図形の周長 (この場合は四角形) は、始点から始まり、ジオメトリ図のセグメントによって定義されたとおりに拡張されます。 IsClosed プロパティを TRUE に設定すると、最後のセグメントの終点を始点に接続するセグメントを 1 つ追加することで四角形が閉じられることを示します。

    // rect is initialized outside of the sample and 
    //  contains the coordinates of the rectangular geometry to create.
    XPS_RECT                            rect = {0,0,100,100};       

    HRESULT                             hr = S_OK;
    IXpsOMGeometryFigure                *rectFigure;
    IXpsOMGeometry                      *imageRectGeometry;
    IXpsOMGeometryFigureCollection      *geomFigureCollection;

    // Define the start point and create an empty figure.
    XPS_POINT                           startPoint = {rect.x, rect.y};
    hr = xpsFactory->CreateGeometryFigure( &startPoint, &rectFigure );
    // Define the segments of the geometry figure.
    //  First, define the type of each segment.
    XPS_SEGMENT_TYPE segmentTypes[3] = {
        XPS_SEGMENT_TYPE_LINE,  // each segment is a straight line
        XPS_SEGMENT_TYPE_LINE, 
        XPS_SEGMENT_TYPE_LINE
    };

    // Define the x and y coordinates of each corner of the figure
    //  the start point has already been defined so only the 
    //  remaining three corners need to be defined.
    FLOAT segmentData[6] = {
        rect.x,                (rect.y + rect.height),
        (rect.x + rect.width), (rect.y + rect.height), 
        (rect.x + rect.width), rect.y 
    };

    // Describe if the segments are stroked (that is if the segment lines
    //  should be drawn as a line).
    BOOL segmentStrokes[3] = {
        TRUE, TRUE, TRUE // Yes, draw each of the segment lines.
    };

    // Add the segment data to the figure.
    hr = rectFigure->SetSegments(
                        3, 
                        6, 
                        segmentTypes, 
                        segmentData, 
                        segmentStrokes);

    // Set the closed and filled properties of the figure.
    hr = rectFigure->SetIsClosed( TRUE );
    hr = rectFigure->SetIsFilled( TRUE );
 
    // Create the geometry object.
    hr = xpsFactory->CreateGeometry( &imageRectGeometry );
    
    // Get a pointer to the figure collection interface of the geometry...
    hr = imageRectGeometry->GetFigures( &geomFigureCollection );

    // ...and then add the figure created above to this geometry.
    hr = geomFigureCollection->Append( rectFigure );
    // If not needed for anything else, release the rectangle figure.
    rectFigure->Release();

    // when done adding figures, release the figure collection. 
    geomFigureCollection->Release();

    //  When done with the geometry object, release the object.
    // imageRectGeometry->Release();
    //  In this case, imageRectGeometry is used in the next sample
    //  so the geometry object is not released, yet.
    

パスを作成してビジュアル コレクションに追加する

このコード例の最後のセクションでは、パス オブジェクトを作成して構成し、ページのビジュアル オブジェクトの一覧に追加します。

    HRESULT                    hr = S_OK;
    // The page interface pointer is initialized outside of this sample.
    IXpsOMPath                *rectPath = NULL;
    IXpsOMVisualCollection    *pageVisuals = NULL;

    // Create the new path object.
    hr = xpsFactory->CreatePath( &rectPath );

    // Add the geometry to the path.
    //  imageRectGeometry is initialized outside of this example.
    hr = rectPath->SetGeometryLocal( imageRectGeometry );

    // Set the short description of the path to provide
    //  a textual description of the object for accessibility.
    hr = rectPath->SetAccessibilityShortDescription( L"Red Rectangle" );
    
    // Set the fill and stroke brushes to use the brushes 
    //  created in the first section.
    hr = rectPath->SetFillBrushLocal( xpsFillBrush );
    hr = rectPath->SetStrokeBrushLocal( xpsStrokeBrush);

    // Get the visual collection of this page and add this path to it.
    hr = xpsPage->GetVisuals( &pageVisuals );
    hr = pageVisuals->Append( rectPath );
    // If not needed for anything else, release the rectangle path.
    rectPath->Release();
    
    // When done with the visual collection, release it.
    pageVisuals->Release();

    // When finished with the brushes, release the interface pointers.
    if (NULL != xpsFillBrush) xpsFillBrush->Release();
    if (NULL != xpsStrokeBrush) xpsStrokeBrush->Release();

    // When done with the geometry interface, release it.
    imageRectGeometry->Release();

    // When done with the path interface, release it.
    rectPath->Release();

ベスト プラクティス

IXpsOMPath インターフェイスで指定された図形の説明テキストを追加します。 視覚障碍のあるユーザーのために、SetAccessibilityShortDescription メソッドと SetAccessibilityLongDescription メソッドを使用して、スクリーン リーダーなどのアクセシビリティ サポート機能のテキスト コンテンツを提供します。

追加情報

このページのコード例では、パスの塗りつぶしブラシとストローク ブラシとして IXpsOMSolidColorBrush インターフェイスを使用します。 IXpsOMSolidColorBrush インターフェイスに加えて、IXpsOMGradientBrushIXpsOMImageBrush、または IXpsOMVisualBrush インターフェイスを使用できます。

ストロークは、図の周囲に描画できる線です。 XML Paper Specification では、さまざまなストロークの線スタイルがサポートされています。 ストロークの線スタイルを指定するには、 IXpsOMPath インターフェイスの次のメソッドを使用してストロークのプロパティを設定します。

次のステップ

XPS OM 内を移動する

XPS OM にテキストを書き込む

XPS OM にイメージを配置する

XPS OM を XPS ドキュメントに書き込む

XPS OM を印刷する

このページで使用

IOpcPartUri

IXpsOMGeometry

IXpsOMGeometryFigure

IXpsOMGeometryFigureCollection

IXpsOMObjectFactory

IXpsOMPage

IXpsOMPath

IXpsOMSolidColorBrush

IXpsOMVisualCollection

詳細情報

XPS OM の初期化

XPS ドキュメント API リファレンス

XML Paper Specification