Ver Mensaje Individual
  #4  
Antiguo 30-04-2019
rrf rrf is offline
Miembro
 
Registrado: ago 2003
Ubicación: S/C Tenerife, España
Posts: 454
Reputación: 21
rrf Va por buen camino
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:
Código Delphi [-]
  uses inifiles, ...

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

  // Borra INI anterior, si existe
  if FileExists( ChangeFileExt(Application.ExeName, '.ini') )
    then DeleteFile(  ChangeFileExt(Application.ExeName, '.ini') ) ;
  // Crear un  INI
  FIni := TIniFile.CReate(ChangeFileExt(Application.ExeName, '.ini'));
  try
    oc := SimpleGraph.ObjectsCount;
    // Recorrer los objetos creados en el grafo
    for I := 0 to (SimpleGraph.Objects.Count - 1) do begin
      o := SimpleGraph.Objects.Items[i];
      // Es un nodo
      if (o is TGraphNode) then begin
        // Acceder à la clase
        FIni.WriteString('OBJ'+IntToStr(i), 'Clase', TGraphNode(o).ClassName);
        // posicion del objeto
        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));
        // Color de fondo
        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;

  // Obtener la clase del Nodo
  function GetNodeClass(AClass:string):TGraphNodeClass;
  begin
    if (AClass = 'TRectangularNode') then
      Result := TRectangularNode;
  end;

begin

  // Commando por defecto
  SimpleGraph.CommandMode := cmInsertNode;
  // Fichero de objetos
  FIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  secc := TStringList.CReate;
  try
    // Leer la lista de nodos a crear
    FIni.ReadSections(secc);
    // Recorerr los nodos a crear
    for i := 0 to (secc.Count -1) do begin
      // Clase del nodo
      Str := FIni.ReadString('OBJ' + IntToStr(i), 'Clase', 'TRectangularNode');
      // Definir la clase
      SimpleGraph.DefaultNodeClass := GetNodeClass(Str);
      // Posicion del objeto
      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);
      // Crear el nodo
      o := SimpleGraph.InsertNode(Rect);
      // color de fondo..
      TGraphNode(o).Brush.Color := FIni.ReadInteger('OBJ' + IntToStr(i), 'Color', 0);
      // Otras propiedades...
    end;
  finally
    FreeAndNil(FIni);
    FreeAndNil(secc);
  end;

end;
Responder Con Cita