工具:
http://blog.csdn.net/bcbobo21cn/article/details/44200205
demo工程:
http://pan.baidu.com/s/1kVEkDnT
1 新建单文档MFC exe 工程
2 添加菜单和菜单的消息处理函数
3 按下图代码运行,啥也没画出来;
下断点检查y的值,其值一直是-100,不会变;
因为radian定义为了int,重新定义为double,好了;另外还要把pDC->MoveTo()移动到循环之外;
4 添加四个菜单
5 主要代码
视类头文件添加
private:
int qvtype;
int w,h; //存储窗口宽和高
int w2,h2; //存储窗口宽和高的一半
视类预定义
#define PI 3.14159265359
菜单消息处理函数
void CMathqv1View::OnMenuitem32771()
{
// TODO: Add your command handler code here
qvtype=1;
Invalidate();
}
void CMathqv1View::OnMenuitem32772()
{
// TODO: Add your command handler code here
qvtype=2;
Invalidate();
}
void CMathqv1View::OnMenuitem32773()
{
// TODO: Add your command handler code here
qvtype=3;
Invalidate();
}
void CMathqv1View::OnMenuitem32774()
{
// TODO: Add your command handler code here
qvtype=4;
Invalidate();
}
视类OnDraw函数
void CMathqv1View::OnDraw(CDC* pDC)
{
CMathqv1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int x=0,y=0;
double radian;
CPen pen(PS_SOLID,1,RGB(255,0,0));//构造画笔对象
CPen *pOldPen;
pOldPen=pDC->SelectObject(&pen);
RECT rect1;
GetClientRect(&rect1);
w=rect1.right;
h=rect1.bottom;
w2=rect1.right/2;
h2=rect1.bottom/2;
switch(qvtype)
{
case 1: // 正弦
pDC->MoveTo((int)x,(int)h2);
for(x=0;x<w;x++)
{
//弧度=X坐标/曲线宽度*角系数*π
//Y坐标=振幅*曲线宽度*sin(弧度)
radian =x/((double)60*2)*PI;
y=sin(radian)*2*60;
pDC->LineTo(x,y+h2);
}
break;
case 2: // 余弦
pDC->MoveTo((int)x,(int)h2+120);
for(x=0;x<w;x++)
{
radian =x/((double)60*2)*PI;
y=cos(radian)*2*60;
pDC->LineTo(x,y+h2);
}
break;
case 3: // 正切
pDC->MoveTo((int)x,(int)h2);
for(x=0;x<w;x++)
{
radian =x/((double)60*2)*PI;
y=tan(radian)*2*60;
pDC->LineTo(x,y+h2);
}
break;
case 4: // 反正弦
pDC->MoveTo((int)x,(int)h2);
for(x=0;x<w;x++)
{
radian =x/((double)60*2);
y=asin(radian)*2*60;
pDC->LineTo(x,y+h2);
}
break;
}
pDC->SelectObject(pOldPen);//恢复先前的画笔
}
6 效果
正弦
余弦
正切
反正弦,碉堡了;画出一个如下错误曲线;反正弦记不清了;看来还要再上一下初中;
变更背景为黑色见
http://blog.csdn.net/bcbobo21cn/article/details/50957548