设计一个简易的文本编辑器,具有创建、编辑、保存普通文本文件的功能。
unit U nit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
f:textfile;
wfilename:string;
flag:boolean;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
assignfile(f,'newfile.txt');
form1.Caption:='newfile.txt';
wfilename:='newfile.txt';
rewrite(f);
memo1.Enabled:=true;
button1.Enabled:=false;
button2.Enabled:=false;
button3.Enabled:=false;
button4.Enabled:=true;
button5.Enabled:=true;
button6.Enabled:=true;
button7.Enabled:=true;
closefile(f);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
line:string;
begin
if opendialog1.Execute then
begin
assignfile(f,opendialog1.FileName);
reset(f);
form1.Caption:=opendialog1.FileName;
memo1.Enabled:=false;
flag:=true;
while not eof(f) do
begin
readln(f,line);
memo1.Lines.Add(line);
end;
button1.Enabled:=false;
button2.Enabled:=false;
button3.Enabled:=false;
button4.Enabled:=false;
button5.Enabled:=false;
button6.Enabled:=true;
button7.Enabled:=true;
closefile(f);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
line:string;
begin
if opendialog1.Execute then
begin
wfilename:=opendialog1.FileName;
assignfile(f,wfilename);
form1.Caption:=wfilename;
reset(f);
memo1.Enabled:=true;
while not eof(f) do
begin
readln(f,line);
memo1.Lines.Add(line);
end;
button1.Enabled:=false;
button2.Enabled:=false;
button3.Enabled:=false;
button4.Enabled:=true;
button5.Enabled:=true;
button6.Enabled:=true;
button7.Enabled:=true;
closefile(f);
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
i,lastline:integer;
begin
lastline:=memo1.Lines.Add('')-1;
assignfile(f,wfilename);
rewrite(f);
for i:=0 to lastline do
begin
write(f,memo1.lines[i]);
end;
closefile(f);
end;
procedure TForm1.Button5Click(Sender: TObject);
var
i,lastline:integer;
writefile:textfile;
begin
if savedialog1.Execute then
begin
wfilename:=savedialog1.FileName;
assignfile(writefile,wfilename);
rewrite(writefile);
form1.Caption:=wfilename;
lastline:=memo1.Lines.Add('')-1;
for i:=0 to lastline do
begin
writeln(writefile,memo1.lines[i]);
end;
closefile(writefile);
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
var
i:integer;
begin
if not flag then
i:=messagedlg('save file ?',mtConfirmation,mbYesNoCancel,0);
if i <> 2 then
begin
if i=6 then
Button4Click(sender);
memo1.Clear;
form1.Caption:='';
memo1.Enabled:=false;
button1.Enabled:=true;
button2.Enabled:=true;
button3.Enabled:=true;
button4.Enabled:=false;
button5.Enabled:=false;
button6.Enabled:=false;
button7.Enabled:=true;
end;
flag:=false;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
close;
end;
end.