unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
RichEdit: TRichEdit;
btn_copy: TButton;
btn_cut: TButton;
btn_paste: TButton;
btn_del: TButton;
btn_select: TButton;
btn_canel: TButton;
btn_new: TButton;
btn_save: TButton;
btn_open: TButton;
btn_font: TButton;
OpenDig: TOpenDialog;
SaveDlog: TSaveDialog;
FontDialog: TFontDialog;
procedure btn_copyClick(Sender: TObject);
procedure btn_cutClick(Sender: TObject);
procedure btn_pasteClick(Sender: TObject);
procedure btn_delClick(Sender: TObject);
procedure btn_selectClick(Sender: TObject);
procedure btn_canelClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure RichEditSelectionChange(Sender: TObject);
procedure btn_newClick(Sender: TObject);
procedure btn_openClick(Sender: TObject);
procedure btn_saveClick(Sender: TObject);
procedure btn_fontClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn_copyClick(Sender: TObject);
begin
RichEdit.CopyToClipboard;
RichEdit.SetFocus;
end;
procedure TForm1.btn_cutClick(Sender: TObject);
begin
RichEdit.CutToClipboard;
RichEdit.SetFocus;
end;
procedure TForm1.btn_pasteClick(Sender: TObject);
begin
RichEdit.PasteFromClipboard;
RichEdit.SetFocus;
end;
procedure TForm1.btn_delClick(Sender: TObject);
begin
RichEdit.SelText := '';
RichEdit.SetFocus;
end;
procedure TForm1.btn_selectClick(Sender: TObject);
begin
RichEdit.HideSelection := false;
RichEdit.SelectAll;
RichEdit.SetFocus;
end;
procedure TForm1.btn_canelClick(Sender: TObject);
begin
RichEdit.Undo;
RichEdit.SetFocus;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if RichEdit.SelLength = 0
then begin
btn_copy.Enabled := false;
btn_cut.Enabled := false;
btn_del.Enabled := false;
end
else begin
btn_copy.Enabled := true;
btn_cut.Enabled := true;
btn_del.Enabled := true;
end
end;
procedure TForm1.RichEditSelectionChange(Sender: TObject);
begin
if RichEdit.SelLength = 0
then begin
btn_copy.Enabled := false;
btn_cut.Enabled := false;
btn_del.Enabled := false;
end
else begin
btn_copy.Enabled := true;
btn_cut.Enabled := true;
btn_del.Enabled := true;
end
end;
procedure TForm1.btn_newClick(Sender: TObject);
var i:word;
begin
if RichEdit.Modified
then begin
i := messagedlg('文件被修改了,是否保存',mtconfirmation,[mbyes,mbno,mbcancel],1);
if i = mryes then
if SaveDlog.Execute then begin
RichEdit.Lines.SaveToFile(saveDlog.FileName);
RichEdit.Text :='';
RichEdit.Modified := false;
end;
if i=mrno then
begin
RichEdit.Text := '';
RichEdit.Modified := false;
end;
end
else begin
RichEdit.Text := '';
RichEdit.Modified := false;
end;
end;
procedure TForm1.btn_openClick(Sender: TObject);
var i:word;
begin
if RichEdit.Modified then
begin
i:=messagedlg('文件被修改,是否先保存。。',mtconfirmation,[mbyes,mbno,mbcancel],1);
if i = mryes then
if SaveDlog.Execute then
begin
RichEdit.Lines.SaveToFile(SaveDlog.FileName);
if OpenDig.Execute then
RichEdit.Lines.LoadFromFile(OpenDig.FileName);
RichEdit.Modified := false;
end;
if i = mrno then
if OpenDig.Execute then
begin
RichEdit.Lines.LoadFromFile(OpenDig.FileName);
RichEdit.Modified := false;
end;
end
else
begin
if OpenDig.Execute then
RichEdit.Lines.LoadFromFile(OpenDig.FileName);
RichEdit.Modified := false;
end;
end;
procedure TForm1.btn_saveClick(Sender: TObject);
begin
if RichEdit.Modified then
if SaveDlog.Execute then
begin
RichEdit.Lines.SaveToFile(SaveDlog.FileName);
RichEdit.Modified := false;
end;
end;
procedure TForm1.btn_fontClick(Sender: TObject);
begin
FontDialog.Font.Assign(RichEdit.DefAttributes);
if FontDialog.Execute then
RichEdit.SelAttributes.Assign(FontDialog.Font);
end;
end.