如何在MFC应用程序中创建与pRenderTarget兼容的几何对象?也就是说,我有:
LRESULT CWorkSpaceBar::OnDrawDirect2D(WPARAM wParam, LPARAM lParam)
{
CHwndRenderTarget* pRenderTarget = (CHwndRenderTarget*)lParam;
pRenderTarget->FillGeometry(//<<-- here I need a geometry, i.e. an Ellipse Geometry
}
我试过了:
ID2D1EllipseGeometry *m_pEllipseGeometry = nullptr;
ID2D1Factory *pFactory = nullptr;
m_pEllipseGeometry->GetFactory(&pFactory);
pFactory->CreateEllipseGeometry(ellipse, &m_pEllipseGeometry);
pRenderTarget->FillGeometry(m_pEllipseGeometry, m_pBlackBrush);
唉,我收到错误的说法:
错误C2664&#39; void CRenderTarget :: FillGeometry(CD2DGeometry *,CD2DBrush *,CD2DBrush *)&#39;:
无法从&#39; ID2D1EllipseGeometry *&#39;转换参数1至 &#39; CD2DGeometry *&#39;
我理解与类型存在冲突,但是从我设法检查的是没有继承自CD2DGeometry的MFC类,CD2DGeometry是一个抽象类,pRenderTarget期望CD2DGeometry *作为它的第一个论点。
我真的很困惑。
任何帮助真的很感激。
答案 0 :(得分:0)
ID2D1EllipseGeometry *m_pEllipseGeometry = nullptr;
ID2D1Factory *pFactory = nullptr;
m_pEllipseGeometry->GetFactory(&pFactory);
这会失败,因为m_pEllipseGeometry
是nullptr
。要使用MFC,请尝试以下示例:
pRenderTarget->Clear({ 1, 1, 1, 1 });
CRect rc(0, 0, 100, 100);
CD2DRectF d2d_rect(rc);
CD2DEllipse ellipse(&d2d_rect);
CD2DSolidColorBrush brush(pRenderTarget, { 0.5f, 0.5f, 0.5f, 1 });
pRenderTarget->FillEllipse(ellipse, &brush);
查看头文件,似乎MFC根本不支持ID2D1EllipseGeometry
。要访问pFactory
,请使用以下示例:
if (SUCCEEDED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory))
{
...
pFactory->Release();
}
还有AfxGetD2DState()->GetDirect2dFactory()
。您可以使用CreateHwndRenderTarget
获取ID2D1HwndRenderTarget