我也是初学,前几天才做了一个,有些不懂的问题,也是请教了这里的高手的,我把我做的步骤贴给你,你试试哦.
记事本的做法:
1、打开delphi,保存一个名为editfm的工程文件和textedit的文件。然后把form的caption属性改为自己喜欢的名字,我改的是“晨烟记事本”,name改为editform。
2、添加一个mainmenu和popupmenu的控件,分别添加主菜单文件(mifile),编辑(miedit),格式(miformat),帮助(mohelp),在其下拉菜单里添加新建(minew)//{也可以用系统默认的N1、N2等,只是等会设置一些连接的时候会比较麻烦}打开(miopen)、保存(misave)、另存为(misaveas)、打印(miprint)、打印设置(miprintsetup)、退出(miexit),撤消(miundo),重做,剪切,复制,粘贴,删除,全选,查找,替换//{这几个代码比较简单,所以小小的偷了下懒,用了系统自带的name},居左(mileft),居中(micenter),居右(miright)这几个的miautowrap属性改为true,然后继续添加字体,字体变大,字体变小,自动换行,字数,背景,关于(miabout),帮助主题。然后在popupmenu设置下拉菜单,一般为撤消,剪切等。
3、添加richedit控件,属性name改为editor,align改为alclient,hidescrollbars改为false,scrollbars改为$$vertical,并在font中设置自己喜欢的字体颜色大小等。
4、在dialogs中,添加打印,保存,字体,颜色,打印,找印设置,查找,替换控件,把name分别改为dgopen,dgsave,dgfont,dgprint,dgprintsetup,dgfind,dgreplace。
5、在接口部分,添加一个声明
//
const
defaulfgilename='晨烟记事本';
在private下面添加私有成员:
//
pathname:string;
hasundo:boolean;
添加声明:
procedure CheckTextModified;
并在代码部分实现:
procedure TEditForm.CheckTextModified;
begin
if Editor.Modified then
//修改过
begin
case Application.MessageBox('文件没有保存!要保存吗?',
'提示',MB_YESNO+MB_ICONQUESTION) of
IDYES:
//保存文件
MISaveClick(Self);
//保存文件
end;
end;
end;
6、单击退出,在代码框里输入close;
单击新建,在代码框中输入
//
CheckTextModified;
//检测是否做了修改
Editor.Lines.Clear;
//清除所有文本
Editor.Modified:=False;
//把Modified属性设为False
editform.Caption:='晨烟记事本'+'--'+'新文档';
7、单击打开,在代码框中输入//DgOpen.Execute;
单击打开控件,在属性filter弹出的对话框filter editor,在filter name和filter输入:
文本文件(*.txt)
*.txt
RTF文件(*.rtf)
*.rtf
所有文件
*.*
8、单击保存,在属性filter弹出的对话框filter editor,在filter name和filter输入:
文本文件(*.txt)
*.txt
RTF文件(*.rtf)
*.rtf
所有文件
*.*
在属性options的ofnoreadonlyretu改为true,ofenablesizing改为false。
在代码框中输入以下代码:
//begin
if PathName = '晨烟记事本' then
//第一次保存
MISaveAsClick(Sender)
else
begin
Editor.Lines.SaveToFile(PathName); //已经保存过
Editor.Modified := False;
StatusBar1.Panels[1].Text:='';
end;
end;
9、单击另存为,输入代码:
begin
DgSave.FileName := '新文档';
if DgSave.Execute then
begin
PathName := DgSave.FileName;
//获取路径名
if DgSave.FilterIndex=1 then
//txt文件
begin
Editor.PlainText:=True;
PathName:=PathName+'.txt';
end;
if DgSave.FilterIndex=2 then
//rtf文件
begin
Editor.PlainText:=False;
PathName:=PathName+'.rtf';
end;
if DgSave.FilterIndex=3 then
//所有文件
begin
Editor.PlainText:=False;
end;
Editor.Lines.SaveToFile(PathName); //保存文件
Editor.Modified := False;
//设置Editor为未改变
Caption := ExtractFileName(PathName);
//取出文件名,设为窗口标题
StatusBar1.Panels[1].Text:='';
end;
end;
10、单击打印代码,输入
//
if DgPrint.Execute then
Editor.Print(PathName);
打印设置代码: DgPrinterSetup.Execute;
剪切代码: Editor.CutToClipboard;
复制代码:Editor.CopyToClipboard;
粘贴代码:Editor.PasteFromClipboard;
刪除:Editor.ClearSelection;
全选:Editor.SelectAll;
查找:DgFind.Execute;
替换:DgReplace.Execute;
撤消:
if not HasUndo then
begin
Editor.Undo;
HasUndo:=True;
重做:
if HasUndo then
begin
Editor.Undo;
HasUndo:=False;
end;
居左:
MILeft.Checked := True;
MICenter.Checked := False;
MIRight.Checked := False;
Editor.Paragraph.Alignment := taLeftJustify;
居中:
MILeft.Checked := False;
MICenter.Checked := True;
MIRight.Checked := False;
Editor.Paragraph.Alignment := taCenter;
居右:
MILeft.Checked := False;
MICenter.Checked := False;
MIRight.Checked := True;
Editor.Paragraph.Alignment := taRightJustify ;
字体:
if dgfont.Execute then
editForm.editor.Font:=dgfont.Font;
字体变大:with editor.Font do size:=size+1;
字体变小:with editor.Font do size:=size-1;
背景: if colordialog1.Execute then
editForm.editor.Color:=colordialog1.color;
字数:var
s:string;
i,e,c:integer;
begin
s:=editor.lines.text;
e:=0;
c:=0;
for i:=1 to length(s) do
begin
if (ord (s[i])>=33) and (ord(s[i])<=126) then
begin
inc(e);
StatusBar1.Panels[2].Text:='字母个数:'+inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
11、添加win32里的控件statusbar,单击右键,添加四个栏目,0的那一个,我没搞好,直接把width改为0了。第一个栏目我放的是时间,第二个第三个是数字统计,第四个就随便放点什么小东东了。
12、自动换行:属性checked改为true
with Editor do
begin
WordWrap := not WordWrap; //让WordWrap取反
if WordWrap then
//自动换行
ScrollBars := ssVertical
//Editor只有竖直滚动条
else
ScrollBars := ssBoth;
N1.Checked := WordWrap;
end;
13、字数统计
var
s:string;
i,e,c:integer;
begin
s:=editor.lines.text;
e:=0;
c:=0;
for i:=1 to length(s) do
begin
if (ord (s[i])>=33) and (ord(s[i])<=126) then
begin
inc(e);
StatusBar1.Panels[2].Text:='字母个数:'+inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
StatusBar1.Panels[3].Text:='汉字个数:'+inttostr(c div 2);
end;
end;
end;
14、单击控件查找,在属性events的onfind双击,添加代码:
var
FoundPos,StartPos,FindLen:integer;
st:TSearchTypes;
begin
if not (frDown in DgFind.Options) then
begin
DgFind.CloseDialog;
ShowMessage('请向下查找。');
exit;
end;
StartPos:=Editor.SelStart+Editor.SelLength; //StartPos代表初始查找位置
FindLen:=Length(Editor.Text)-StartPos;
//FindLen代表从StartPos开始到文件末尾的长度
st:=[];
if frMatchCase in DgFind.Options then //匹配大小写
st:=st+[stMatchCase];
if frWholeWord in DgFind.Options then
//匹配整个词
st:=st+[stWholeWord];
FoundPos:=Editor.FindText(DgFind.FindText,StartPos,FindLen,st);
//开始查找
DgFind.CloseDialog;
if FoundPos <> -1 then
//找到
begin
Editor.SelStart:=FoundPos;
Editor.SelLength:=Length(DgFind.FindText);
end
else
//未找到
ShowMessage('未找到 '+DgFind.FindText);
end;
15、单击替换控件,在属性events的onreplace双击,添加如下代码:
var
FoundPos,StartPos,FindLen:integer;
st:TSearchTypes;
begin
if not (frDown in DgReplace.Options) then
begin
DgReplace.CloseDialog;
ShowMessage('请向下替换。');
exit;
end;
StartPos:=Editor.SelStart+Editor.SelLength; //StartPos代表初始查找位置
FindLen:=Length(Editor.Text)-StartPos;
//FindLen代表从StartPos开始到文件末尾的长度
st:=[];
if frMatchCase in DgReplace.Options then //匹配大小写
st:=st+[stMatchCase];
if frWholeWord in DgReplace.Options then
//匹配整个词
st:=st+[stWholeWord];
FoundPos:=Editor.FindText(DgReplace.FindText,StartPos,FindLen,st);
//开始查找
DgReplace.CloseDialog;
//关闭替换对话框
if FoundPos <> -1 then
//找到
begin
while FoundPos <> -1 do
begin
Editor.SelStart:=FoundPos;
Editor.SelLength:=Length(DgReplace.FindText);
Editor.SelText:=DgReplace.ReplaceText;
//把文本替换掉
if not (frReplaceAll in DgReplace.Options) then //如果不是全部替换
break;
StartPos:=Editor.SelStart+Length(DgReplace.ReplaceText);
FindLen:=Length(Editor.Text)-StartPos;
//FindLen代表从StartPos开始到文件末尾的长度
FoundPos:=Editor.FindText(DgReplace.FindText,
StartPos,FindLen,st);
//继续查找
end;
ShowMessage('替换完毕!');
end
else
//未找到
ShowMessage('未找到 '+DgReplace.FindText);
end;
16、在system里加timer控件并双击,添加代码:
var
ADate:TDateTime;
ss:string;
Days:array[1..7] of string;
begin
days[1]:='星期日';
days[2]:='星期一';
days[3]:='星期二';
days[4]:='星期三';
days[5]:='星期四';
days[6]:='星期五';
days[7]:='星期六';
ADate:=strtodate(FormatDateTime('yyyy-mm-dd',date));
ss:=FormatDateTime('yyyy"年"m"月"d"日"hh:nn:ss',now);
StatusBar1.Panels[1].Text:=ss+days[DayOfWeek(ADate)];
end;
17、添加一个toolbar的控件,我晕,写到这里,都忘了添加imagelist的控件了没有了,如果没有,这时就要添加这个控件了,单击右键,点imatelist
editor,在弹出的对话框中,找出一些相对映的图片。把toolbar的属性inages改成imagelist1。然后在toolbar上单击右键new button,在属性imagelndex中,选合适的图标,在hint中写出相关的名字,这样,鼠标经过时,就会有相应的提示。然后在events属性中,点onclick,选择和此图标相对应的事件。这里任意加多少个图标。
18单击右键弹出菜单的选项,也是在对应的onclick中选择相对应的事件。
19,对了,在文件的下拉菜单中,在属性shortcut中,选择合适的快捷键。
不好意思呀,我是菜鸟,初学delphi,我也记不清有没有漏掉什么,干脆把整个代码贴出来。
对了,还有关于,就是新建一个form,然后添加几个label的控件,写上自己想写的话,然后保存就ok了,当然,如果愿意,就加一个按钮,代码就是colse;
然后就是帮助主题,也是新建一个form,然后添加label控件或是别的文本编辑的控件,内容自己随便添加啦,不过最好把属性改为只读。
unit editfm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, StdCtrls, ComCtrls, Menus,AboutFM,ToolWin, ExtCtrls;
type
Teditform = class(TForm)
MainMenu1: TMainMenu;
mifile: TMenuItem;
miedit: TMenuItem;
miformat: TMenuItem;
mihelp: TMenuItem;
minew: TMenuItem;
miopen: TMenuItem;
misave: TMenuItem;
misaveas: TMenuItem;
miprint: TMenuItem;
miprintsetup: TMenuItem;
N1: TMenuItem;
miexit: TMenuItem;
N2: TMenuItem;
miundo: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
N10: TMenuItem;
N11: TMenuItem;
N12: TMenuItem;
mileft: TMenuItem;
micenter: TMenuItem;
miright: TMenuItem;
N13: TMenuItem;
N14: TMenuItem;
N15: TMenuItem;
N16: TMenuItem;
N17: TMenuItem;
N18: TMenuItem;
miabort: TMenuItem;
N19: TMenuItem;
PopupMenu1: TPopupMenu;
N20: TMenuItem;
N21: TMenuItem;
N22: TMenuItem;
N23: TMenuItem;
N24: TMenuItem;
N25: TMenuItem;
N26: TMenuItem;
N27: TMenuItem;
N28: TMenuItem;
Editor: TRichEdit;
dgopen: TOpenDialog;
dgsave: TSaveDialog;
dgfont: TFontDialog;
dgprint: TPrintDialog;
dgPrinterSetup: TPrinterSetupDialog;
dgFind: TFindDialog;
dgReplace: TReplaceDialog;
ImageList1: TImageList;
ToolBar1: TToolBar;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ToolButton8: TToolButton;
ToolButton9: TToolButton;
ToolButton10: TToolButton;
ToolButton11: TToolButton;
ToolButton12: TToolButton;
StatusBar1: TStatusBar;
ToolButton13: TToolButton;
ToolButton1: TToolButton;
ToolButton14: TToolButton;
ToolButton15: TToolButton;
N29: TMenuItem;
N30: TMenuItem;
ColorDialog1: TColorDialog;
Timer1: TTimer;
procedure miexitClick(Sender: TObject);
procedure minewClick(Sender: TObject);
procedure misaveasClick(Sender: TObject);
procedure misaveClick(Sender: TObject);
procedure miopenClick(Sender: TObject);
procedure miprintClick(Sender: TObject);
procedure miprintsetupClick(Sender: TObject);
procedure miundoClick(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N9Click(Sender: TObject);
procedure N11Click(Sender: TObject);
procedure N12Click(Sender: TObject);
procedure dgFindFind(Sender: TObject);
procedure dgReplaceReplace(Sender: TObject);
procedure N17Click(Sender: TObject);
procedure mileftClick(Sender: TObject);
procedure micenterClick(Sender: TObject);
procedure mirightClick(Sender: TObject);
procedure miabortClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure N14Click(Sender: TObject);
procedure N15Click(Sender: TObject);
procedure N16Click(Sender: TObject);
procedure N18Click(Sender: TObject);
procedure N30Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
pathname:string;
hasundo:boolean;
public
{ Public declarations }
procedure CheckTextModified;
end;
var
editform: Teditform;
const
defaulfgilename='晨烟记事本';
implementation
{$R *.dfm}
procedure TEditForm.CheckTextModified;
begin
if Editor.Modified then
//修改过
begin
case Application.MessageBox('文件没有保存!要保存吗?',
'提示',MB_YESNO+MB_ICONQUESTION) of
IDYES:
//保存文件
MISaveClick(Self);
//保存文件
end;
end;
end;
procedure Teditform.miexitClick(Sender: TObject);
begin
close;
end;
procedure Teditform.minewClick(Sender: TObject);
begin
CheckTextModified;
//检测是否做了修改
Editor.Lines.Clear;
//清除所有文本
Editor.Modified:=False;
//把Modified属性设为False
editform.Caption:='晨烟记事本'+'--'+'新文档';
end;
procedure Teditform.misaveasClick(Sender: TObject);
begin
DgSave.FileName := '新文档';
if DgSave.Execute then
begin
PathName := DgSave.FileName;
//获取路径名
if DgSave.FilterIndex=1 then
//txt文件
begin
Editor.PlainText:=True;
PathName:=PathName+'.txt';
end;
if DgSave.FilterIndex=2 then
//rtf文件
begin
Editor.PlainText:=False;
PathName:=PathName+'.rtf';
end;
if DgSave.FilterIndex=3 then
//所有文件
begin
Editor.PlainText:=False;
end;
Editor.Lines.SaveToFile(PathName); //保存文件
Editor.Modified := False;
//设置Editor为未改变
Caption := ExtractFileName(PathName);
//取出文件名,设为窗口标题
StatusBar1.Panels[1].Text:='';
end;
end;
procedure Teditform.misaveClick(Sender: TObject);
begin
if PathName = '晨烟记事本' then
//第一次保存
MISaveAsClick(Sender)
else
begin
Editor.Lines.SaveToFile(PathName); //已经保存过
Editor.Modified := False;
StatusBar1.Panels[1].Text:='';
end;
end;
procedure Teditform.miopenClick(Sender: TObject);
begin
DgOpen.Execute;
end;
procedure Teditform.miprintClick(Sender: TObject);
begin
if DgPrint.Execute then
Editor.Print(PathName);
end;
procedure Teditform.miprintsetupClick(Sender: TObject);
begin
DgPrinterSetup.Execute;
end;
procedure Teditform.miundoClick(Sender: TObject);
begin
if not HasUndo then
begin
Editor.Undo;
HasUndo:=True;
end;
end;
procedure Teditform.N3Click(Sender: TObject);
begin
if HasUndo then
begin
Editor.Undo;
HasUndo:=False;
end;
end;
procedure Teditform.N5Click(Sender: TObject);
begin
Editor.CutToClipboard;
end;
procedure Teditform.N6Click(Sender: TObject);
begin
Editor.CopyToClipboard;
end;
procedure Teditform.N7Click(Sender: TObject);
begin
Editor.PasteFromClipboard;
end;
procedure Teditform.N8Click(Sender: TObject);
begin
Editor.ClearSelection;
end;
procedure Teditform.N9Click(Sender: TObject);
begin
Editor.SelectAll;
end;
procedure Teditform.N11Click(Sender: TObject);
begin
DgFind.Execute;
end;
procedure Teditform.N12Click(Sender: TObject);
begin
DgReplace.Execute;
end;
procedure Teditform.dgFindFind(Sender: TObject);
var
FoundPos,StartPos,FindLen:integer;
st:TSearchTypes;
begin
if not (frDown in DgFind.Options) then
begin
DgFind.CloseDialog;
ShowMessage('请向下查找。');
exit;
end;
StartPos:=Editor.SelStart+Editor.SelLength; //StartPos代表初始查找位置
FindLen:=Length(Editor.Text)-StartPos;
//FindLen代表从StartPos开始到文件末尾的长度
st:=[];
if frMatchCase in DgFind.Options then //匹配大小写
st:=st+[stMatchCase];
if frWholeWord in DgFind.Options then
//匹配整个词
st:=st+[stWholeWord];
FoundPos:=Editor.FindText(DgFind.FindText,StartPos,FindLen,st);
//开始查找
DgFind.CloseDialog;
if FoundPos <> -1 then
//找到
begin
Editor.SelStart:=FoundPos;
Editor.SelLength:=Length(DgFind.FindText);
end
else
//未找到
ShowMessage('未找到 '+DgFind.FindText);
end;
procedure Teditform.dgReplaceReplace(Sender: TObject);
var
FoundPos,StartPos,FindLen:integer;
st:TSearchTypes;
begin
if not (frDown in DgReplace.Options) then
begin
DgReplace.CloseDialog;
ShowMessage('请向下替换。');
exit;
end;
StartPos:=Editor.SelStart+Editor.SelLength; //StartPos代表初始查找位置
FindLen:=Length(Editor.Text)-StartPos;
//FindLen代表从StartPos开始到文件末尾的长度
st:=[];
if frMatchCase in DgReplace.Options then //匹配大小写
st:=st+[stMatchCase];
if frWholeWord in DgReplace.Options then
//匹配整个词
st:=st+[stWholeWord];
FoundPos:=Editor.FindText(DgReplace.FindText,StartPos,FindLen,st);
//开始查找
DgReplace.CloseDialog;
//关闭替换对话框
if FoundPos <> -1 then
//找到
begin
while FoundPos <> -1 do
begin
Editor.SelStart:=FoundPos;
Editor.SelLength:=Length(DgReplace.FindText);
Editor.SelText:=DgReplace.ReplaceText;
//把文本替换掉
if not (frReplaceAll in DgReplace.Options) then //如果不是全部替换
break;
StartPos:=Editor.SelStart+Length(DgReplace.ReplaceText);
FindLen:=Length(Editor.Text)-StartPos;
//FindLen代表从StartPos开始到文件末尾的长度
FoundPos:=Editor.FindText(DgReplace.FindText,
StartPos,FindLen,st);
//继续查找
end;
ShowMessage('替换完毕!');
end
else
//未找到
ShowMessage('未找到 '+DgReplace.FindText);
end;
procedure Teditform.N17Click(Sender: TObject);
begin
with Editor do
begin
WordWrap := not WordWrap; //让WordWrap取反
if WordWrap then
//自动换行
ScrollBars := ssVertical
//Editor只有竖直滚动条
else
ScrollBars := ssBoth;
N1.Checked := WordWrap;
end;
end;
procedure Teditform.mileftClick(Sender: TObject);
begin
MILeft.Checked := True;
MICenter.Checked := False;
MIRight.Checked := False;
Editor.Paragraph.Alignment := taLeftJustify;
end;
procedure Teditform.micenterClick(Sender: TObject);
begin
MILeft.Checked := False;
MICenter.Checked := True;
MIRight.Checked := False;
Editor.Paragraph.Alignment := taCenter;;
end;
procedure Teditform.mirightClick(Sender: TObject);
begin
MILeft.Checked := False;
MICenter.Checked := False;
MIRight.Checked := True;
Editor.Paragraph.Alignment := taRightJustify ;
end;
procedure Teditform.miabortClick(Sender: TObject);
begin
AboutForm.ShowModal;
end;
procedure Teditform.FormCreate(Sender: TObject);
begin
PathName := '晨烟记事本';
HasUndo:=False;
end;
procedure Teditform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CheckTextModified;
end;
procedure Teditform.N14Click(Sender: TObject);
begin
if dgfont.Execute then
editForm.editor.Font:=dgfont.Font;
end;
procedure Teditform.N15Click(Sender: TObject);
begin
with editor.Font do size:=size+1;
end;
procedure Teditform.N16Click(Sender: TObject);
begin
with editor.Font do size:=size-1;
end;
procedure Teditform.N18Click(Sender: TObject);
var
s:string;
i,e,c:integer;
begin
s:=editor.lines.text;
e:=0;
c:=0;
for i:=1 to length(s) do
begin
if (ord (s[i])>=33) and (ord(s[i])<=126) then
begin
inc(e);
StatusBar1.Panels[2].Text:='字母个数:'+inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
StatusBar1.Panels[3].Text:='汉字个数:'+inttostr(c div 2);
end;
end;
end;
procedure Teditform.N30Click(Sender: TObject);
begin
if colordialog1.Execute then
editForm.editor.Color:=colordialog1.color;
end;
procedure Teditform.Timer1Timer(Sender: TObject);
var
ADate:TDateTime;
ss:string;
Days:array[1..7] of string;
begin
days[1]:='星期日';
days[2]:='星期一';
days[3]:='星期二';
days[4]:='星期三';
days[5]:='星期四';
days[6]:='星期五';
days[7]:='星期六';
ADate:=strtodate(FormatDateTime('yyyy-mm-dd',date));
ss:=FormatDateTime('yyyy"年"m"月"d"日"hh:nn:ss',now);
StatusBar1.Panels[1].Text:=ss+days[DayOfWeek(ADate)];
end;
end.