Condividi tramite


Come creare un pennello sfumato lineare

Per creare un pennello sfumatura lineare, utilizzare il metodo CreateLinearGradientBrush e specificare le proprietà del pennello sfumatura lineare e la raccolta di interruzioni sfumature. Alcuni overload consentono di specificare le proprietà del pennello. Il codice seguente illustra come creare un pennello sfumato lineare per riempire un quadrato e un pennello nero a tinta unita per disegnare il contorno del quadrato.

Il codice produce l'output illustrato nella figura seguente.

illustrazione di un quadrato riempito con un pennello sfumato lineare

  1. Dichiarare una variabile di tipo ID2D1LinearGradientBrush.

        ID2D1LinearGradientBrush *m_pLinearGradientBrush;
    
  2. Utilizzare il metodo ID2D1RenderTarget::CreateGradientStopCollection per creare l'insieme ID2D1GradientStopCollection con una matrice dichiarata di strutture D2D1_GRADIENT_STOP, come illustrato nel codice seguente.

    Nota

    A partire da Windows 8, puoi usare il metodo ID2D1DeviceContext::CreateGradientStopCollection per creare invece un insieme ID2D1GradientStopCollection1. Questa interfaccia aggiunge sfumature a colori elevati e l'interpolazione di sfumature in colori dritti o premoltiplicati. Per altre informazioni, vedere la pagina ID2DDeviceContext::CreateGradientStopCollection .

     

    // 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. Usare ID2D1RenderTarget ::CreateLinearGradientBrush per creare un pennello sfumato lineare, riempire il quadrato con il pennello e disegnare il quadrato con il pennello nero.

    // The line that determines the direction of the gradient starts at
    // the upper-left corner of the square and ends at the lower-right corner.
    
    if (SUCCEEDED(hr))
    {
        hr = m_pRenderTarget->CreateLinearGradientBrush(
            D2D1::LinearGradientBrushProperties(
                D2D1::Point2F(0, 0),
                D2D1::Point2F(150, 150)),
            pGradientStops,
            &m_pLinearGradientBrush
            );
    }
    
    m_pRenderTarget->FillRectangle(&rcBrushRect, m_pLinearGradientBrush);
    m_pRenderTarget->DrawRectangle(&rcBrushRect, m_pBlackBrush, 1, NULL);
    

Informazioni di riferimento su Direct2D