복잡한 도형을 그리고 채우는 방법

Direct2D는 곡선, 호 및 선을 포함할 수 있는 복잡한 셰이프를 설명하기 위한 ID2D1PathGeometry 인터페이스를 제공합니다. 이 항목에서는 경로 기하 도형을 정의하고 렌더링하는 방법을 설명합니다.

경로 기하 도형을 정의하려면 먼저 ID2D1Factory::CreatePathGeometry 메서드를 사용하여 경로 기하 도형을 만든 다음 경로 기하 도형의 Open 메서드를 사용하여 ID2D1GeometrySink를 검색합니다. 그런 다음 싱크의 다양한 Add 메서드를 호출하여 선, 곡선 및 호를 추가할 수 있습니다.

다음 예제에서는 ID2D1PathGeometry를 만들고, 싱크를 검색하고, 이를 사용하여 모래 시계 모양을 정의합니다.

ID2D1GeometrySink *pSink = NULL;

// Create a path geometry.
if (SUCCEEDED(hr))
{
    hr = m_pD2DFactory->CreatePathGeometry(&m_pPathGeometry);

    if (SUCCEEDED(hr))
    {
        // Write to the path geometry using the geometry sink.
        hr = m_pPathGeometry->Open(&pSink);

        if (SUCCEEDED(hr))
        {
            pSink->BeginFigure(
                D2D1::Point2F(0, 0),
                D2D1_FIGURE_BEGIN_FILLED
                );

            pSink->AddLine(D2D1::Point2F(200, 0));

            pSink->AddBezier(
                D2D1::BezierSegment(
                    D2D1::Point2F(150, 50),
                    D2D1::Point2F(150, 150),
                    D2D1::Point2F(200, 200))
                );

            pSink->AddLine(D2D1::Point2F(0, 200));

            pSink->AddBezier(
                D2D1::BezierSegment(
                    D2D1::Point2F(50, 150),
                    D2D1::Point2F(50, 50),
                    D2D1::Point2F(0, 0))
                );

            pSink->EndFigure(D2D1_FIGURE_END_CLOSED);

            hr = pSink->Close();
        }
        SafeRelease(&pSink);
    }
}

ID2D1PathGeometry는 디바이스 독립적 리소스이므로 한 번 만들어 애플리케이션 수명 동안 보존할 수 있습니다. (다양한 유형의 리소스에 대한 자세한 내용은 리소스 개요를 참조하세요.)

다음 예제에서는 경로 기하 도형의 윤곽선과 채우기를 그리는 데 사용할 두 개의 브러시를 만듭니다.

if (SUCCEEDED(hr))
{
    // Create a black brush.
    hr = m_pRenderTarget->CreateSolidColorBrush(
        D2D1::ColorF(D2D1::ColorF::Black),
        &m_pBlackBrush
        );
}

if (SUCCEEDED(hr))
{
    // Create a linear gradient.
    static const D2D1_GRADIENT_STOP stops[] =
    {
        {   0.f,  { 0.f, 1.f, 1.f, 0.25f }  },
        {   1.f,  { 0.f, 0.f, 1.f, 1.f }  },
    };

    hr = m_pRenderTarget->CreateGradientStopCollection(
        stops,
        ARRAYSIZE(stops),
        &pGradientStops
        );

    if (SUCCEEDED(hr))
    {
        hr = m_pRenderTarget->CreateLinearGradientBrush(
            D2D1::LinearGradientBrushProperties(
                D2D1::Point2F(100, 0),
                D2D1::Point2F(100, 200)),
            D2D1::BrushProperties(),
            pGradientStops,
            &m_pLGBrush
            );
    }

    SafeRelease(&pGradientStops);
}

마지막 예제에서는 DrawGeometryFillGeometry 메서드를 사용하여 기하 도형의 윤곽선과 내부를 그립니다. 이 예제에서는 다음 그림에 표시된 출력을 생성합니다.

모래 시계 모양의 기하 도형 그림

void DemoApp::RenderGeometryExample()
{
    // Translate subsequent drawings by 20 device-independent pixels.
    m_pRenderTarget->SetTransform(
        D2D1::Matrix3x2F::Translation(20.f, 20.f)
        );

    // Draw the hour glass geometry at the upper left corner of the client area.
    m_pRenderTarget->DrawGeometry(m_pPathGeometry, m_pBlackBrush, 10.f);
    m_pRenderTarget->FillGeometry(m_pPathGeometry, m_pLGBrush);
}

이 예제에서는 코드를 생략합니다. 기하 도형에 대한 자세한 내용은 Geometries 개요를 참조하세요.