登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

读取csv文件数据内容进行图形绘制(vc++描述)  

2009-07-29 19:17:21|  分类: VC++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

读取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);

 

  评论这张
 
阅读(1918)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018