共用方式為


在 XPS OM 中繪製圖形

此頁面描述如何在 XPS OM 中繪製圖形。

若要在頁面中繪製以向量為基礎的圖形,請具現化 IXpsOMPath 介面、填入所需的內容,並將它新增至頁面或畫布的視覺物件清單。 IXpsOMPath 介面包含向量型圖形的這類屬性,例如外框、填滿色彩、線條樣式和線條色彩。 路徑的圖形是由 IXpsOMGeometry 介面所指定,其中包含 IXpsOMGeometryFigure 介面的 集合,並選擇性地指定 IXpsOMMatrixTransform 介面。 您可以使用繼承自 IXpsOMBrush 介面的任何介面,填滿路徑的周長和路徑所描述之圖形的內部。

在程式中使用下列程式碼範例之前,請先閱讀 Common 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.

定義圖形

程式碼範例的第二個區段會 建立 IXpsOMGeometry 介面。 然後, 它會建立指定圖形圖形的 IXpsOMGeometryFigure 介面,並將該圖形新增至 IXpsOMGeometry 介面。 此範例會建立由 rect 指定的 矩形。 區段必須只針對矩形四側的三個定義。 在此案例中,圖形的周長是從起點開始,並延伸為幾何圖形區段所定義。 將 IsClosed 屬性設定為 TRUE 表示矩形已關閉,方法是新增一個額外的區段,將最後一個區段的結尾連接到起點。

    // 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 介面之外,您還可以使用 IXpsOMGradientBrush、 IXpsOMImageBrush IXpsOMVisualBrush 介面。

筆劃是可在圖形周長周圍繪製的線條。 XML 紙張規格 支援許多不同的筆劃線條樣式。 若要指定筆劃線條樣式,請使用下列 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 紙張規格