创建 ID2D1RenderTarget 时出现问题
Problen while creating 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);
}
编译错误:
Error (active) E0304 no instance of overloaded function "ID2D1Factory::CreateHwndRenderTarget" matches the argument list
Error C2664 'HRESULT ID2D1Factory::CreateHwndRenderTarget(const D2D1_RENDER_TARGET_PROPERTIES &,const D2D1_HWND_RENDER_TARGET_PROPERTIES &,ID2D1HwndRenderTarget **)': cannot convert argument 1 from 'D2D1_RENDER_TARGET_PROPERTIES' to 'const D2D1_RENDER_TARGET_PROPERTIES *'
完整代码:
#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);
}
}
正如我在评论中所说,你必须将 renderTarget
声明为 ID2D1HwndRenderTarget*
因为 CreateHwndRenderTarget method 需要这种类型,所以只需像这样更改你的 test
代码声明例如:
void test(ID2D1Factory* factory, ID2D1HwndRenderTarget* rendertarget, HWND hwnd)
我是 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);
}
编译错误:
Error (active) E0304 no instance of overloaded function "ID2D1Factory::CreateHwndRenderTarget" matches the argument list
Error C2664 'HRESULT ID2D1Factory::CreateHwndRenderTarget(const D2D1_RENDER_TARGET_PROPERTIES &,const D2D1_HWND_RENDER_TARGET_PROPERTIES &,ID2D1HwndRenderTarget **)': cannot convert argument 1 from 'D2D1_RENDER_TARGET_PROPERTIES' to 'const D2D1_RENDER_TARGET_PROPERTIES *'
完整代码:
#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);
}
}
正如我在评论中所说,你必须将 renderTarget
声明为 ID2D1HwndRenderTarget*
因为 CreateHwndRenderTarget method 需要这种类型,所以只需像这样更改你的 test
代码声明例如:
void test(ID2D1Factory* factory, ID2D1HwndRenderTarget* rendertarget, HWND hwnd)