读取csv文件数据内容进行图形绘制(vc++描述)
根据csv文件数据内容绘图。
csv文件内容包括:图形类型(横线,竖线,横半圆弧,竖半圆弧),长度(线长度,半圆半径)。根据csv文件中提供的数据,在dialog中绘制出相应的图形。如果要绘制的图形超出dialog的 x轴边界,则从x=0,y不变的坐标开始绘制该图形;如果超出了y轴边界,则从x不变,y=0坐标开始绘制。(编码要求:从csv文件读取的内容和操作使用链表类保持)。
例如:csv文件内容为:(假设:a:横线,b:竖线,c:横半圆,d:竖半圆)
style,length
a, 50
b, 50
c, 25
d, 25
首先要定义头文件:
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
using namespace std;
vector<string> str_iterator ; //to deposit the data
在初始化里也可以加上如下语句:
m_fileName="d:\\recordset.csv";
UpdateData(FALSE);
然后在dialog中添加一个读取文件的按钮,并添加如下代码:
UpdateWindow();
str_iterator.clear();
fstream istrm; //define a I/O stream
string s;
int pos,i;
char *store;
string name;
name=m_fileName;
UpdateData(TRUE);
store=(char *)name.c_str();
istrm.open(store,ios::in); //open the file by only-read
if(!istrm)
{
MessageBox("Can't open file!\n");
}
else
{
while(!istrm.eof())
{
istrm>>s;
i=0;
int log=1;
while(log)
{
pos = s.find(",",i);
if(pos == string::npos)
{ //if can't find
str_iterator .push_back(s.substr(i));
log=0;
}
else //if find the character of ","
str_iterator .push_back(s.substr(i,pos-i));
i = pos+1;
}
}
str_iterator.pop_back();
istrm.close();
UpdateWindow(); //引用"OnPaint()"
}
最后在"OnPaint()"里面添加如下的处理代码:
UpdateWindow();
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(0,0,255));
CRect rect;
GetClientRect(&rect);
dc.MoveTo(5,46);
CPoint p;
int var;
char *ch;
CWnd *pWnd = GetDlgItem(IDC_STATIC);
CPen *pOldPen= dc.SelectObject(&pen);
int x=0;
while (x < str_iterator .size())
{
ch = (char*)str_iterator [x].c_str();
var = atoi((char*)str_iterator [x+1].c_str());
switch(*ch)
{
case 'a': // draw the horizontal line
{
p=dc.GetCurrentPosition();
if((p.x+var) > rect.right) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
p=dc.GetCurrentPosition();
dc.LineTo(p.x+var,p.y);
break;
}
case 'b': // draw the vertical line
{
p=dc.GetCurrentPosition();
if((p.y+var) > rect.bottom) // 判断是否越界
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.LineTo(p.x,p.y+var);
break;
}
case 'c': // draw the semicircle
{
p=dc.GetCurrentPosition();
if(((p.x+var*2) > rect.right)) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
if (((p.y+var) > rect.bottom))
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.ArcTo(p.x,p.y-var,(p.x+var*2),p.y+var,p.x,p.y,(p.x+var*2),p.y);
break;
}
case 'd': // draw the semicircle
{
p=dc.GetCurrentPosition();
if((p.x+var)> rect.right) // 判断是否越界
{
dc.MoveTo(5,p.y);
}
if((p.y+var*2)>rect.bottom)
{
dc.MoveTo(p.x,46);
}
p=dc.GetCurrentPosition();
dc.Arc(p.x-var,(p.y+var*2),p.x+var,p.y,p.x,(p.y+var*2),p.x,p.y);
dc.MoveTo(p.x,p.y+var*2);
break;
}
default:
break;
}
x = x+2;
}
dc.SelectObject(pOldPen);