【发布时间】:2014-07-25 09:13:53
【问题描述】:
我正在使用带有 MFC 的 Direct2D,并且想知道如何使用实时数据来更新渲染目标。例如,我有以下 AFX_WM_DRAW2D 处理程序:
afx_msg LRESULT CTestView::OnDraw2d(WPARAM wParam, LPARAM lParam)
{
CString text;
CHwndRenderTarget* pRenderTarget = (CHwndRenderTarget*)lParam;
ASSERT_VALID(pRenderTarget);
// Clear window background
pRenderTarget->Clear(ColorF(ColorF::Beige));
// Draw text
CRect rect;
GetClientRect(rect);
text.Format(_T("%i"), value);
pRenderTarget->DrawText(text, rect, m_pBlueBrush, m_pTextFormat);
return TRUE;
}
变量“值”由定时器全局更新:
void CTestView::OnTimer(UINT_PTR nIDEvent)
{
CRect rect;
this->GetWindowRect(&rect);
this->InvalidateRect(&rect);
if (value == NULL)
value = 0;
value++;
CView::OnTimer(nIDEvent);
}
不幸的是,我似乎无法弄清楚如何使用通过 Direct2D 显示的更新变量来重绘界面。做这个的最好方式是什么?我读过 Direct2D 比 GDI 快得多,所以我想我会尝试使用它来处理不断更新的变量。
谢谢!
【问题讨论】:
-
Direct2D 几乎不会比 GDI 快,除非您已经拥有 Direct3D 应用程序。无论如何,GDI 和 Direct2D 重绘 UI 的逻辑是相同的:每当您的代码逻辑推断需要刷新 UI 时,调用
InvalidateRect,然后等待调用WM_PAINT-handler。 -
请显示您在哪里调用 OnDraw2d 的代码。我一直在开发具有相同要求的纯 win32 应用程序。只需调用绘图函数来响应 WM_PAINT 消息。
标签: c++ visual-c++ mfc direct2d