几种形状的图形转换实现
正方形-----> 圆形--->三角形-->正方形->
即实现这三种图形的循环转换。
步骤:
打开VC++6.0 ----à点击File--->New---à单击Projects--->MFC AppWizard(exe)->给工程命名
---->OK ----->点击Workspace上的ClassView这一栏-->鼠标右击View类
--->左键点击Add Member Variable
-->Type项---int , Name项---m_igraphics, Access项---Private--->OK
-->在View类的构造函数中,初始化m_igraphics = 0;
-->在View 类里的OnDraw()增加清除当前图形和判断当前图形语句,具体看程序1;
-->点击Add Windows Message handler..----->点击WM_CREATE-->点击Add Handler--->OK
-->点击Add Windows Message handler.. ----->点击WM_TIMER--->点击Add Handler--->OK
-->在View类中点击OnCreate(),添加代码,代码参考程序2;
-->在View类中点击OnTimer(),添加代码,代码参考程序3;
程序1:
在// TODO: add draw code for native date here添加如下代码:
// clear the current graphics
CBrush brush;
Brush.CreateSolidBrush(RGB(255, 255, 255));
CRect myrect(0, 0, 1024, 1024);
pDC->FillRect(myrect, &brush);
// Judge the current grahics
If (m_igraphics == 1)
{
// draw rect
pDC->Rectange(20, 20, 89, 89);
}
else if (m_igraphics == 2)
{
// draw circle
pDC->Ellipse(20, 20, 89, 89);
}
else
{
// draw triangle
pDC->MoveTo(20, 20);
pDC->LineTo(89, 45);
pDC->LineTo(20, 89);
pDC->LineTo(20, 20);
}
程序2:
在// TODO: add draw code for native date here添加如下代码:
// Set looping time
SetTimer(1, 750, NULL);
程序3:
在// TODO: add draw code for native date here添加如下代码:
// get pointer pdc
CDC *pDC = GetDC();
// call OnDraw() for redraw
OnDraw(pDC);
// make m_ituxing for looping
M_igraphics = (m_igraphics + 1) % 3;