在 XPS OM 中绘制图形

本页介绍如何在 XPS OM 中绘制图形。

若要在页面中绘制矢量图形,请实例化 IXpsOMPath 接口,使用所需内容填充它,并将其添加到页面或画布的视觉对象列表中。 IXpsOMPath 接口包含矢量形状的属性,如轮廓、填充颜色、线条样式和线条颜色。 路径的形状是由 IXpsOMGeometry 接口(包含 IXpsOMGeometryFigure 接口的集合)和(可选)IXpsOMMatrixTransform 接口指定的。 可以使用继承自 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.

定义形状

代码示例的第二部分创建 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 接口指定的形状的文本说明。 为使具有视觉障碍的用户受益,请使用 SetAccessibilityShortDescriptionSetAccessibilityLongDescription 方法为辅助功能(如屏幕阅读器)提供文本内容

其他信息

此页中的代码示例使用 IXpsOMSolidColorBrush 接口作为路径的填充画笔和笔划画笔。 除了 IXpsOMSolidColorBrush 接口之外,还可以使用 IXpsOMGradientBrushIXpsOMImageBrushIXpsOMVisualBrush 接口

笔划是可以围绕图形外围绘制的线条。 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 纸张规范