Share via


Comment créer un pinceau dégradé radial

Pour créer un pinceau dégradé radial, utilisez la méthode ID2DRenderTarget::CreateRadialGradientBrush et spécifiez les propriétés du pinceau de dégradé radial et la collection de points de dégradé. Certaines surcharges vous permettent de spécifier les propriétés du pinceau. Le code suivant montre comment créer un pinceau dégradé radial pour remplir un cercle et un pinceau noir plein pour dessiner le contour du cercle.

Le code produit le résultat présenté sur l’illustration suivante.

illustration d’un cercle rempli d’un pinceau dégradé radial

  1. Déclarez une variable de type ID2D1RadialGradientBrush.

        ID2D1RadialGradientBrush *m_pRadialGradientBrush;
    
  2. Créez un tableau de structures D2D1_GRADIENT_STOP à placer dans la collection de points de dégradé. La structure D2D1_GRADIENT_STOP contient la position et la couleur d’un point de dégradé. La position indique la position relative d’un point de dégradé dans le pinceau. La valeur se trouve dans la plage [0.0f, 1.0f], comme indiqué dans le code suivant.

    // Create an array of gradient stops to put in the gradient stop
    // collection that will be used in the gradient brush.
    ID2D1GradientStopCollection *pGradientStops = NULL;
    
    D2D1_GRADIENT_STOP gradientStops[2];
    gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Yellow, 1);
    gradientStops[0].position = 0.0f;
    gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::ForestGreen, 1);
    gradientStops[1].position = 1.0f;
    // Create the ID2D1GradientStopCollection from a previously
    // declared array of D2D1_GRADIENT_STOP structs.
    hr = m_pRenderTarget->CreateGradientStopCollection(
        gradientStops,
        2,
        D2D1_GAMMA_2_2,
        D2D1_EXTEND_MODE_CLAMP,
        &pGradientStops
        );
    
  3. Utilisez la méthode ID2D1RenderTarget::CreateGradientStopCollection pour créer la collection ID2D1GradientStopCollection à partir d’un tableau déclaré de structures D2D1_GRADIENT_STOP. Utilisez ensuite CreateRadialGradientBrush pour créer un pinceau dégradé radial.

    Remarque

    À compter de Windows 8, vous pouvez utiliser la méthode ID2D1DeviceContext::CreateGradientStopCollection pour créer une collection ID2D1GradientStopCollection1 plutôt que d’utiliser la méthode ID2D1RenderTarget::CreateGradientStopCollection. Cette interface ajoute des dégradés de couleur élevée et l’interpolation de dégradés dans des couleurs droites ou prémultipliées. Pour plus d’informations, consultez la page ID2DDeviceContext::CreateGradientStopCollection.

     

    // The center of the gradient is in the center of the box.
    // The gradient origin offset was set to zero(0, 0) or center in this case.
    if (SUCCEEDED(hr))
    {
        hr = m_pRenderTarget->CreateRadialGradientBrush(
            D2D1::RadialGradientBrushProperties(
                D2D1::Point2F(75, 75),
                D2D1::Point2F(0, 0),
                75,
                75),
            pGradientStops,
            &m_pRadialGradientBrush
            );
    }
    
    m_pRenderTarget->FillEllipse(ellipse, m_pRadialGradientBrush);
    m_pRenderTarget->DrawEllipse(ellipse, m_pBlackBrush, 1, NULL);
    

Référence Direct2D