
void CCOMTOOLDlg::OnButtonXiantu()
{
CPaintDC dc(this);
CRect rect;
GetDlgItem(IDC_STATIC_PAINT)->GetClientRect(rect);
CWnd *pWnd = GetDlgItem(IDC_STATIC_PAINT);
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetViewportOrg(0,0);
pDC->SetViewportExt(rect.right,rect.bottom);
pDC->SetWindowOrg(0,0);
pDC->SetWindowExt(580,310);
double w;
CString strcai;
GetDlgItemText(IDC_EDIT_CAIYANG,strcai);
if (strcai=="")
AfxMessageBox("請輸入采樣間隔!");
else
w=atof(strcai);
// double w=0.5;//設定采樣速率
double n=540.0/380.0;
double m=270.0/6.0;
int ns=int(360.0/w);
rect.Width();
rect.Height();
//用rect.Width()替代nxLength,rect.Height()替代nyLength
int nxLength=rect.Width();
int nyLength=rect.Height();
int nxDivisionNumber = 18;
int nyDivisionNumber = 5;
int nMeshLength;
CString str;
int t;
pDC->Rectangle(0,0,nxLength,nyLength);
CPen* pPenRed = new CPen; //創建畫筆物件
pPenRed->CreatePen(PS_SOLID, 1, RGB(255,0,0)); //紅色畫筆
CPen* pPenGreen = new CPen; //創建畫筆物件
pPenGreen->CreatePen(PS_SOLID, 1, RGB(0,255,0));//綠色畫筆
CPen* pPenBlue = new CPen; //創建畫筆物件
pPenBlue->CreatePen(PS_DOT, 1, RGB(0,0,255)); //藍色畫筆
CPen* pPenBlack = new CPen; //創建畫筆物件
pPenBlack->CreatePen(PS_SOLID, 1, RGB(0,0,0)); //黑色畫筆
//選中黑色畫筆,并保存當前畫筆
CGdiObject* pOldPen = pDC->SelectObject(pPenBlack);
pDC->MoveTo(30,10); //繪制坐標軸
pDC->LineTo(30,nyLength-30); //垂直軸,(30,nyLength-30)是客戶坐標原點
pDC->LineTo(nxLength-10,nyLength-30); //水平軸
//橫坐標箭頭
pDC->MoveTo(nxLength-10-10,nyLength-30-3);
pDC->LineTo(nxLength-10,nyLength-30);
pDC->LineTo(nxLength-10-10,nyLength-30+3);
pDC->LineTo(nxLength-10-10,nyLength-30-3);
//縱坐標箭頭
pDC->MoveTo(27,20);
pDC->LineTo(30,10);
pDC->LineTo(33,20);
pDC->LineTo(27,20);
//橫坐標數字
nMeshLength = (nxLength-40)/(nxDivisionNumber+1);
for (t=0; t<nxDivisionNumber+1; t++){
str.Format("%d",t*20);
pDC->TextOut((27+t*nMeshLength), nyLength-27, str);
}
//橫坐標標識
str.Format("角度");
pDC->TextOut(nxLength-33,nyLength-27,str);
//縱坐標數字
nMeshLength = (nyLength-40)/(nyDivisionNumber+1);
for (t=0; t<nyDivisionNumber+1; t++){
str.Format("%d",t);
pDC->TextOut(3, nyLength-40-t*nMeshLength, str);
}
//縱坐標標識
str.Format("電壓值");
pDC->TextOut(1,3,str);
//網格線
pDC->SelectObject(pPenBlue);
nMeshLength = (nxLength-40)/(nxDivisionNumber+1);
for (t=1; t<nxDivisionNumber+2; t++){
pDC->MoveTo(30+t*nMeshLength,nyLength-30);
pDC->LineTo(30+t*nMeshLength,10);
}
nMeshLength = (nyLength-40)/(nyDivisionNumber+1);
for (t=1; t<nyDivisionNumber+2; t++){
pDC->MoveTo(30,nyLength-30-t*nMeshLength);
pDC->LineTo(nxLength-10,nyLength-30-t*nMeshLength);
}
// TODO: Add your control notification handler code here
// CString stredit;
// GetDlgItem(IDC_EDIT_OPEN)->GetWindowText(stredit);
CString strdata1,strdata2,strdata3,strdata4;//用于暫時存放截取的列的值
int index;
int i=0;
int line = 0;//記錄行號
double *data1;
data1=new double[ns];//存放第一列結果
double *data2;
data2=new double[ns];//存放第二列結果
double *data3;
data3=new double[ns];//存放第三列結果
double *data4;
data4=new double[ns]; //存放第四列結果
//讀取一行內容存放在strLine中
//int nLineCount = m_Receive.GetLineCount();
int nLineCount = m_open.GetLineCount();//獲得編輯控制元件的行數
CString strText, linebuf;
// Dump every line of text of the edit control.
for (i=1; i < nLineCount; i++)
{
// length of line i:
// int len = m_Receive.LineLength(m_Receive.LineIndex(i));
int len = m_open.LineLength(m_open.LineIndex(i));//LineLength獲得編輯控制元件中行的長度,LineIndex獲得在多行編輯控制元件中的某行的字符索引
//m_Receive.GetLine(i, strText.GetBuffer(len), len);
m_open.GetLine(i, strText.GetBuffer(len), len);//Getline獲得編輯控制元件的一行
strText.ReleaseBuffer(len);//ReleaseBuffer釋放GetBuffer分配的動態記憶體
// linebuf.Format(_T("line %d: '%s'\n"), i, strText);
linebuf.Format(_T("%s\n"), strText);
//AfxMessageBox(linebuf);
//AFXDUMP(linebuf);//輸出得每行資料
//獲取到第一個tab鍵的索引
index = strText.FindOneOf(TEXT(" "));
//取得第一列資料
strdata1 =strText.Left(8);
//取到后洗掉所取內容
strText.Delete(0,index);
//去掉空格
strText.TrimLeft();
index = strText.FindOneOf(TEXT(" "));
//取到后洗掉所取內容
strText.Delete(0,index);
//去掉空格
strText.TrimLeft();
//第二列資料
index = strText.FindOneOf(TEXT(" "));
strdata2 = strText.Left(8);
strText.Delete(0,index);
strText.TrimLeft();
index = strText.FindOneOf(TEXT(" "));
//取到后洗掉所取內容
strText.Delete(0,index);
//去掉空格
strText.TrimLeft();
//第三列資料
index = strText.FindOneOf(TEXT(" "));
strdata3 = strText.Left(8);
strText.Delete(0,index);
strText.TrimLeft();
index = strText.FindOneOf(TEXT(" "));
//取到后洗掉所取內容
strText.Delete(0,index);
//去掉空格
strText.TrimLeft();
//第四列資料,取到第一個空格位置的索引
index = strText.FindOneOf(TEXT("\n"));
strdata4 = strText.Left(8);
strText.Delete(0,index);
strText.TrimLeft();
data1[i] = (double)atof(strdata1)*m; // data1[i] = atof(strdata1)*m;//100.0
data2[i] = (double)atof(strdata2)*m; // data2[i] = atof(strdata2)*m;
data3[i] = (double)atof(strdata3)*m; // data3[i] = atof(strdata3)*m;
data4[i] = (double)atof(strdata4)*m; // data4[i] = atof(strdata4)*m;
}
pDC->SelectObject(pPenRed);//選取紅色畫筆
pDC->MoveTo(30,nyLength-30-data1[0]);
for (i=1;i<nLineCount-1;i++)
{
pDC->LineTo(30+w*n*i,nyLength-30-data1[i]);
}
pDC->MoveTo(30+w*n*ns,nyLength-30-data1[nLineCount]);
pDC->SelectObject(pPenGreen);//選取綠色畫筆
pDC->MoveTo(30,nyLength-30-data2[0]);
for (i=1;i<nLineCount-1;i++)
{
pDC->LineTo(30+w*n*i,nyLength-30-data2[i]);
}
pDC->MoveTo(30+w*n*ns,nyLength-30-data2[nLineCount]);
pDC->SelectObject(pPenBlue);//選取藍色畫筆
pDC->MoveTo(30,nyLength-30-data3[0]);
for (i=1;i<line-1;i++)
{
pDC->LineTo(30+w*n*i,nyLength-30-data3[i]);
}
pDC->MoveTo(30+w*n*ns,nyLength-30-data3[nLineCount]);
pDC->SelectObject(pPenBlack);//選取黑色畫筆
pDC->MoveTo(30,nyLength-30-data4[0]);
for (i=1;i<nLineCount-1;i++)
{
pDC->LineTo(30+w*n*i,nyLength-30-data4[i]);
}
pDC->MoveTo(30+w*n*ns,nyLength-30-data4[nLineCount]);
pDC->SelectObject(pOldPen);
delete pPenRed;
delete pPenGreen;
delete pPenBlue;
delete pPenBlack;
delete [] data1;
delete [] data2;
delete [] data3;
delete [] data4;
}
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124444.html
標籤:基礎類
