首页 > 解决方案 > 创建 ID2D1RenderTerget 时出现问题

问题描述

我是 Direct2d 的新手,并试图创建创建渲染目标的函数(类似于 eigne 的一部分),但我对此有疑问。当我在 main 中创建渲染目标时,它可以工作,但不能在外部工作。我尝试创建的功能:

{
    RECT rc;
    GetClientRect(hwnd, &rc);

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

    // Create a Direct2D render target.
        factory->CreateHwndRenderTarget(
        D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(hwnd, size),
        &rendertarget);

}

编译错误:

错误(活动)E0304 重载函数“ID2D1Factory::CreateHwndRenderTarget”的实例与参数列表不匹配

错误 C2664 'HRESULT ID2D1Factory::CreateHwndRenderTarget(const D2D1_RENDER_TARGET_PROPERTIES &,const D2D1_HWND_RENDER_TARGET_PROPERTIES &,ID2D1HwndRenderTarget **)':无法将参数 1 从 'D2D1_RENDER_TARGET_PROPERTIES' 转换为 'const D2D1_PROPERTIES_REND

完整代码:

#include <d2d1.h>
#include <d2d1_1helper.h>
#include <wincodec.h>
#pragma comment(lib,"d2d1.lib")
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
    WPARAM wParam, LPARAM lParam);

ID2D1Factory* factory;
ID2D1HwndRenderTarget* rendertarget;

void test(ID2D1Factory* factory,ID2D1RenderTarget* rendertarget, HWND hwnd)
{
    RECT rc;
    GetClientRect(hwnd, &rc);

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

    // Create a Direct2D render target.
        factory->CreateHwndRenderTarget(
        D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(hwnd, size),
        &rendertarget);

}
int CALLBACK WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc;
    ZeroMemory(&wc, sizeof(wc));
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.lpszClassName = L"class";

    RegisterClass(&wc);
    HWND hwnd = CreateWindow(
        L"class",
        L"Direct2D init",
        WS_OVERLAPPEDWINDOW, 100, 100, 600, 600,
        NULL, NULL, hInstance, NULL);
    RECT clrc;
    GetClientRect(hwnd, &clrc);
    D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
    HRESULT hr = factory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(
            hwnd, D2D1::SizeU(clrc.right - clrc.left, clrc.bottom - clrc.top))
        ,&rendertarget);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    MSG msg;

    while (GetMessage(&msg, NULL, NULL, NULL))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);

    }
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_PAINT:
        rendertarget->BeginDraw();
        rendertarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
        rendertarget->EndDraw();
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}

标签: c++winapidirect2d

解决方案


正如我在评论中所说,您必须声明renderTarget为,ID2D1HwndRenderTarget*因为CreateHwndRenderTarget 方法需要这种类型,所以只需像这样更改您的test代码声明,例如:

void test(ID2D1Factory* factory, ID2D1HwndRenderTarget* rendertarget, HWND hwnd)

推荐阅读