ID2D1HwndRenderTarget 介面 (d2d1.h)

將繪圖指令轉譯至視窗。

繼承

ID2D1HwndRenderTarget 介面繼承自 ID2D1RenderTargetID2D1HwndRenderTarget 也有下列類型的成員:

方法

ID2D1HwndRenderTarget 介面具有這些方法。

 
ID2D1HwndRenderTarget::CheckWindowState

指出是否遮蔽與此轉譯目標相關聯的 HWND。
ID2D1HwndRenderTarget::GetHwnd

傳回與此轉譯目標相關聯的 HWND。
ID2D1HwndRenderTarget::Resize

將轉譯目標的大小變更為指定的圖元大小。 (多載 2/2)
ID2D1HwndRenderTarget::Resize

將轉譯目標的大小變更為指定的圖元大小。 (多載 1/2)

備註

如同其他轉譯目標的情況,您必須先呼叫 BeginDraw ,才能發出繪圖命令。 完成繪圖之後,請呼叫 EndDraw 以指出繪圖已完成,並釋放支持轉譯目標的緩衝區存取權。

針對ID2D1HwndRenderTarget,BeginDraw的唯一副作用是變更轉譯目標的狀態,以允許發出繪圖命令。

EndDraw 會排清任何批處理的繪圖命令。 如果沒有發生任何錯誤,它也會顯示緩衝區,導致它出現在相關聯的視窗上。 最後, EndDraw 會傳回繪圖或呈現中發生之第一個錯誤的 HRESULT,以及發生錯誤時的標記狀態。

ID2D1HwndRenderTarget 對象會經過雙重緩衝處理,因此發出的繪圖命令不會立即出現,而是會在離螢幕介面上執行。 呼叫 EndDraw 時,如果沒有轉譯錯誤,則會顯示螢幕外緩衝區。 如果 EndDraw 排清批次中發生轉譯錯誤,則不會顯示緩衝區,而且應用程式必須呼叫 BeginDraw 並重新繪製框架。 清可用來在呼叫 EndDraw 之前檢查錯誤,如果應用程式想要顯示框架,而不論錯誤為何。

硬體轉譯目標的後端緩衝區是由 GetPixelSize 所指定的大小。 如果 EndDraw 顯示緩衝區,則會延展此點陣圖,以覆蓋其呈現所在的表面:視窗的整個工作區。 如果轉譯目標是在硬體中轉譯,而且如果轉譯目標使用軟體,則會使用雙線性篩選來執行此延展。 (一般而言,應用程式會呼叫 Resize 以確保轉譯目標的圖元大小和目的地相符的圖元大小,而且不需要調整,但這不是必要專案。)

在視窗分層配接器的情況下,Direct2D 可確保從轉譯發生轉譯的配接器複製到需要顯示內容的配接器中,複製螢幕外轉譯目標的部分。

如果已移除轉譯目標所在的配接器,或在應用程式執行時升級驅動程式,則會在 EndDraw 呼叫中傳回此錯誤。 在此情況下,應用程式應該視需要建立新的轉譯目標和資源。

建立ID2D1HwndRenderTarget物件

若要建立 ID2D1HwndRenderTarget,請使用 ID2D1Factory::CreateHwndRenderTarget 方法。

您的應用程式應該建立轉譯目標一次,並在應用程式存留期間或直到轉譯目標的 EndDraw 方法傳回 D2DERR_RECREATE_TARGET 錯誤為止。 當您收到此錯誤時,您必須重新建立轉譯目標 (及其) 建立的任何資源。

範例

下列範例會使用 CreateHwndRenderTarget 方法來建立 ID2D1HwndRenderTarget

RECT rc;
GetClientRect(m_hwnd, &rc);

D2D1_SIZE_U size = D2D1::SizeU(
    rc.right - rc.left,
    rc.bottom - rc.top
    );

// Create a Direct2D render target.
hr = m_pD2DFactory->CreateHwndRenderTarget(
    D2D1::RenderTargetProperties(),
    D2D1::HwndRenderTargetProperties(m_hwnd, size),
    &m_pRenderTarget
    );

下一個範例會使用 ID2D1HwndRenderTarget 將文字繪製到視窗。

//  Called whenever the application needs to display the client
//  window. This method writes "Hello, World"
//
//  Note that this function will automatically discard device-specific
//  resources if the Direct3D device disappears during function
//  invocation, and will recreate the resources the next time it's
//  invoked.
//
HRESULT DemoApp::OnRender()
{
    HRESULT hr;

    hr = CreateDeviceResources();

    if (SUCCEEDED(hr))
    {
        static const WCHAR sc_helloWorld[] = L"Hello, World!";

        // Retrieve the size of the render target.
        D2D1_SIZE_F renderTargetSize = m_pRenderTarget->GetSize();

        m_pRenderTarget->BeginDraw();

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

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

        m_pRenderTarget->DrawText(
            sc_helloWorld,
            ARRAYSIZE(sc_helloWorld) - 1,
            m_pTextFormat,
            D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
            m_pBlackBrush
            );

        hr = m_pRenderTarget->EndDraw();

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

    return hr;
}

此範例已省略程序代碼。

規格需求

需求
最低支援的用戶端 適用於 Windows Vista 的 Windows 7、Windows Vista SP2 和平臺更新 [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2008 R2、Windows Server 2008 SP2 和 Platform Update for Windows Server 2008 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 d2d1.h

另請參閱

ID2D1RenderTarget