|
10鱼币
在小甲鱼老师的:028鱼C记事本V1.0(下) - 阶段性上机实践(小结)04 - 零基础入门学习Delphi028的原代码如下。但我在下面红色字体部分的部分老是搞不清是什么意思,请高手解答一下,谢谢了先!
代码如下:
var
Form1: TForm1;
implementation
{$R *.dfm}
var
FileName: String;
procedure TForm1.New1Click(Sender: TObject);
begin
If Memo1.Modified = True then
begin
If ID_OK = MessageBox(0, '内容已经发生变化,是否需要保存!', '提醒', MB_OKCANCEL Or MB_ICONWARNING) then
begin
SaveDialog1.Filter:= '文本文件(*.txt)|*.TXT';
If SaveDialog1.Execute then
begin
FileName:= SaveDialog1.FileName + '.txt';
Memo1.Lines.SaveToFile(FileName);
end
Else
Exit;
end;
end
Else
Memo1.Lines.Clear;
end;
procedure TForm1.Open1Click(Sender: TObject);
begin
OpenDialog1.Filter:= '文本文件(*.txt)|*.txt'; // 过滤器
If OpenDialog1.Execute then
begin
FileName:= OpenDialog1.FileName + '.txt';
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;
Memo1.Lines.SaveToFile(FileName);
end
Else
Exit;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Undo1Click(Sender: TObject);
begin
Memo1.Undo;
end;
procedure TForm1.Cut1Click(Sender: TObject);
begin
Memo1.CutToClipboard;
end;
procedure TForm1.Copy1Click(Sender: TObject);
begin
Memo1.CopyToClipboard;
end;
procedure TForm1.Paste1Click(Sender: TObject);
begin
Memo1.PasteFromClipboard;
end;
procedure TForm1.Delete1Click(Sender: TObject);
begin
Memo1.ClearSelection;
end;
procedure TForm1.CtrlA1Click(Sender: TObject);
begin
Memo1.SelectAll;
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
For i:= 0 to Memo1.Lines.Count do
begin
PosReturn:= Pos(FindDialog1.FindText, Memo1.Lines[i]);
If PosReturn <> 0 then
begin
Skipchars:= 0;
For j:= 0 to i-1 do
begin
Skipchars:= Skipchars + Length(Memo1.Lines[j]);
end;
SkipChars:= SkipChars + (i*2);//这里的i 为什么要乘以2?;
SkipChars:= SkipChars + PosReturn - 1;
Memo1.SetFocus;
Memo1.SelStart:= SkipChars;
Memo1.SelLength:= Length(FindDialog1.FindText);
Break;
end;
end;
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));
end;
procedure TForm1.InsertDate1Click(Sender: TObject);
begin
Memo1.SelText:= DatetimeToStr(now());
end;
procedure TForm1.Replace1Click(Sender: TObject);
begin
ReplaceDialog1.ReplaceText:= Memo1.SelText;
ReplaceDialog1.Execute;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If Memo1.Modified = True then
begin
If ID_OK = MessageBox(0, '内容已经发生变化,是否需要保存!', '提醒', MB_OKCANCEL Or MB_ICONWARNING) then
begin
SaveDialog1.Filter:= '文本文件(*.txt)|*.TXT';
If SaveDialog1.Execute then
begin
FileName:= SaveDialog1.FileName + '.txt';
Memo1.Lines.SaveToFile(FileName);
end
Else
Exit;
end;
end;
end;
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var
SelPos: Integer;
begin
with TReplaceDialog(Sender) do
begin
SelPos:= Pos(FindText, Memo1.Lines.Text);
if SelPos > 0 then
begin
Memo1.SelStart := SelPos - 1;
Memo1.SelLength := Length(FindText);
Memo1.SelText := ReplaceText;
end
else
MessageBox(0, '抱歉,找不到需要替换的文件!', '提醒!', MB_OK Or MB_ICONWARNING);
end;
end;
end.
|
最佳答案
查看完整内容
For j:= 0 to i-1 do
begin
Skipchars:= Skipchars + Length(Memo1.Lines[j]);
end;
这个循环将前面所有行的字符数加起来,但是没有加上换行符
SkipChars:= SkipChars + (i*2);
就把所有的换行符加上了
SkipChars:= SkipChars + PosReturn - 1
就是把本行Find到的字符串前面的字符数加起来
|