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
//弧度=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
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
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
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