To do shapes in the form I use a vector that stores a struct with the x, y, color and shape values of every shape I put on the form.
I have to save and open these values.
To save and open files with shapes.
I use a TOpenDialog and a TSaveDialog.
I save the values fine, but when I have to write the open code I try that the same vector receive the saved values but it doesn't accept it.
It sends an exception with the vector.
I had to use 4 vectors, one for the x, other for the y, other for the color and other for the shape.
Could you tell me how can make it with one vector only?
#include <vcl.h>
#pragma hdrstop
#include "coordenadas.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BGuardarClick(TObject *Sender)
{
AnsiString archivo;
{
if(DGuardar->Execute()){
archivo=DGuardar->FileName;
ofstream outfile(archivo.c_str(),ios::binary); //abrir archivo para escritura
tam=vdata.size();
outfile.write((char*)&tam,sizeof(tam));
TShape *fig;
fig = new TShape(this);
fig->Parent =Form1;
fig->Left=X;
fig->Top=Y;
fig->Height=20;
fig->Width=20;
for(int k=0;k<vdata.size();k++){
elemento=vdata[k].X;
outfile.write((char*)&(elemento), sizeof(elemento));
}
for(int k=0;k<vdata.size();k++){
elemento1=vdata[k].Y;
outfile.write((char*)&(elemento1), sizeof(elemento1));
}
for(int k=0;k<vdata.size();k++){
elemento2=vdata[k].color;
outfile.write((char*)&(elemento2),sizeof(elemento2));
}
for(int k=0;k<vdata.size();k++){
elemento3=vdata[k].forma;
outfile.write((char*)&(elemento3),sizeof(elemento3));
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BAbrirClick(TObject *Sender)
{
AnsiString archivo;
if(DAbrir->Execute()){
archivo=DAbrir->FileName;
ifstream infile(archivo.c_str(),ios::binary); //abrir archivo para lectura
num.erase(num.begin(),num.end());
num2.erase(num2.begin(),num2.end());
num3.erase(num3.begin(),num3.end());
num4.erase(num4.begin(),num4.end());
vdata.erase(vdata.begin(),vdata.end());
vf.erase(vf.begin(),vf.end());
infile.read((char*)&tam,sizeof(int));
{
for(int k=0;k<tam;k++){
infile.read((char*)&elemento,sizeof(elemento));
num.insert(num.end(),elemento);
}
for(int k=0;k<tam;k++){
infile.read((char*)&elemento1,sizeof(elemento1));
num2.insert(num2.end(),elemento1);
}
for(int k=0;k<tam;k++){
infile.read((char*)&elemento2,sizeof(elemento2));
num3.insert(num3.end(),elemento2);
}
for(int k=0;k<tam;k++){
infile.read((char*)&elemento3,sizeof(elemento3));
num4.insert(num4.end(),elemento3);
}
}
for(k=0;k<tam;k++){
TShape *fig;
fig = new TShape(this);
fig->Parent=Form1;
fig->Left=num[k];
fig->Top=num2[k];
fig->Height=20;
fig->Width=20;
fig->Brush->Color=num3[k];
fig->Shape=num4[k];
vf.insert(vf.end(),fig);
}
}
}
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TShape *fig;
fig = new TShape(this);
fig->Parent =Form1;
fig->Left=X;
fig->Top=Y;
fig->Height=20;
fig->Width=20;
if(TBCirculo->Down){
fig->Shape=stCircle;
fig->Brush->Color=clLime;
}
else{
fig->Shape=stRectangle;
fig->Brush->Color=clLime;
}
if(TBYellow->Down){
fig->Brush->Color=clYellow;
}
else if(TBOlive->Down){
fig->Brush->Color=clOlive;
}
else if (TBNavy->Down){
fig->Brush->Color=clNavy;
}
else if(TBTeal->Down){
fig->Brush->Color=clTeal;
}
else if(TBAqua->Down){
fig->Brush->Color=clAqua;
}
else if(TBPurple->Down){
fig->Brush->Color=clPurple;
}
else if(TBRed->Down){
fig->Brush->Color=clRed;
}
figu.X=fig->Left;
figu.Y=fig->Top;
figu.color=fig->Brush->Color;
figu.forma=fig->Shape;
Lx->Items->Add(figu.X);
Ly->Items->Add(figu.Y);
Lcolor->Items->Add(figu.color);
Lforma->Items->Add(figu.forma);
vdata.insert(vdata.end(),figu);
vf.insert(vf.end(),fig);
}