【问题标题】:Is this the correct way to assign a class' private HWND to another class?这是将类的私有 HWND 分配给另一个类的正确方法吗?
【发布时间】:2013-03-17 23:17:26
【问题描述】:

我有一个 Game 类,它的私有 HWND 成员 m_hWnd 会获得一个值:

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

稍后,将创建一个 D2DResources 对象,其中必须传递游戏的 m_hWnd。

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();

}

很快进入 pD2DResources->Initialize(m_hWnd) 一个 RenderTarget 被创建:

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);
    }

最重要的是这一行:

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

HwndRenderTargetProperties 的第一个参数 HWND 将是渲染目标从其绘图命令发出输出的 HWND。在我上面的代码中,渲染目标会将其顺序发布到在 Game 对象中创建的相同 HWND 还是副本?我遇到了一些错误,可能是因为命令发给了错误的 HWND,我不知道。

那么,上面的代码是否可以工作(渲染目标是否会将 m_hWnd 命令发送到 Game 对象中)?如果它不起作用,解决方案是什么?谢谢。

【问题讨论】:

    标签: c++ windows winapi pointers hwnd


    【解决方案1】:

    完全一样。将 HWND 视为指向底层操作系统资源的“指针”。当您复制 HWND 时,您将“指针”复制到资源,而不是资源本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2019-02-11
      • 2015-08-17
      相关资源
      最近更新 更多