Skip to content

Instantly share code, notes, and snippets.

@reinaldoacdc
Last active June 27, 2019 01:19
Show Gist options
  • Save reinaldoacdc/3fcc92a6610bfb07d3ddc68b14e54164 to your computer and use it in GitHub Desktop.
Save reinaldoacdc/3fcc92a6610bfb07d3ddc68b14e54164 to your computer and use it in GitHub Desktop.
ClassHelper for TApplication
unit ApplicationHelper;
interface
uses Vcl.Forms;
type
TApplicationHelper = class helper for TApplication
procedure OpenForm( frm :TForm; obj :TFormClass);
procedure CloseAllForms;
procedure Log(text: String);
end;
implementation
{ TApplicationHelper }
procedure TApplicationHelper.CloseAllForms;
var
n : integer;
Form : TForm;
begin
// Fecha os forms criados no formulário principal
n := 0;
While n <= ( Application.MainForm.ComponentCount - 1 ) do
Begin
if ( Application.MainForm.Components[ n ] is TForm ) then
Begin
Form := TForm(Application.MainForm.Components[n]);
If ( Form <> Application.MainForm ) then
Begin
N := -1;
Form.Close;
Form.Free;
End;
End;
Inc(n);
end;
end;
procedure TApplicationHelper.Log(text: String);
var
NomeDoLog: string;
Arquivo: TextFile;
begin
NomeDoLog := ExtractFilePath(Application.ExeName)+'Log.txt';
AssignFile(Arquivo, NomeDoLog);
if FileExists(NomeDoLog) then
Append(Arquivo)
else
ReWrite(Arquivo);
try
Writeln(arquivo, text);
finally
CloseFile(arquivo)
end;
end;
procedure TApplicationHelper.OpenForm(frm: TForm; obj: TFormClass);
begin
if frm = nil then
frm := obj.Create(nil);
frm.Show;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment