unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
OpenDialog1: TOpenDialog;
PaintBox1: TPaintBox;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
private
{ private declarations }
FPicture : TPicture;
protected
Function GetLastFileName: String;
Procedure SetlastFileName(sFilename: String);
public
{ public declarations }
procedure OpenPictureFileName(sFilename: String);
procedure OpenPictureDialog;
property LastFileName: String read GetLastFileName write SetlastFileName;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
const
StorageExt = '.LastSelectedPicture';
var
StorageFileName : String;
function TForm1.GetLastFileName: String;
var
sl : TStringList;
ThisLastPicture : String;
begin
// Make sure StorageFilename actually exist on disk
if FileExists(StorageFilename) then
begin
// Create stringlist.
sl := TStringList.Create;
// Load data stored on disk into stringlist
sl.LoadFromFile(StorageFilename);
// Check if there is at least one entry inside the stringlist
if (sl.Count > 0) then
begin
// Assume one entry, first being last used filename of picture
ThisLastPicture := sl.Strings[0];
// Check if the entry from the stringlist contains a valid (existing)
// filename and return the result
if FileExists(ThisLastPicture)
then Result := ThisLastPicture
else Result := '';
end;
// Free Stringlst resources.
sl.Free;
end;
end;
procedure TForm1.SetlastFileName(sFilename: String);
var
sl : TStringList;
begin
// Create the stringlist.
sl := TStringList.Create;
// Add the given sFilename to the stringlist
sl.Add(sFilename);
// Store the stringlist with lastfilename to disk
sl.SaveToFile(StorageFileName);
// Free stringlist resources.
sl.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
LastPicture: String;
begin
// Construct the picture
FPicture := TPicture.Create;
// Construct filename used for storing text
// This filename/path assumes you are allowed to read/write in your
// application directory. If not then change accordingly.
StorageFileName := ChangeFileExt(Application.ExeName, StorageExt);
// Retrieve the last used picture filename (if any).
LastPicture := LastFileName;
// Only if LastPicture was valid we can load/display it
if (LastPicture <> '') then
begin
OpenPictureFileName(LastPicture);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(FPicture);
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
// Paint the bitmap whenever the system tells us to
PaintBox1.Canvas.Draw(0, 0, FPicture.Bitmap);
end;
procedure TForm1.OpenPictureFileName(sFilename: String);
var
sFullFilename : String;
sFilenameOnly : String;
begin
sFullFilename := sFilename;
// Construct filename without path to show in a label
sFilenameOnly := extractfilename(sFullFilename);
Label1.Caption := sFilenameOnly;
// Clear picture and load file from disk
FPicture.Clear;
FPicture.LoadFromFile(sFullFilename);
// Prepare paintbox to display picure
Paintbox1.Canvas.Clear;
Paintbox1.Width := FPicture.Bitmap.Width;
Paintbox1.height := FPicture.Bitmap.Height;
// Invalidate to force drawing of picture
Paintbox1.invalidate;
end;
procedure TForm1.OpenPictureDialog;
var
sFullFilename: String;
begin
// Open a dialog for user to select a file
if OpenDialog1.Execute then
begin
// Retrieve the selected filename
sFullFilename := OpenDialog1.FileName;
// Attempt to open the picture
OpenPictureFilename(sFullFilename);
// Make sure the filename of the last chosen picure is stored to disk.
LastFilename := sFullFilename;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// User clicked a button, so act on it.
OpenPictureDialog;
end;