鱼C记事本V1.0
有没有那位大神知道怎么实现全部替换功能?unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls,ShellAPI;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
file1: TMenuItem;
new1: TMenuItem;
open1: TMenuItem;
save1: TMenuItem;
N1: TMenuItem;
exit1: TMenuItem;
edit1: TMenuItem;
undo1: TMenuItem;
N2: TMenuItem;
copy1: TMenuItem;
paste1: TMenuItem;
cut1: TMenuItem;
delete1: TMenuItem;
N3: TMenuItem;
all1: TMenuItem;
N4: TMenuItem;
find1: TMenuItem;
replace1: TMenuItem;
N5: TMenuItem;
date: TMenuItem;
O1: TMenuItem;
font1: TMenuItem;
help1: TMenuItem;
about1: TMenuItem;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
FindDialog1: TFindDialog;
ReplaceDialog1: TReplaceDialog;
ColorDialog1: TColorDialog;
FontDialog1: TFontDialog;
background1: TMenuItem;
procedure open1Click(Sender: TObject);
procedure save1Click(Sender: TObject);
procedure new1Click(Sender: TObject);
procedure exit1Click(Sender: TObject);
procedure undo1Click(Sender: TObject);
procedure copy1Click(Sender: TObject);
procedure paste1Click(Sender: TObject);
procedure cut1Click(Sender: TObject);
procedure delete1Click(Sender: TObject);
procedure all1Click(Sender: TObject);
procedure dateClick(Sender: TObject);
procedure font1Click(Sender: TObject);
procedure background1Click(Sender: TObject);
procedure about1Click(Sender: TObject);
procedure find1Click(Sender: TObject);
procedure FindDialog1Find(Sender: TObject);
procedure replace1Click(Sender: TObject);
procedure ReplaceDialog1Find(Sender: TObject);
procedure ReplaceDialog1Replace(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
FileName : String;
k: Integer = 0;
m: Integer =0;
{$R *.dfm}
procedure TForm1.open1Click(Sender: TObject);
begin
OpenDialog1.Filter := '文本文档(*.txt)|*.txt';
if OpenDialog1.Execute then
begin
FileName := OpenDialog1.FileName;
Memo1.Lines.LoadFromFile(FileName);
end
else
exit;
end;
procedure TForm1.save1Click(Sender: TObject);
begin
SaveDialog1.Filter := '文本文档(*.TXT)|*.TXT';
if SaveDialog1.Execute then
begin
FileName := SaveDialog1.FileName + '.txt' ;
Memo1.Lines.SaveToFile(FileName);
end
else
exit;
end;
procedure TForm1.new1Click(Sender: TObject);
begin
if Memo1.Modified = True then
begin
if ID_OK = MessageBox(Handle,'内容以发生改变,是否保存?','提示',MB_OKCANCEL) then
//判断在选择弹出对话框的按钮时所执行的动作
begin
SaveDialog1.Filter := '文本文档(*.TXT)|*.TXT';
SaveDialog1.Execute;
FileName := SaveDialog1.FileName + '.txt' ;
Memo1.Lines.SaveToFile(FileName);
end
else
Memo1.Lines.Text:= '';
end
else
Memo1.Lines.Text:= ''
end;
procedure TForm1.exit1Click(Sender: TObject);
begin
close;
end;
procedure TForm1.undo1Click(Sender: TObject);
begin
Memo1.Undo; //实现撤销操作
end;
procedure TForm1.copy1Click(Sender: TObject);
begin
Memo1.CopyToClipboard ; //将选中的部分复制到剪贴板
end;
procedure TForm1.paste1Click(Sender: TObject);
begin
Memo1.PasteFromClipboard ; //从剪贴板粘贴内容
end;
procedure TForm1.cut1Click(Sender: TObject);
begin
Memo1.CutToClipboard ; //将选中部分剪贴到剪贴板
end;
procedure TForm1.delete1Click(Sender: TObject);
begin
Memo1.ClearSelection; //删除选中部分
end;
procedure TForm1.all1Click(Sender: TObject);
begin
Memo1.SelectAll ; //全选
end;
procedure TForm1.dateClick(Sender: TObject);
begin
Memo1.SelText := DatetimeToStr (now())//将系统当时的时间日期插入到光标所在位置 DatetimeToStr返回的是字符型数据
end;
procedure TForm1.font1Click(Sender: TObject);
begin
if FontDialog1.Execute then //弹出字体设置对话框
Memo1.Font := FontDialog1.Font
else
exit;
end;
procedure TForm1.background1Click(Sender: TObject);
begin
if ColorDialog1.Execute then //弹出设置颜色对话框
Memo1.Color := ColorDialog1.color
else
exit;
end;
procedure TForm1.about1Click(Sender: TObject);
begin
ShellAbout (self.Handle,pChar('鱼C记事本V1.0'),pChar('鱼C工作室——小甲鱼'),HICON(nil))
// ShellAbout函数使用前应现在uses下添加 ShellAPI
end;
procedure TForm1.find1Click(Sender: TObject);
begin
FindDialog1.FindText := Memo1.SelText ;
FindDialog1.Execute ;
end;
procedure TForm1.FindDialog1Find(Sender: TObject);
var
i,j,PosReturn,SkipChars : Integer;
begin
if (k<=Memo1.Lines.Count) then
begin
for i := k to Memo1.Lines.Count do
begin
PosReturn := Pos (FindDialog1.FindText ,Memo1.Lines );
if PosReturn <> 0 then
begin
SkipChars := 0;
for j := 0 to i - 1 do
SkipChars := SkipChars + Length(Memo1.Lines);
SkipChars := SkipChars + (i*2); //在每次进行换行的时候,要按回车,占用2个字节,所以从第1行结束开始,每行都要多2个字节
SkipChars := SkipChars + PosReturn - 1;
Memo1.SetFocus;
Memo1.SelStart := SkipChars;
Memo1.SelLength := Length(FindDialog1.FindText );
Break;
end;
k := k+1;
end;
k := k+1 //K就实现了逐行查找,即查找下一个按键
end
else
begin
k := 0 ;
ShowMessage ('找不到'+ FindDialog1.FindText);
end;
end;
procedure TForm1.replace1Click(Sender: TObject);
begin
ReplaceDialog1.FindText := Memo1.SelText ;
ReplaceDialog1.Execute ;
end;
procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
i,j,PosReturn,SkipChars : Integer;
begin
if (m<=Memo1.Lines.Count) then
begin
for i := m to Memo1.Lines.Count do
begin
PosReturn := Pos (ReplaceDialog1.FindText ,Memo1.Lines );
if PosReturn <> 0 then
begin
SkipChars := 0;
for j := 0 to i - 1 do
SkipChars := SkipChars + Length(Memo1.Lines);
SkipChars := SkipChars + (i*2);
SkipChars := SkipChars + PosReturn - 1;
Memo1.SetFocus;
Memo1.SelStart := SkipChars;
Memo1.SelLength := Length(ReplaceDialog1.FindText );
Break;
end;
m := m+1;
end;
m := m+1
end
else
begin
m := 0 ;
ShowMessage ('找不到'+ ReplaceDialog1.FindText);
end;
end;
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
Memo1.SelText := ReplaceDialog1.ReplaceText ;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Memo1.Modified = True then
begin
if ID_OK = MessageBox(Handle,'内容以发生改变,是否保存?','提示',MB_OKCANCEL) then
//判断在选择弹出对话框的按钮时所执行的动作
begin
SaveDialog1.Filter := '文本文档(*.TXT)|*.TXT';
SaveDialog1.Execute;
FileName := SaveDialog1.FileName + '.txt' ;
Memo1.Lines.SaveToFile(FileName);
end
else
exit;
end;
end;
end.
页:
[1]