📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, ImgList, ComCtrls, ToolWin,Winprocs;
type
Ttxtform = class(TForm)
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
MainMenu1: TMainMenu;
menufile: TMenuItem;
menunew: TMenuItem;
menuopen: TMenuItem;
menusave: TMenuItem;
menusaves: TMenuItem;
N1: TMenuItem;
menuymsz: TMenuItem;
menuprint: TMenuItem;
N2: TMenuItem;
menuend: TMenuItem;
menuedit: TMenuItem;
menucx: TMenuItem;
N3: TMenuItem;
menucut: TMenuItem;
menucopy: TMenuItem;
menupaste: TMenuItem;
menudel: TMenuItem;
N4: TMenuItem;
menufind: TMenuItem;
menufinds: TMenuItem;
menuchange: TMenuItem;
menuleap: TMenuItem;
N5: TMenuItem;
menusel: TMenuItem;
menttimedate: TMenuItem;
menugs: TMenuItem;
menuauto: TMenuItem;
menufond: TMenuItem;
H1: TMenuItem;
menuhelp: TMenuItem;
menuabout: TMenuItem;
PopupMenu1: TPopupMenu;
pupcx: TMenuItem;
pupcut: TMenuItem;
pupcopy: TMenuItem;
puppaste: TMenuItem;
pupdel: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
pupsel: TMenuItem;
FontDialog1: TFontDialog;
ImageList1: TImageList;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton5: TToolButton;
toolcx: TToolButton;
toolcut: TToolButton;
toolcopy: TToolButton;
ToolButton9: TToolButton;
tooldel: TToolButton;
toolleft: TToolButton;
toolmiddle: TToolButton;
toolright: TToolButton;
toolbold: TToolButton;
toolitalic: TToolButton;
toolunderline: TToolButton;
N8: TMenuItem;
N9: TMenuItem;
subbold: TMenuItem;
subitalic: TMenuItem;
subunderline: TMenuItem;
N11: TMenuItem;
L1: TMenuItem;
submiddle: TMenuItem;
R1: TMenuItem;
FindDialog1: TFindDialog;
memotxt: TRichEdit;
ToolButton3: TToolButton;
PrintDialog1: TPrintDialog;
B1: TMenuItem;
ColorDialog1: TColorDialog;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure menuopenClick(Sender: TObject);
procedure menusaveClick(Sender: TObject);
procedure menunewClick(Sender: TObject);
procedure menusavesClick(Sender: TObject);
procedure menueditClick(Sender: TObject);
procedure menucutClick(Sender: TObject);
procedure menucopyClick(Sender: TObject);
procedure menupasteClick(Sender: TObject);
procedure menucxClick(Sender: TObject);
procedure menudelClick(Sender: TObject);
procedure menuselClick(Sender: TObject);
procedure menuendClick(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
procedure menuautoClick(Sender: TObject);
procedure menufondClick(Sender: TObject);
procedure subboldClick(Sender: TObject);
procedure subitalicClick(Sender: TObject);
procedure subunderlineClick(Sender: TObject);
procedure toolleftClick(Sender: TObject);
procedure toolmiddleClick(Sender: TObject);
procedure toolrightClick(Sender: TObject);
procedure menufindClick(Sender: TObject);
procedure memotxt1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure memotxt1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FindDialog1Find(Sender: TObject);
procedure memotxtMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure memotxtKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure memotxtChange(Sender: TObject);
procedure ToolButton3Click(Sender: TObject);
procedure menuprintClick(Sender: TObject);
procedure B1Click(Sender: TObject);
procedure menttimedateClick(Sender: TObject);
procedure menuaboutClick(Sender: TObject);
private
filename:string;
function savedatas():boolean; //保存函数
function ismodify():boolean; //文本是否改变函数
{ Private declarations }
public
{ Public declarations }
end;
var
txtform: Ttxtform;
implementation
uses unit2;
{$R *.dfm}
{ Ttxtform }
procedure Ttxtform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ismodify then
action:=caFree;
end;
procedure Ttxtform.menuopenClick(Sender: TObject);
begin
if ismodify then
begin
if OpenDialog1.Execute then
begin
if FileExists(OpenDialog1.FileName) then
begin
memotxt.Lines.LoadFromFile(OpenDialog1.FileName);
txtform.Caption:=Extractfilename(opendialog1.FileName)+'-记事本'; //extractfilename函数取文件名
filename:=opendialog1.FileName;
end
else
showmessage(opendialog1.FileName+'不存在');
end;
end;
//else
// opendialog1.Execute;
end;
function Ttxtform.savedatas: boolean;
var
wil:boolean;
begin
wil:=true;
if fileexists(filename) then //判断文件名是否存在
begin
memotxt.Lines.SaveToFile(filename);
memotxt.Modified:=false;
end
else
while wil=true do //使弹出对话框循环
begin
if savedialog1.Execute then
begin
if fileexists(savedialog1.FileName) then
begin
if application.MessageBox('文件名已经存在,是否保存?','保存',MB_yesno)=idyes then
begin
memotxt.Lines.SaveToFile(savedialog1.FileName);
filename:=savedialog1.FileName;
txtform.Caption:=Extractfilename(savedialog1.FileName)+'-文本编辑器';
memotxt.Modified:=false;
break;
end
else
savedialog1.Execute;
end
else
begin
memotxt.Lines.SaveToFile(savedialog1.FileName);
txtform.Caption:=Extractfilename(savedialog1.FileName)+'-文本编辑器';
filename:=savedialog1.FileName;
memotxt.Modified:=false;
break;
end;
end;
break;
end; //while end
end;
procedure Ttxtform.menusaveClick(Sender: TObject);
begin
savedatas; //调用savedatas函数
end;
procedure Ttxtform.menunewClick(Sender: TObject);
begin
if ismodify then //调用ismodify函数
begin
memotxt.Lines.Clear;
filename:='';
end;
end;
function Ttxtform.ismodify: boolean; //判断是否发生改变的函数
var
intResult:integer;
begin
if memotxt.Modified then
begin
intResult:=application.MessageBox('数据已经改变,要保存吗?','保存',mb_yesnocancel);
if intResult=idyes then
result:=savedatas;
if intResult=idno then
result:=true;
if intResult=idcancel then
result:=false;
end
else
result:=true
end;
procedure Ttxtform.menusavesClick(Sender: TObject);
begin
while true do
begin
if savedialog1.Execute then
begin
if FileExists(savedialog1.FileName) then
begin
if application.MessageBox('是否覆盖?','文件保存',MB_YesNo)=idyes then
begin
memotxt.Lines.SaveToFile(savedialog1.FileName);
filename:=savedialog1.FileName;
txtform.Caption:=Extractfilename(savedialog1.FileName)+'-文本编辑器';
memotxt.Modified:=false;
break;
end ;
end
else
begin
memotxt.Lines.SaveToFile(savedialog1.FileName);
txtform.Caption:=Extractfilename(savedialog1.FileName)+'-文本编辑器';
filename:=savedialog1.FileName;
memotxt.Modified:=false;
break;
end;
end
else
break;
end; //while end
end;
procedure Ttxtform.menueditClick(Sender: TObject);
begin
if memotxt.SelLength<>0 then
begin
menucopy.Enabled:=true;
menucut.Enabled:=true;
menudel.Enabled:=true;
end
else
begin
menucopy.Enabled:=false;
menucut.Enabled:=false;
menudel.Enabled:=false;
end;
if memotxt.Modified then
begin
menucx.Enabled:=true;
end
else
menucx.Enabled:=false;
end;
procedure Ttxtform.menucutClick(Sender: TObject);
begin
memotxt.CutToClipboard;
end;
procedure Ttxtform.menucopyClick(Sender: TObject);
begin
memotxt.CopyToClipboard;
end;
procedure Ttxtform.menupasteClick(Sender: TObject);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -