c++ - 创建 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);
}
}
解决方案
正如我在评论中所说,您必须声明renderTarget
为,ID2D1HwndRenderTarget*
因为CreateHwndRenderTarget 方法需要这种类型,所以只需像这样更改您的test
代码声明,例如:
void test(ID2D1Factory* factory, ID2D1HwndRenderTarget* rendertarget, HWND hwnd)
推荐阅读
- performance - Python-请求在大响应时变慢
- testing - 带有gorm INSERT的sqlmock
- windows-10 - 在 LaTeX 构建上覆盖旧 PDF (Sublime 3)
- java - 调用方法时的异常处理
- swift - Swift - 使用枚举映射 UISegmentedControl
- c# - 为 Kentico 对象生成类
- javascript - .eslintrc.js 键带有“-”(破折号)
- amazon-ec2 - GPU 利用率如何在神经网络训练的背景下发挥作用?
- android - 带有 createChooser 的 startActivity 不显示选择器但立即启动
- sql - Rails has_many through - 如何为连接表指定键列?