Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#5

AW: Pfadangaben bei Image-Dateien

  Alt 6. Feb 2016, 09:54
Also egal in welchem Ordner ist es ja nun nicht, sondern die Dateien befinden sich ja immer relativ zum Anwendungsverzeichnis.
Code:
.\Anwendung.exe
.\Spielkarten\Spielkarte1.jpg
.\Spielkarten\Spielkarte2.jpg
...
Diese Dateien sind also ein fester Bestandteil der Anwendung und somit per Definition eine Ressource.

Ressourcen können sich direkt in der Anwendung befinden oder eben auch getrennt vorliegen.

Am sinnvollsten definiert man das auch so in seinem Programm
Delphi-Quellcode:
unit Unit1;

interface

type
  Resources = class sealed
  private const
    SpielkartenFolder = 'Spielkarten';
    Spielkarten: array [ 0 .. 7 ] of string = (
      {} 'Spielkarte1.jpg',
      {} 'Spielkarte2.jpg',
      {} 'Spielkarte3.jpg',
      {} 'Spielkarte4.jpg',
      {} 'Spielkarte5.jpg',
      {} 'Spielkarte6.jpg',
      {} 'Spielkarte7.jpg',
      {} 'Spielkarte8.jpg' );
  private
    class function GetAppFolder: string; static;
    class function PathCombine( const PartA, PartB: string ): string; overload;
    class function PathCombine( const Parts: array of string ): string; overload;
    class function GetSpielkarte( const Index: Integer ): string; static;

    class property AppFolder: string read GetAppFolder;
  public
    class property Spielkarte1: string index 0 read GetSpielkarte;
    class property Spielkarte2: string index 1 read GetSpielkarte;
    class property Spielkarte3: string index 2 read GetSpielkarte;
    class property Spielkarte4: string index 3 read GetSpielkarte;
    class property Spielkarte5: string index 4 read GetSpielkarte;
    class property Spielkarte6: string index 5 read GetSpielkarte;
    class property Spielkarte7: string index 6 read GetSpielkarte;
    class property Spielkarte8: string index 7 read GetSpielkarte;
  end;

implementation

uses
  SysUtils;

{ Resources }

class function Resources.GetAppFolder: string;
begin
  Result := ExtractFilePath( ParamStr( 0 ) );
end;

class function Resources.GetSpielkarte( const Index: Integer ): string;
begin
  Result := PathCombine( [ AppFolder, SpielkartenFolder, Spielkarten[ index ] ] );
end;

class function Resources.PathCombine( const PartA, PartB: string ): string;
begin
  Result := IncludeTrailingPathDelimiter( PartA ) + PartB;
end;

class function Resources.PathCombine( const Parts: array of string ): string;
var
  I: Integer;
begin
  Result := Parts[ 0 ];

  for I := 1 to high( Parts ) do
    begin
      Result := PathCombine( Result, Parts[ I ] );
    end;
end;

end.
und in der Anwendung brauche ich dann nur noch Image1.Picture.LoadFromFile( Resources.Spielkarte1 ); aufrufen.

(Ist schon lange her, aber class property kennt Delphi 7 wohl noch nicht )
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat