note.pas
来自「用Delphi编的一个简单的记事本,这里面实现了一些简单的功能~!」· PAS 代码 · 共 533 行
PAS
533 行
unit note;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus,childform, ComCtrls, ToolWin, ImgList, StdCtrls,inifiles,
ExtActns, StdActns, ActnList, BandActn, ExtCtrls;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit: TMenuItem;
N3: TMenuItem;
Print: TMenuItem;
N4: TMenuItem;
Save: TMenuItem;
Open: TMenuItem;
New: TMenuItem;
Window: TMenuItem;
Show1: TMenuItem;
N8: TMenuItem;
Tile1: TMenuItem;
Help: TMenuItem;
About1: TMenuItem;
Close: TMenuItem;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
ToolBar1: TToolBar;
tb_new: TToolButton;
tb_open: TToolButton;
tb_save: TToolButton;
ToolButton4: TToolButton;
tb_undo: TToolButton;
tb_cut: TToolButton;
tb_copy: TToolButton;
ImageList1: TImageList;
ToolButton8: TToolButton;
tb_print: TToolButton;
ToolBar2: TToolBar;
ToolButton11: TToolButton;
tb_paste: TToolButton;
tb_bold: TToolButton;
tb_Italic: TToolButton;
tb_UnderLine: TToolButton;
tb_color: TToolButton;
ToolButton18: TToolButton;
tb_left: TToolButton;
tb_center: TToolButton;
tb_right: TToolButton;
StatusBar1: TStatusBar;
ComboBox1: TComboBox;
PrinterSetupDialog1: TPrinterSetupDialog;
PrintDialog1: TPrintDialog;
N1: TMenuItem;
ComboBox2: TComboBox;
ToolButton1: TToolButton;
ColorDialog1: TColorDialog;
PopupMenu1: TPopupMenu;
FontDialog1: TFontDialog;
ActionList1: TActionList;
RichEditAlignLeft1: TRichEditAlignLeft;
RichEditAlignRight1: TRichEditAlignRight;
RichEditAlignCenter1: TRichEditAlignCenter;
ActionList2: TActionList;
EditCut1: TEditCut;
EditCopy1: TEditCopy;
EditPaste1: TEditPaste;
EditUndo1: TEditUndo;
FileSaveAs1: TFileSaveAs;
FileExit1: TFileExit;
OpenPicture1: TOpenPicture;
SavePicture1: TSavePicture;
FontEdit1: TFontEdit;
Action1: TAction;
WindowClose1: TWindowClose;
PrintDlg1: TPrintDlg;
RichEditBold1: TRichEditBold;
RichEditItalic1: TRichEditItalic;
RichEditUnderline1: TRichEditUnderline;
CustomizeActionBars1: TCustomizeActionBars;
FindDialog1: TFindDialog;
ReplaceDialog1: TReplaceDialog;
Timer1: TTimer;
procedure CloseClick(Sender: TObject);
procedure NewClick(Sender: TObject);
procedure OpenClick(Sender: TObject);
procedure SaveClick(Sender: TObject);
procedure Tile1Click(Sender: TObject);
procedure Cascade1Click(Sender: TObject);
procedure ArrangeAllClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure PrintSetupClick(Sender: TObject);
procedure PrintClick(Sender: TObject);
procedure MyClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ComboBox1Change(Sender: TObject);
procedure tb_boldClick(Sender: TObject);
procedure ComboBox2Change(Sender: TObject);
procedure tb_ItalicClick(Sender: TObject);
procedure tb_UnderLineClick(Sender: TObject);
procedure tb_colorClick(Sender: TObject);
procedure ExitClick(Sender: TObject);
procedure tb_undoClick(Sender: TObject);
procedure tb_cutClick(Sender: TObject);
procedure tb_copyClick(Sender: TObject);
procedure tb_pasteClick(Sender: TObject);
procedure tb_printClick(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure tb_saveClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);//动态菜单OnClick事件响应
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
fname:string;
achild:Tformchild;
f1,f2,f3,f4,lines:tmenuitem;
n:byte;//自动生成的子菜单个数
//a:boolean=true; //判断是粗体还是斜体或加下划线
myinifile:Tinifile;
implementation
{$R *.dfm}
procedure TMainForm.MyClick(Sender: TObject);//动态菜单OnClick事件响应
var
i,p,l:integer;
find:boolean;
s:string;
begin
s:=TMenuItem(Sender).Caption; // 动态菜单标题
p:=pos(':',TMenuItem(Sender).Caption); //":"在菜单标题中的位置
l:=length(TMenuItem(Sender).Caption); //菜单标题长度
fname:=copy(s,p-1,l-p+2); // 取出菜单标题里面的文件路径
i:=mdiChildCount;
find:=false;
while not find and (i>=0) do
if mdiChildren[i].Caption=fname then
begin
mdiChildren[i].Show;
find:=true;
fname:=opendialog1.FileName;
end
else
i:=i-1;
if not find then
begin
achild:=tformchild.Create(application);
achild.RichEdit1.Lines.LoadFromFile(fname);
achild.Caption:=fname;
end;
end;
procedure TMainForm.CloseClick(Sender: TObject);
begin
achild:=tformChild.Create(application);
achild.RichEdit1.Clear;
getdir(0,fname);
if length(fname)=3 then
fname:=fname+'文档'+inttostr(MdiChildCount)
else
fname:=fname+'\文档'+inttostr(mdichildcount);
achild.Caption:=fname;
tb_save.Enabled:=true;
tb_print.Enabled:=true;
tb_copy.Enabled:=false;
tb_cut.Enabled:=false;
tb_paste.Enabled:=true;
tb_print.Enabled:=true;
tb_undo.Enabled:=true;
end;
procedure TMainForm.NewClick(Sender: TObject);
begin //新建
achild:=tformChild.Create(application);
achild.RichEdit1.Clear;
getdir(0,fname);
if length(fname)=3 then
fname:=fname+'文档'+inttostr(MdiChildCount)
else
fname:=fname+'\文档'+inttostr(mdichildcount);
achild.Caption:=fname;
tb_save.Enabled:=true;
tb_print.Enabled:=true;
tb_copy.Enabled:=false;
tb_cut.Enabled:=false;
tb_paste.Enabled:=true;
tb_print.Enabled:=true;
tb_undo.Enabled:=true;
end;
procedure TMainForm.OpenClick(Sender: TObject);
var //打开
i:integer;
find:boolean;
begin
if opendialog1.Execute and fileExists(opendialog1.FileName) then
begin
i:=mdiChildCount;
find:=false;
while not find and (i>=0) do
if mdiChildren[i].Caption=opendialog1.FileName then
begin
mdiChildren[i].Show;
find:=true;
fname:=opendialog1.FileName;
end
else
i:=i-1;
if not find then
begin
achild:=tformchild.Create(application);
achild.RichEdit1.Lines.LoadFromFile(opendialog1.FileName);
achild.Caption:=opendialog1.FileName;
fname:=opendialog1.FileName;
end;
end;
//动态创建菜单
try
lines:=tMenuItem.Create(self);
lines.Caption:='-';
if n=1 then
begin
file1.Insert(file1.Count-1,lines);
myinifile.WriteString('line','分割线',lines.Caption); //写入ini文件
end;
f1:=tMenuItem.Create(self);
f1.Caption:=inttostr(n)+' '+fname;
file1.Insert(file1.Count-2,f1);
myinifile.WriteString('path'+inttostr(n),'文件路径',f1.Caption); //写入ini文件
f1.OnClick:=myclick;
except
f1.Free;
lines.Free;
raise;
end;
n:=n+1;
if n=5 then n:=1;
end;
procedure TMainForm.SaveClick(Sender: TObject);
var //保存
curChild:Tformchild;
begin
curChild:=self.ActiveMDIchild as Tformchild;
if pos('文档',fname)<>0 then
begin
if savedialog1.Execute then
begin
fname:=savedialog1.FileName;
curchild.richedit1.Lines.savetofile(fname);
curchild.Caption:=fname;
end;
end
else
begin
fname:=self.ActiveMDIChild.Caption;
curchild.richedit1.Lines.savetofile(fname);
curchild.Caption:=fname;
end;
end;
procedure TMainForm.Tile1Click(Sender: TObject);
begin
Tile;
end;
procedure TMainForm.Cascade1Click(Sender: TObject);
begin
cascade ;
end;
procedure TMainForm.ArrangeAllClick(Sender: TObject);
begin
ArrangeIcons;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
combobox1.Items:=screen.Fonts;
n:=1;
tb_copy.Enabled:=false;
tb_cut.Enabled:=false;
tb_paste.Enabled:=false;
tb_undo.Enabled:=false;
end;
procedure TMainForm.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
combobox1.Canvas.FillRect(rect);
combobox1.Canvas.Font.Name:=combobox1.Items[index];
combobox1.Canvas.Font.Size:=0;
combobox1.Canvas.TextOut(rect.Left+1,rect.Top,combobox1.Items[index])
end;
procedure TMainForm.PrintSetupClick(Sender: TObject);
begin
try
PrinterSetupDialog1.Execute
except
Application.MessageBox(Pchar('无法找到默认的打印机'+#13+#10+'请确认打印机已安装正确。'),Pchar(Application.Title),MB_ICONEXCLAMATION);
end;
end;
procedure TMainForm.PrintClick(Sender: TObject);
begin //打印
try
formchild.RichEdit1.Print(FName);
except
Application.MessageBox(Pchar('无法找到默认的打印机'+#13+#10+'请确认打印机已安装正确。'),Pchar(Application.Title),MB_ICONEXCLAMATION);
end;
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
{ myinifile:=tinifile.Create('files.ini');
try
lines:=tmenuitem.Create(self);
lines.Caption:=myinifile.ReadString('line','分割线','');
if lines.Caption<>'' then
file1.Insert(file1.Count-1,lines);
f1:=tmenuItem.Create(self);
f1.Caption:=myinifile.ReadString('path1','文件路径','');
if f1.Caption<>'' then
file1.Insert(file1.Count-2,f1);
f2:=tmenuItem.Create(self);
f2.Caption:=myinifile.ReadString('path2','文件路径','');
if f1.Caption<>'' then
file1.Insert(file1.Count-2,f2);
f3:=tmenuItem.Create(self);
f3.Caption:=myinifile.ReadString('path3','文件路径','');
if f1.Caption<>'' then
file1.Insert(file1.Count-2,f3);
f4:=tmenuItem.Create(self);
f4.Caption:=myinifile.ReadString('path4','文件路径','');
if f1.Caption<>'' then
file1.Insert(file1.Count-2,f4);
f1.OnClick:=myclick;
f2.OnClick:=myclick;
f3.OnClick:=myclick;
f4.OnClick:=myclick;
except
lines.Free;
f1.Free;
end; }
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// myinifile.Destroy;
end;
procedure TMainForm.ComboBox1Change(Sender: TObject);
begin
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Name:=combobox1.Text;
end;
procedure TMainForm.tb_boldClick(Sender: TObject);
begin //粗体
if tb_bold.Down then
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style:=TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style+[fsbold]
else
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style:=TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style-[fsbold]
end;
procedure TMainForm.ComboBox2Change(Sender: TObject);
begin //字体大小
TFormChild(ActiveMDIchild).Richedit1.SelAttributes.Size:=strtoint(combobox2.Text)
end;
procedure TMainForm.tb_ItalicClick(Sender: TObject);
begin //斜体
if tb_Italic.Down then
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style:=TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style+[fsItalic]
else
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style:=TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style-[fsItalic]
end;
procedure TMainForm.tb_UnderLineClick(Sender: TObject);
begin //下划线
if tb_UnderLine.Down then
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style:=TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style+[fsUnderline]
else
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style:=TFormChild(ActiveMDIchild).richedit1.SelAttributes.Style-[fsUnderline]
end;
procedure TMainForm.tb_colorClick(Sender: TObject);
begin //颜色
if colordialog1.Execute then
TFormChild(ActiveMDIchild).richedit1.SelAttributes.Color:=colordialog1.Color
end;
procedure TMainForm.ExitClick(Sender: TObject);
var //退出
curChild:Tformchild;
begin
if (TFormChild(ActiveMDIchild).Richedit1.Modified) then
if messageDlg('是否保存已修改的内容?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
begin
curChild:=self.ActiveMDIchild as Tformchild;
if pos('文档',fname)<>0 then
begin
if savedialog1.Execute then
begin
fname:=savedialog1.FileName;
curchild.richedit1.Lines.savetofile(fname);
curchild.Caption:=fname;
end;
end
else
begin
fname:=self.ActiveMDIChild.Caption;
curchild.richedit1.Lines.savetofile(fname);
curchild.Caption:=fname;
end;
end;
end;
procedure TMainForm.tb_undoClick(Sender: TObject);
begin //撤消
TFormChild(ActiveMDIchild).richedit1.Undo;
end;
procedure TMainForm.tb_cutClick(Sender: TObject);
begin //剪切
TFormChild(ActiveMDIchild).Richedit1.CutToClipboard;
end;
procedure TMainForm.tb_copyClick(Sender: TObject);
begin //复制
TFormChild(ActiveMDIchild).Richedit1.CopyToClipboard;
end;
procedure TMainForm.tb_pasteClick(Sender: TObject);
begin //粘贴
TFormChild(ActiveMDIchild).Richedit1.PasteFromClipboard;
end;
procedure TMainForm.tb_printClick(Sender: TObject);
begin //打印
Showmessage('由于条件不允许,不能打印文件~!');
end;
procedure TMainForm.About1Click(Sender: TObject);
begin
Showmessage('能力有限,不能完善此记事本.抱歉~!');
end;
procedure TMainForm.N1Click(Sender: TObject);
begin
Showmessage('此记事本是由潘秀田所做,使用当中如有问题请联系…………');
end;
procedure TMainForm.tb_saveClick(Sender: TObject);
var //保存
curChild:Tformchild;
begin
curChild:=self.ActiveMDIchild as Tformchild;
if pos('文档',fname)<>0 then
begin
if savedialog1.Execute then
begin
fname:=savedialog1.FileName;
curchild.richedit1.Lines.savetofile(fname);
curchild.Caption:=fname;
end;
end
else
begin
fname:=self.ActiveMDIChild.Caption;
curchild.richedit1.Lines.savetofile(fname);
curchild.Caption:=fname;
end;
end;
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
StatusBar1.Panels[1].Text:=datetimetostr(now());
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?