使用Direct2D绘制时钟的问题

问题描述 投票:0回答:1

我正在尝试使用Direct2D绘制时钟。该程序可以正确获取当前时间,但是错误是Direct2D绘制时钟时出现的,因为时钟似乎与当前时间已过时。要获取当前时间,请使用GetLocalTime()函数,然后映射这些值以将它们转换为一个角度,以便可以绘制它们。我该如何解决?

...

renderTarget->BeginDraw();
renderTarget->Clear(ColorF(ColorF::Black));

GetLocalTime(&sysTime);

wstring text = L"Hour: " + to_wstring(sysTime.wHour)
    + L"\nMinute: " + to_wstring(sysTime.wMinute)
    + L"\nSecond: " + to_wstring(sysTime.wSecond);

brush->SetColor(ColorF(ColorF::White));
renderTarget->DrawTextW(text.c_str(), text.length(), textFormat, textRect, brush);

D2D1_POINT_2F centerPoint = Point2F(320, 240);
FLOAT hourAngle = map(sysTime.wHour%12, 0, 12, 0, 360);
FLOAT minuteAngle = map(sysTime.wMinute, 0, 60, 0, 360);
FLOAT secondAngle = map(sysTime.wSecond, 0, 60, 0, 360);

brush->SetColor(ColorF(ColorF::DeepPink));
renderTarget->DrawEllipse(D2D1::Ellipse(centerPoint, 150, 150), brush, 5);

renderTarget->SetTransform(Matrix3x2F::Rotation(secondAngle, centerPoint));
brush->SetColor(ColorF(ColorF::Blue));
renderTarget->DrawLine(centerPoint,Point2F(centerPoint.x,centerPoint.y + 150*0.9), brush, 10,lineStrokeStyle);

renderTarget->SetTransform(Matrix3x2F::Rotation(minuteAngle, centerPoint));
brush->SetColor(ColorF(ColorF::White));
renderTarget->DrawLine(centerPoint,Point2F(centerPoint.x,centerPoint.y + 150*0.7), brush, 10, lineStrokeStyle);

renderTarget->SetTransform(Matrix3x2F::Rotation(hourAngle, centerPoint));
brush->SetColor(ColorF(ColorF::GreenYellow));
renderTarget->DrawLine(centerPoint, Point2F(centerPoint.x,centerPoint.y + 150*0.5), brush, 10,lineStrokeStyle);

renderTarget->SetTransform(Matrix3x2F::Identity());

HRESULT hrErr = renderTarget->EndDraw();

if (hrErr != S_OK) {
    MessageBox(hWnd, L"Direct2D Error", L"Direct2D Error", MB_OK | MB_ICONERROR);
    SafeRelease(&brush);
    SafeRelease(&renderTarget);
    SafeRelease(&factory);
}

...
c++ c winapi directx direct2d
1个回答
0
投票

是,您可以使用GetLocalTime获取当前的本地日期和时间。问题可能出在将时间映射到某个角度。

但是这些可以解决,因为MSDN中有非常详细的示例。

代替计算线的坐标,我们可以计算角度,然后应用旋转变换。以下代码显示了绘制一只时针的功能。 fAngle参数提供手的角度,以度为单位。

void Scene::DrawClockHand(float fHandLength, float fAngle, float fStrokeWidth)
{
    m_pRenderTarget->SetTransform(
        D2D1::Matrix3x2F::Rotation(fAngle, m_ellipse.point)
            );

    // endPoint defines one end of the hand.
    D2D_POINT_2F endPoint = D2D1::Point2F(
        m_ellipse.point.x,
        m_ellipse.point.y - (m_ellipse.radiusY * fHandLength)
        );

    // Draw a line from the center of the ellipse to endPoint.
    m_pRenderTarget->DrawLine(
        m_ellipse.point, endPoint, m_pStroke, fStrokeWidth);
}

此代码绘制一条垂直线,从时钟面的中心开始,到端点endPoint结束。通过应用旋转变换,直线绕椭圆的中心旋转。旋转的中心点是形成钟面的椭圆中心。

void Scene::RenderScene()
{
    m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue));

    m_pRenderTarget->FillEllipse(m_ellipse, m_pFill);
    m_pRenderTarget->DrawEllipse(m_ellipse, m_pStroke);

    // Draw hands
    SYSTEMTIME time;
    GetLocalTime(&time);

    // 60 minutes = 30 degrees, 1 minute = 0.5 degree
    const float fHourAngle = (360.0f / 12) * (time.wHour) + (time.wMinute * 0.5f);
    const float fMinuteAngle =(360.0f / 60) * (time.wMinute);

    DrawClockHand(0.6f,  fHourAngle,   6);
    DrawClockHand(0.85f, fMinuteAngle, 4);

    // Restore the identity transformation.
    m_pRenderTarget->SetTransform( D2D1::Matrix3x2F::Identity() );
}

参考:Drawing Clock Hands

调试:

1

整个代码示例:

© www.soinside.com 2019 - 2024. All rights reserved.