2007년 4월 8일 일요일

GDI+ coordinate transform

이 예제는 Visual C++.NET programming Bible - Young Jin 에서 발췌한 것이다.

// OnInitialUpdate override

void CGdiplusDemoView::OnInitialUpdate()
{
    CView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    SetTimer(0, 100, NULL);
}

// OnTimer override

void CGdiplusDemoView::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: Add your message handler code here and/or call default
    Invalidate();

    CView::OnTimer(nIDEvent);
}


#define R_SUN   50
#define R_EARTH 30
#define R_MOON  20

#define EARTH_TO_SUN    300
#define EARTH_TO_MOON   70

void CGdiplusDemoView::OnDraw(CDC* pDC)
{
    Graphics g(pDC->m_hDC);

    static int angleEarth, angleLunar;
    CRect rect;
    GetClientRect(rect);

    Pen pen(Color(255, 0, 0, 0), 3);
    SolidBrush brush(Color(255, 0, 0, 255));

    // Create font
    FontFamily fontFamily(L"Arial");
    Font font(&fontFamily, 12, FontStyleBold, UnitPixel);

    // Text output format
    StringFormat stringFormat;
    stringFormat.SetAlignment(StringAlignmentCenter);
    stringFormat.SetLineAlignment(StringAlignmentCenter);

    // Draw sun
    g.ResetTransform();
    g.TranslateTransform((REAL)rect.Width()/2, (REAL)rect.Height()/2);
    g.DrawEllipse(&pen, -R_SUN, -R_SUN, 2*R_SUN, 2*R_SUN);
    g.DrawString(L"SUN", 3, &font, PointF(0, 0), &stringFormat, &brush);

    // Draw earth
    g.RotateTransform((REAL)angleEarth);
    g.TranslateTransform(EARTH_TO_SUN, 0);
    g.DrawEllipse(&pen, -R_EARTH, -R_EARTH, 2*R_EARTH, 2*R_EARTH);
    g.DrawString(L"EARTH", 5, &font, PointF(0, 0), &stringFormat, &brush);

    // Draw moon
    g.RotateTransform((REAL)angleLunar);
    g.TranslateTransform(EARTH_TO_MOON, 0);
    g.DrawEllipse(&pen, -R_MOON, -R_MOON, 2*R_MOON, 2*R_MOON);
    g.DrawString(L"MOON", 4, &font, PointF(0, 0), &stringFormat, &brush);

    angleEarth++;
    angleLunar += 12;
}


댓글 없음: