문제

I have the Game class which at some point will have its private HWND member m_hWnd gain a value:

m_hWnd=CreateWindowEx(NULL,
            "Window Class",
            "Game", // Replace with gameName
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            NULL,
            NULL,
            hInst,
            this);

Later, an D2DResources object is created in which the game's m_hWnd must be passed on.

void Game::CreateRessources(HINSTANCE hInst)
{   
    CreateWindowClass(hInst); //m_hWnd gains its value it this function

    pMessageLog=CreateMessageLog();
    pD2DResources=CreateD2DResources(m_hWnd);
    pD2DResources->Initialize(m_hWnd);

    pWinsock=CreateWinsock();

}

Soon into pD2DResources->Initialize(m_hWnd) a RenderTarget is created:

HRESULT D2DResources::Initialize(HWND hwnd)
{
    HRESULT hr;

    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory); // Create factory

    RECT rc;
    GetClientRect(hwnd,&rc);

    if(SUCCEEDED(hr)) // Creates render target
    {
        pD2DFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(
                hwnd,
                D2D1::SizeU(
                    rc.right - rc.left,
                    rc.bottom - rc.top)),
                &pRT);
    }

It is this line which is most important:

pD2DFactory->CreateHwndRenderTarget(
                D2D1::RenderTargetProperties(),
                D2D1::HwndRenderTargetProperties(
                    hwnd,
                    D2D1::SizeU(
                        rc.right - rc.left,
                        rc.bottom - rc.top)),
                    &pRT);

The first argument of HwndRenderTargetProperties, the HWND, will be the HWND to which the render target issues the output from its drawing commands. In my code above, will the render target issues its order to the very same HWND that was created into the Game object or is it a copy? I get some bugs and perhaps it is because orders are issued to a wrong HWND, I don't know.

So, would the code above work (Would the render target issue its orders the the m_hWnd into the Game object) or not? If it doesn't work, what will be the solution? Thanks.

도움이 되었습니까?

해결책

It's the very same. Think of a HWND as a 'pointer' to the underlying OS resource. When you copy a HWND you are copying the 'pointer' to the resource, not the resource itself.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top