Condividi tramite


Disegnare grafica in un file XPS OM

Questa pagina descrive come disegnare grafica in un file XPS OM.

Per disegnare elementi grafici basati su vettori in una pagina, creare un'istanza di un'interfacciaIXpsOMPath, popolarla con il contenuto desiderato e aggiungerla all'elenco di oggetti visivi della pagina o dell'area di disegno. Un'interfaccia IXpsOMPath contiene tali proprietà di una forma basata su vettore come contorno, colore di riempimento, stile linea e colore linea. La forma del percorso viene specificata da un'interfacciaIXpsOMGeometry, che contiene un insieme di interfacce IXpsOMGeometryFigure e, facoltativamente, un'interfaccia IXpsOMMatrixTransform. È possibile usare qualsiasi interfaccia che eredita dall'interfaccia IXpsOMBrush per riempire il perimetro del percorso e l'interno della forma descritta dal percorso.

Prima di usare gli esempi di codice seguenti nel programma, leggere la dichiarazione di non responsabilità in Attività comuni di programmazione documenti XPS.

Esempio di codice

Nell'esempio di codice seguente viene creato un percorso semplice che descrive un rettangolo riempito con un singolo colore.

Creare il tratto e i pennelli di riempimento

La prima sezione dell'esempio di codice crea il IXpsOMSolidColorBrush che verrà usato per riempire l'oggetto path.

    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.

Definire la forma

La seconda sezione dell'esempio di codice crea l'interfacciaIXpsOMGeometry. Crea quindi l'interfacciaIXpsOMGeometryFigureche specifica la forma della figura e aggiunge la figura all'interfaccia IXpsOMGeometry. Nell'esempio viene creato un rettangolo specificato da . I segmenti devono essere definiti solo per tre dei quattro lati del rettangolo. Il perimetro della forma, in questo caso il rettangolo, inizia dal punto iniziale ed estende come definito dai segmenti della figura geometrica. L'impostazione della proprietà IsClosed su TRUE indica che il rettangolo viene chiuso aggiungendo un segmento aggiuntivo che collega la fine dell'ultimo segmento al punto iniziale.

    // 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.
    

Creare il percorso e aggiungerlo alla raccolta di oggetti visivi

La sezione finale di questo esempio di codice crea e configura l'oggetto path, quindi lo aggiunge all'elenco di oggetti visivi della pagina.

    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();

Procedure consigliate

Aggiungere una descrizione testuale della forma specificata dall'interfacciaIXpsOMPath. A vantaggio degli utenti con problemi visivi, usare i metodi SetAccessibilityShortDescription e SetAccessibilityLongDescription per fornire contenuto testuale per le funzionalità di supporto dell'accessibilità, ad esempio le utilità per la lettura dello schermo.

Informazioni aggiuntive

L'esempio di codice in questa pagina usa un'interfaccia IXpsOMSolidColorBrush come pennello di riempimento e pennello del tratto per il percorso. Oltre all'interfaccia IXpsOMSolidColorBrush, è possibile usare un'interfaccia IXpsOMGradientBrush, IXpsOMImageBrusho interfaccia IXpsOMVisualBrush.

Il tratto è la linea che può essere disegnata intorno al perimetro della figura. La XML Paper Specification supporta molti stili di linea del tratto diversi. Per specificare lo stile della linea del tratto, impostare le proprietà del tratto con i metodi seguenti dell'interfacciaIXpsOMPath:

passaggi successivi

Esplorare la XPS OM

scrivere testo in un XPS OM

posizionare le immagini in un XPS OM

Scrivere un file XPS OM in un documento XPS

Stampare un XPS OM

utilizzato in questa pagina

IOpcPartUri

IXpsOMGeometry

IXpsOMGeometryFigure

IXpsOMGeometryFigureCollection

IXpsOMObjectFactory

IXpsOMPage

IXpsOMPath

IXpsOMSolidColorBrush

IXpsOMVisualCollection

per altre informazioni

inizializzare un XPS OM

informazioni di riferimento sulle API documento XPS

xml Paper Specification