IDCompositionDevice::CreateTargetForHwnd 方法 (dcomp.h)

建立組合目標物件,該物件系結至指定視窗控制碼所代表的視窗, (HWND) 。

語法

HRESULT CreateTargetForHwnd(
  [in]  HWND                hwnd,
  [in]  BOOL                topmost,
  [out] IDCompositionTarget **target
);

參數

[in] hwnd

類型: HWND

應該系結組合目標物件的視窗。 此參數不得為 Null。

[in] topmost

類型: BOOL

如果視覺化樹狀結構應該顯示在 hwnd 參數所指定視窗的子系上,則為 TRUE;否則,視覺化樹狀結構會顯示在子系後面。

[out] target

類型: IDCompositionTarget**

新的組合目標物件。 此參數不得為 Null。

傳回值

類型: HRESULT

如果函式成功,則會傳回S_OK。 否則,它會傳回 HRESULT 錯誤碼。 如需錯誤碼清單,請參閱 DirectComposition 錯誤碼

備註

Microsoft DirectComposition 視覺化樹狀結構必須先系結至視窗,才能在螢幕上顯示任何專案。 視窗可以是最上層視窗或子視窗。 不論是哪一種情況,視窗都可以是分層視窗,但在所有情況下,視窗都必須屬於呼叫進程。 如果視窗屬於不同的進程,這個方法會 傳回DCOMPOSITION_ERROR_ACCESS_DENIED

當 DirectComposition 內容組成視窗時,內容一律會透過GetDC函式所傳回的裝置內容) (,或呼叫 Microsoft DirectX Present方法,直接繪製到該視窗的任何內容之上撰寫。 不過,因為視窗裁剪規則適用于 DirectComposition 內容,如果視窗有子視窗,這些子視窗可能會裁剪視覺化樹狀結構。 最上層參數會決定子視窗是否裁剪視覺化樹狀結構。

在概念上,每個視窗都包含四個層級:

  1. 直接繪製到視窗控制碼的內容 (這是最底層) 。
  2. 選擇性的 DirectComposition 視覺化樹狀結構。
  3. 所有子視窗的內容,如果有的話。
  4. 另一個選擇性的 DirectComposition 視覺化樹狀結構 (這是最上層) 。
這四個圖層都會裁剪到視窗的可見區域。

最多隻能針對系統中的每個視窗建立兩個組合目標,一個最上層,一個不是最上層。 如果組合目標已經系結至指定圖層的指定視窗,這個方法就會失敗。 當組合目標物件終結時,它所撰寫的圖層可供新的組合目標物件使用。

範例

下列範例會建立並初始化裝置物件,然後將裝置物件系結至組合目標視窗。

#include <dcomp.h>
#include <d3d11.h>

HRESULT InitializeDirectCompositionDevice(HWND hwndTarget, 
        ID3D11Device **ppD3D11Device, IDCompositionDevice **ppDevice,
        IDCompositionTarget **ppCompTarget)
{
    HRESULT hr = S_OK;
    D3D_FEATURE_LEVEL featureLevelSupported;
    IDXGIDevice *pDXGIDevice = nullptr;

    // Verify that the arguments are valid.
    if (hwndTarget == NULL || ppD3D11Device == nullptr || ppDevice == nullptr || 
                            ppCompTarget == nullptr)
    {
        return E_INVALIDARG;
    }

    // Create the D3D device object. Note that the 
    // D3D11_CREATE_DEVICE_BGRA_SUPPORT flag is needed for rendering 
    // on surfaces using Direct2D. 
    hr = D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        NULL,
        D3D11_CREATE_DEVICE_BGRA_SUPPORT, // needed for rendering on surfaces using Direct2D
        NULL,
        0,
        D3D11_SDK_VERSION,
        ppD3D11Device,
        &featureLevelSupported,
        NULL);

    if (SUCCEEDED(hr))
    {
        // Create the DXGI device used to create bitmap surfaces.
        hr = (*ppD3D11Device)->QueryInterface(&pDXGIDevice);
    }

    if (SUCCEEDED(hr))
    {
        // Create the DirectComposition device object.
        hr = DCompositionCreateDevice(pDXGIDevice, __uuidof(IDCompositionDevice), 
                reinterpret_cast<void **>(ppDevice));
    }

    if (SUCCEEDED(hr))
    {
        // Bind the DirectComposition device to the target window.
        hr = (*ppDevice)->CreateTargetForHwnd(hwndTarget, TRUE, ppCompTarget);   
    }

    return hr;
}

需求

   
最低支援的用戶端 Windows 8 [僅限傳統型應用程式]
最低支援的伺服器 Windows Server 2012 [僅限傳統型應用程式]
目標平台 Windows
標頭 dcomp.h
程式庫 Dcomp.lib
Dll Dcomp.dll

另請參閱

組合目標視窗

IDCompositionDevice

IDCompositionTarget

IDCompositionTarget::SetRoot

IDCompositionVisual