强制重绘Direct2D

时间:2018-07-08 14:02:19

标签: c++ mfc direct2d

我在用于绘图的MFC C ++应用程序中有一个组合框。现在,我正在尝试使用Direct2D进行绘制。但是,我不能在初始绘制后重新绘制内容。使用下面的代码,当应用程序启动并且Direct2D内容按预期显示时,将调用我的“ OnAfxDraw2D”方法。

稍后,当我想将组合框返回到初始状态时,我调用DoD2D()方法。如果使用第一个选项实现该方法,则该方法将按预期工作,但有时会在Visual Studio中产生断言错误(Winhand.cpp第208行)。如果我使用DoD2D()方法的第二个选项-则什么也不会发生(未绘制图形内容)。

我在这里想念东西吗?

RenderArea::RenderArea() : CButton()
{
    BOOL bUseDCRenderTarget = TRUE;
    EnableD2DSupport(TRUE, bUseDCRenderTarget);
}

void RenderArea::DoDataExchange(CDataExchange* pDX)
{
    CButton::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(RenderArea, CButton)
    ON_REGISTERED_MESSAGE(AFX_WM_DRAW2D, &RenderArea::OnAfxDraw2D)
END_MESSAGE_MAP()

void RenderArea::DoD2D()
{
    if (!IsD2DSupportEnabled())
        return;

    // Option 1) Works, but fails an assertion in debug mode
    Invalidate();
    return;

    // Option 2) Does not work.
    CDCRenderTarget *pRenderTarget = GetDCRenderTarget();
    pRenderTarget->BeginDraw();
    DrawD2DResource(pRenderTarget);
    pRenderTarget->EndDraw();
}

void RenderArea::DrawD2DResource(CDCRenderTarget* pDCRenderTarget)
{
    ASSERT_VALID(pDCRenderTarget);

    D2D1_COLOR_F crBack =
        CRenderTarget::COLORREF_TO_D2DCOLOR(::GetSysColor(COLOR_WINDOW));
    pDCRenderTarget->Clear(crBack);

    CD2DSizeF sizeTrans(0.f, 105.f);
    pDCRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(sizeTrans));

    CD2DEllipse ellipse(CD2DRectF(10.f, 10.f, 200.f, 100.f));
    CD2DSolidColorBrush brush(pDCRenderTarget, ::GetSysColor(COLOR_WINDOWTEXT));
    pDCRenderTarget->DrawEllipse(ellipse, &brush);
    CD2DPointF pointFrom(10.f, 10.f), pointTo(200.f, 100.f);
    pDCRenderTarget->DrawLine(pointFrom, pointTo, &brush);

    pDCRenderTarget->SetTransform(D2D1::IdentityMatrix());

    return;
}

LRESULT RenderArea::OnAfxDraw2D(WPARAM wParam, LPARAM lParam)
{
    CDCRenderTarget* pDCRenderTarget = (CDCRenderTarget*)lParam;
    ASSERT_KINDOF(CDCRenderTarget, pDCRenderTarget);

    DrawD2DResource(pDCRenderTarget);

    HRESULT hr = pDCRenderTarget->EndDraw();

    return static_cast<LRESULT>(TRUE);
}

0 个答案:

没有答案