ID2D1RenderTarget::D rawRectangle (constD2D1_RECT_F*,ID2D1Brush*,FLOAT,ID2D1StrokeStyle*) 方法 (d2d1.h)

繪製具有指定維度和筆劃樣式之矩形的外框。

語法

void DrawRectangle(
  const D2D1_RECT_F *rect,
  ID2D1Brush        *brush,
  FLOAT             strokeWidth,
  ID2D1StrokeStyle  *strokeStyle
);

參數

rect

Type: [in] const D2D1_RECT_F*

要繪製之矩形的維度,以與裝置無關的圖元為單位。

brush

類型:[in] ID2D1Brush*

用來繪製矩形筆劃的筆刷。

strokeWidth

類型:[in] FLOAT

筆劃的寬度,以裝置無關的圖元為單位。 此值必須大於或等於0.0f。 如果未指定此參數,則預設為1.0f。 筆劃在線條上置中。

strokeStyle

類型:[in, 選擇性] ID2D1StrokeStyle*

要繪製的筆劃樣式,或繪製實心筆劃的 NULL

傳回值

備註

此方法失敗時,不會傳回錯誤碼。 若要判斷繪圖方法是否 (例如 DrawRectangle) 失敗,請檢查 ID2D1RenderTarget::EndDrawID2D1RenderTarget::Flush 方法傳回的結果。

範例

下列範例會使用 ID2D1HwndRenderTarget 來繪製和填滿數個矩形。 此範例會產生下圖所示的輸出。

網格線背景上兩個矩形的圖例
// This method discards device-specific
// resources if the Direct3D device disappears during execution and
// recreates the resources the next time it's invoked.
HRESULT DemoApp::OnRender()
{
    HRESULT hr = S_OK;

    hr = CreateDeviceResources();

    if (SUCCEEDED(hr))
    {
        m_pRenderTarget->BeginDraw();

        m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());

        m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

        D2D1_SIZE_F rtSize = m_pRenderTarget->GetSize();

        // Draw a grid background.
        int width = static_cast<int>(rtSize.width);
        int height = static_cast<int>(rtSize.height);

        for (int x = 0; x < width; x += 10)
        {
            m_pRenderTarget->DrawLine(
                D2D1::Point2F(static_cast<FLOAT>(x), 0.0f),
                D2D1::Point2F(static_cast<FLOAT>(x), rtSize.height),
                m_pLightSlateGrayBrush,
                0.5f
                );
        }

        for (int y = 0; y < height; y += 10)
        {
            m_pRenderTarget->DrawLine(
                D2D1::Point2F(0.0f, static_cast<FLOAT>(y)),
                D2D1::Point2F(rtSize.width, static_cast<FLOAT>(y)),
                m_pLightSlateGrayBrush,
                0.5f
                );
        }

        // Draw two rectangles.
        D2D1_RECT_F rectangle1 = D2D1::RectF(
            rtSize.width/2 - 50.0f,
            rtSize.height/2 - 50.0f,
            rtSize.width/2 + 50.0f,
            rtSize.height/2 + 50.0f
            );

        D2D1_RECT_F rectangle2 = D2D1::RectF(
            rtSize.width/2 - 100.0f,
            rtSize.height/2 - 100.0f,
            rtSize.width/2 + 100.0f,
            rtSize.height/2 + 100.0f
            );


        // Draw a filled rectangle.
        m_pRenderTarget->FillRectangle(&rectangle1, m_pLightSlateGrayBrush);

        // Draw the outline of a rectangle.
        m_pRenderTarget->DrawRectangle(&rectangle2, m_pCornflowerBlueBrush);

        hr = m_pRenderTarget->EndDraw();
    }

    if (hr == D2DERR_RECREATE_TARGET)
    {
        hr = S_OK;
        DiscardDeviceResources();
    }

    return hr;
}

如需相關教學課程,請參閱 建立簡單的 Direct2D 應用程式

規格需求

需求
目標平台 Windows
標頭 d2d1.h (包含 D2d1.h)
程式庫 D2d1.lib
Dll D2d1.dll

另請參閱

建立簡單的 Direct2D 應用程式

如何繪製和填滿基本圖形

ID2D1RenderTarget