Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Funktion: Graphic anhand des Namens aus Res erstellen

  Alt 17. Feb 2016, 00:23
Oder man macht es sich etwas gemütlicher:

Die Resourcen als class property hinterlegen
Delphi-Quellcode:
unit AppResources;

interface

uses
  Resources.Types,
  Resources.Vcl.Types;

type
  Resources = class abstract
  private
    class var FBildA: TGraphicResource;
    class var FBildB: TGraphicResource;
    class var FBildC: TGraphicResource;
    class var FBildD: TGraphicResource;
  protected
    class constructor Create;
    class destructor Destroy;
  public
    class property BildA: TGraphicResource read FBildA;
    class property BildB: TGraphicResource read FBildB;
    class property BildC: TGraphicResource read FBildC;
    class property BildD: TGraphicResource read FBildD;
  end;

implementation

{$R AppResources.res AppResources.rc}

{ Resources }

class constructor Resources.Create;
begin
  FBildA := TBitmapResource.Create( 'bild_bmp' );
  FBildB := TJpegResource.Create( 'bild_jpg' );
  FBildC := TPngResource.Create( 'bild_png' );
  FBildD := TGifResource.Create( 'bild_gif' );
end;

class destructor Resources.Destroy;
begin
  FBildA.Free;
  FBildB.Free;
  FBildC.Free;
  FBildD.Free;
end;

end.
und dann einfach verwenden
Delphi-Quellcode:
  Image1.Picture.Assign( Resources.BildA );
  // oder
  Image1.Picture.Assign( Resources.BildB );
  // oder
  Image1.Picture.Assign( Resources.BildC );
  // oder
  Image1.Picture.Assign( Resources.BildD );
Ermöglicht wird das dann durch
Delphi-Quellcode:
unit Resources.Types;

interface

uses
  System.Classes,
  System.SysUtils,
  System.Types;

type
  TResource = class abstract( TInterfacedPersistent, IStreamPersist )
  protected
    function GetDataStream: TStream; virtual; abstract;
  protected
    procedure AssignTo( Dest: TPersistent ); override;
  public
    procedure LoadFromStream( Stream: TStream );
    procedure SaveToStream( Stream: TStream );
  end;

  TEmbeddedResource = class( TResource )
  private
    FResName: string;
  protected
    function GetDataStream: TStream; override;
  public
    constructor Create( const ResName: string );
    property ResName: string read FResName;
  end;

implementation

{ TResource }

procedure TResource.AssignTo( Dest: TPersistent );
var
  other : IStreamPersist;
  source: TStream;
begin
  if Supports( Dest, IStreamPersist, other )
  then
    begin
      source := GetDataStream( );
      try
        other.LoadFromStream( source );
      finally
        source.Free;
      end;
    end
  else
    inherited;
end;

procedure TResource.LoadFromStream( Stream: TStream );
begin
  raise EInvalidOperation.Create( 'Resources are read only' );
end;

procedure TResource.SaveToStream( Stream: TStream );
var
  source: TStream;
begin
  source := GetDataStream( );
  try
    Stream.CopyFrom( source, -1 );
  finally
    source.Free;
  end;
end;

{ TEmbeddedResource }

constructor TEmbeddedResource.Create( const ResName: string );
begin
  inherited Create;
  FResName := ResName;
end;

function TEmbeddedResource.GetDataStream: TStream;
begin
  Result := TResourceStream.Create( HInstance, FResName, RT_RCDATA );
end;

end.
und
Delphi-Quellcode:
unit Resources.Vcl.Types;

interface

uses
  System.Classes,
  Vcl.Graphics,
  Resources.Types;

type
  TGraphicResource = class abstract( TEmbeddedResource )
  private
    function GetGraphic: TGraphic;
  protected
    function GetGraphicClass: TGraphicClass; virtual; abstract;
    procedure AssignTo( Dest: TPersistent ); override;
  end;

  TBitmapResource = class( TGraphicResource )
  protected
    function GetGraphicClass: TGraphicClass; override;
  end;

  TGifResource = class( TGraphicResource )
  protected
    function GetGraphicClass: TGraphicClass; override;
  end;

  TJpegResource = class( TGraphicResource )
  protected
    function GetGraphicClass: TGraphicClass; override;
  end;

  TPngResource = class( TGraphicResource )
  protected
    function GetGraphicClass: TGraphicClass; override;
  end;

implementation

uses
  Vcl.Imaging.GIFImg,
  Vcl.Imaging.pngimage,
  Vcl.Imaging.jpeg;

{ TGraphicResource }

procedure TGraphicResource.AssignTo( Dest: TPersistent );
var
  source: TGraphic;
begin
  if ( Dest is TGraphic ) or ( Dest is TPicture )
  then
    begin
      source := GetGraphic;
      try
        Dest.Assign( source );
      finally
        source.Free;
      end;
    end
  else
    inherited;
end;

function TGraphicResource.GetGraphic: TGraphic;
var
  source: TStream;
begin
  Result := GetGraphicClass( ).Create;
  source := GetDataStream;
  try
    Result.LoadFromStream( source );
  finally
    source.Free;
  end;
end;

{ TBitmapResource }

function TBitmapResource.GetGraphicClass: TGraphicClass;
begin
  Result := TBitmap;
end;

{ TGifResource }

function TGifResource.GetGraphicClass: TGraphicClass;
begin
  Result := TGIFImage;
end;

{ TJpegResource }

function TJpegResource.GetGraphicClass: TGraphicClass;
begin
  Result := TJPEGImage;
end;

{ TPngResource }

function TPngResource.GetGraphicClass: TGraphicClass;
begin
  Result := TPngImage;
end;

end.
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