MFC下绘制正弦波的曲线图。
波形截图如下:
绘制曲线部分的代码如下:
//new wave pDC = pGuiDlg->m_new_wave_control.GetDC(); pGuiDlg->m_new_wave_control.GetClientRect(&rect); //set background color pDC->SetViewportOrg(rect.left, rect.top); pDC->SetTextColor(RGB(255,0,0));//red pDC-> FillSolidRect(rect.left, rect.top, rect.right, rect.bottom, RGB(0,0,0)); //set cordinate line color CPen m_input_cordi_line_pen; m_input_cordi_line_pen.CreatePen(PS_SOLID,2,RGB(255, 0, 0)); pDC->SelectObject(&m_input_cordi_line_pen); //x coordinate pDC->SetViewportOrg(rect.left, rect.bottom/2); n = 0; nTemp=0; while (nTemp <= rect.right) { pDC->LineTo(60*n,0); pDC->LineTo(60*n,-5); pDC->MoveTo(60*n,0); pDC->TextOut(60*n-sPIText[n+1].GetLength()*3,16,sPIText[n+1]); n++; nTemp +=60; } //last point pDC->LineTo(rect.right, 0); pDC->MoveTo(rect.left, rect.bottom/2); //Y coordinate pDC->SetDCPenColor(RGB(255,0,0)); index = -10; nTemp = 0; for(n = 0; nTemp <= rect.bottom/2; n++, nTemp += 10) { pDC->LineTo(rect.left,index * n); pDC->LineTo(5, index * n); pDC->MoveTo(rect.left, index * n); //strTemp.Format("%s", -n); pDC->TextOut(10,index*n,strTemp); } pDC->MoveTo(rect.left, rect.bottom/2); index = -10; nTemp = 0; for(n = 0; nTemp <= rect.bottom/2; n++, nTemp += 10) { pDC->LineTo(rect.left, (index * n + rect.bottom/2)); pDC->LineTo(5, (index * n + rect.bottom/2)); pDC->MoveTo(rect.left, (index * n + rect.bottom/2));; } //set line color CPen m_input_line_pen; m_input_line_pen.CreatePen(PS_SOLID,2,RGB(255, 255, 0)); //yellow pDC->SelectObject(&m_input_line_pen); pDC->SetViewportOrg(rect.left, rect.bottom/2); fp = fopen(FileName, "wb"); if(fp == NULL) { MessageBox(_T("无效文件!")); return; } int h = 0; char temp_value = 0x00; for (double i = 0; i < ((double)rect.right)/19.1; i = i + 0.00005) { pDC->LineTo(i*19.1,(char)(sin((double)i) * 80)); temp_value = (char)(sin((double)(i)) * 80); if (h < 800*1024) { fwrite(&temp_value, sizeof(char), 1, fp); } } fclose(fp); return true;程序运行时,另开一个线程负责该波形绘制,同时把相应正弦波数据写入文件保存。