MFC应用程序绘方格图

1. File——>New Project——>C++——>MFC Application

2. Next——>single document——>Finish

3. Resource View——>[name]——>[name].rc——>Menu——>双击IDR_MAINFRAME

4. 在help右侧的type here框中输入Graphic——>在下拉菜单中输入Graphic1——>

右击Graphic1,Add Event Handler——>左侧默认Command,右侧选C[name]View——>Add and edit

5. 引入math.h头文件:#include "math.h";

在void CfirstAppView::OnGraphicGraphic1()
{
    // TODO: Add your command handler code here
}中,注释下方添加下列代码:

int i,j,px,py;
    double x,y,a;
    const double PI=3.1416;
    CClientDC *pdc=new CClientDC(this);
    CPen pen;
    pen.CreatePen(PS_SOLID,1,RGB(0xff,0,0));  //红色
    CPen *oldpen=(CPen*)pdc->SelectObject(&pen);
    i=0;j=0;
    for(px=140;px<=470;px+=60)
    {
        i+=1;
        for(py=10;py<=350;py+=60)
        {
            if(j==6)
                j=1;
            else
                j+=1;
            if((i%2)==(j%2))
            {
                for(a=0;a<=PI;a+=PI/15)
                {
                    y=60-60*sin(a);
                    pdc->MoveTo((int)px,py+(int)y);
                    pdc->LineTo(px+60,py+(int)y);
                }
            }
            else
            {
                for(a=0;a<=PI;a+=PI/25)
                {
                    x=30-30*cos(a);
                    pdc->MoveTo(px+(int)x,py);
                    pdc->LineTo(px+(int)x,py+60);
                }
            }
        }
    }
    pdc->DeleteDC();

6. 运行后,按Graphic——>Graphic1,出现下图:

image

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s