Bueno, ya lo probé con Delphi 7.
Hice algunos cambios y añado algunos comentarios por si le puede ser de utilidad a otras personas:
El código
tal como está, debe probarse usando solo rectángulos porque, aunque graba todos los tipos de nodos, al leerlos da error si no son rectángulos.
- Dado que RECT no tiene las propiedades Width ni Heigth (y tiene Rigth y Button), hice un pequeña adaptación del código en 2 líneas.
- En el código tal como está, al crear un archivo INI, se sobreescribe el anterior. Si grabamos más nodos que los que había en el archivo INI, no hay problema.
Aunque, si en un archivo INI creado anteriormente habían datos de 6 nodos y nosotros grabamos una imagen con 2 nodos, los datos de los 4 últimos nodos seguirán existiendo. Por ello, hice un pequeño añadido al código para que, antes de grabar un archivo INI, borre el archivo anterior (si existe).
- Y hay que acordarse de añadir INIFILES en el USES:
Incluyo aquí el código de Germán, con esos pequeños cambios hechos en Delphi 7:
Código Delphi
[-]
procedure TForm1.btnGrabarClick(Sender: TObject);
var
i, oc:Integer;
o:TGraphObject;
FIni:TIniFile;
begin
if FileExists( ChangeFileExt(Application.ExeName, '.ini') )
then DeleteFile( ChangeFileExt(Application.ExeName, '.ini') ) ;
FIni := TIniFile.CReate(ChangeFileExt(Application.ExeName, '.ini'));
try
oc := SimpleGraph.ObjectsCount;
for I := 0 to (SimpleGraph.Objects.Count - 1) do begin
o := SimpleGraph.Objects.Items[i];
if (o is TGraphNode) then begin
FIni.WriteString('OBJ'+IntToStr(i), 'Clase', TGraphNode(o).ClassName);
FIni.WriteString('OBJ'+IntToStr(i), 'Left', IntToStr(TGraphNode(o).Left));
FIni.WriteString('OBJ'+IntToStr(i), 'Top', IntToStr(TGraphNode(o).Top));
FIni.WriteString('OBJ'+IntToStr(i), 'Ancho', IntToStr(TGraphNode(o).Width));
FIni.WriteString('OBJ'+IntToStr(i), 'Alto', IntToStr(TGraphNode(o).Height));
FIni.WriteString('OBJ'+IntToStr(i), 'Color', IntToStr(TGraphNode(o).Brush.Color));
end;
end;
finally
FreeAndNil(FIni);
end;
end;
procedure TForm1.btnRecuperarClick(Sender: TObject);
var
i, oc:Integer;
o:TGraphObject;
FIni:TIniFile;
secc:TStringList;
str:String;
Rect:TRect;
function GetNodeClass(AClass:string):TGraphNodeClass;
begin
if (AClass = 'TRectangularNode') then
Result := TRectangularNode;
end;
begin
SimpleGraph.CommandMode := cmInsertNode;
FIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
secc := TStringList.CReate;
try
FIni.ReadSections(secc);
for i := 0 to (secc.Count -1) do begin
Str := FIni.ReadString('OBJ' + IntToStr(i), 'Clase', 'TRectangularNode');
SimpleGraph.DefaultNodeClass := GetNodeClass(Str);
Rect.Left := FIni.ReadInteger('OBJ' + IntToStr(i), 'Left', 10);
Rect.Top := FIni.ReadInteger('OBJ' + IntToStr(i), 'Top', 10);
Rect.Right := Rect.Left + FIni.ReadInteger('OBJ' + IntToStr(i), 'Ancho', 100);
Rect.Bottom := Rect.Top + FIni.ReadInteger('OBJ' + IntToStr(i), 'Alto', 50);
o := SimpleGraph.InsertNode(Rect);
TGraphNode(o).Brush.Color := FIni.ReadInteger('OBJ' + IntToStr(i), 'Color', 0);
end;
finally
FreeAndNil(FIni);
FreeAndNil(secc);
end;
end;