Lazarus Programmering 2

Grundstruktur for LFP program med grafisk brugergrænseflade

Når man skriver et nyt LFP program laver systemet automatisk en fil med denne tekst: den skal normalt ikke redigeres:

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, Unit1
  { you can add units after this };

{$R *.res}

begin
  RequireDerivedFormResource := True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Den redigerbare del af programmet er en anden fil - standardnavnet er "unit1". Der skal også oprettes en "Form" med to "Dialogs" indbygget: OpenDialog og SaveDialog samt en "Button" der starter programmet ved klik, lad også programmet give en meddelelse om, at det er færdigt:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);

var
 infilename, outfilename: string;
 fin, fout: textfile;
 character: char;
 i: integer;
 test: boolean;
(* Add more definitions here *)

(*En procedure erklæret inde i programmet*)
procedure copyline(var fin,fout:text);
      var character:char;
      begin
          while not eoln(fin) do begin
             read(fin,character);
             write(fout,character);
          end;
      end;

begin
if OpenDialog1.Execute then
begin infilename := OpenDialog1.Filename; end;
if SaveDialog1.Execute then
begin outfilename:=SaveDialog1.Filename; end;

AssignFile(fin,infilename); Reset(fin);
AssignFile(fout,outfilename); Rewrite(fout);

(* Insert program code here *)
while not eof(fin) do begin
   read(fin, character);
   write(fout, character);
 end;
(* End of your own program code *)

CloseFile(fin); CloseFile(fout);
ShowMessage('Finished: Click OK to stop program');
end;
end.
Medmindre andet er angivet, er indholdet af denne side licenseret under Creative Commons Attribution-ShareAlike 3.0 License