VC++|VC++ MFC 画图 详解(附源码)

(1)新建 MFC aplication exe 文件:
【VC++|VC++ MFC 画图 详解(附源码)】painttool
(2)在CPaintoo1View类的 OnDraw方法 添加如下代码:
画图程序:
Code:

  1. void CPaintoo1View::OnDraw(CDC* pDC)
  2. {
  3. CPaintoo1Doc* pDoc = GetDocument();
  4. ASSERT_VALID(pDoc);
  5. // TODO: add draw code for native data here
  6. CPen pen_zuobixi,pen_sinx;
  7. pen_zuobixi.CreatePen(PS_SOLID,4,RGB(0,0,0));
  8. pen_sinx.CreatePen(PS_SOLID,2,RGB(0,0,255));
  9. pDC->SelectObject(&pen_zuobixi);
  10. //指定原点
  11. pDC->SetViewportOrg(200,255);
  12. pDC->SetTextColor(RGB(255,0,0));
  13. CString sPIText[] = {"-1","0"," 1" , "2","3","4","5","6","7","8","9","10","11"};
  14. int n= -1;
  15. int nTemp = 0;
  16. while(nTemp<=660)
  17. {pDC->LineTo(60*n,0);
  18. pDC->LineTo(60*n,-5);
  19. pDC->MoveTo(60*n,0);
  20. pDC->TextOut(60*n-sPIText[n+1].GetLength()*3,16,sPIText[n+1]);
  21. n++;
  22. nTemp+=60;
  23. }
  24. pDC->MoveTo(0,0);
  25. CString strTemp;
  26. for(n=-4,nTemp=0; nTemp<=180; n++,nTemp+=60){
  27. pDC->LineTo(0,60*n);
  28. pDC->LineTo(5,60*n);
  29. pDC->MoveTo(0,60*n);
  30. strTemp.Format("%d",-n);
  31. pDC->TextOut(10,60*n,strTemp);
  32. }
  33. double y,radian;
  34. pDC->SelectObject(&pen_sinx);
  35. for(int x=-60; x<600; x++)
  36. {
  37. radian = x/((double)6*2)*Pi;
  38. y=(radian*(-0.109417*radian+1.936310)+2.399982);
  39. pDC->MoveTo((int)x,(int)y);
  40. pDC->LineTo((int)x,(int)y);
  41. }
  42. pen_sinx.DeleteObject();
  43. }
运行结果为:


    推荐阅读