📄 013.htm
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>—DELPHI基础教程—第四章 文本编辑器的设计(二)</TITLE><META NAME="keywords" CONTENT=" DELPHI基础教程 第四章 文本编辑器的设计(二)"><META NAME="description" CONTENT=" - DELPHI基础教程 - 第四章 文本编辑器的设计(二)"><style><!--#page {position:absolute; z-index:0; left:0px; top:0px}.tt3 {font: 9pt/12pt "宋体"}.tt2 {font: 12pt/15pt "宋体"}a {text-decoration:none}a:hover {color: blue;text-decoration:underline}--></style></HEAD><body text="#000000" bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3"><TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0><TR><TD CLASS="tt3" VALIGN="top" width="8%" ><strong><A HREF="014.htm"><FONT style="FONT-SIZE: 9pt">后一页</font></A><BR><A HREF="012.htm"><FONT style="FONT-SIZE: 9pt">前一页</font></A><BR><A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目录</font></A><BR></strong></TD><TD class="tt2" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="隶书">第四章 文本编辑器的设计(二)</FONT></B></center><hr width="94%"></font><p>4.4.2<font face="宋体" lang="ZH-CN">查找对话框部件 </p><p> 查找对话框部件为应用程序提供查找对话框,</font> <fontface="宋体" lang="ZH-CN">用户可使用查找对话框在文本文件中查找字符串。</p><p> 可用</font>Execult<font face="宋体" lang="ZH-CN">方法显示查找对话框,如图</font>4.8<fontface="宋体" lang="ZH-CN">。应用程序要查找的字符放到</font>FindText<fontface="宋体" lang="ZH-CN">属性中。</font>Options <font face="宋体" lang="ZH-CN">属性可决定查找对话框中有哪些选项。例如,</font><font face="宋体" lang="ZH-CN">用户可选择是否显示匹配检查框。</font>Options<fontface="宋体" lang="ZH-CN">的常用选项如表</font>4.2<font face="宋体" lang="ZH-CN">所示。</font></p><font face="宋体" lang="ZH-CN"><p>如果用户在对话框中输入字符并选择</font>FindNext<fontface="宋体" lang="ZH-CN">按钮,对话框将发生</font>OnFind<font face="宋体"lang="ZH-CN">事件。 </p></font><p><font face="宋体" lang="ZH-CN">表</font>4.2 <font face="宋体" lang="ZH-CN">查找对话框的</font>Options<fontface="宋体" lang="ZH-CN">属性的取值及含义</font></p><p><font face="宋体" lang="ZH-CN">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━</font></p><p><font face="宋体" lang="ZH-CN">取值 </font> <fontface="宋体" lang="ZH-CN">含义</font></p><p><font face="宋体" lang="ZH-CN">───────────────────────────────────────</font></p><p>frDown <font face="宋体" lang="ZH-CN">如果是真值,对话框中出现</font>Down<fontface="宋体" lang="ZH-CN">按钮,查找方向向下。如果是假</font></p><p><font face="宋体" lang="ZH-CN">值,</font>Up<font face="宋体" lang="ZH-CN">按钮将被选中,查找方向向上,</font>frDown<font face="宋体" lang="ZH-CN">值可在设计或运行</font></p><p><font face="宋体" lang="ZH-CN">时设置。</font></p><p>frDisableUpDown <font face="宋体" lang="ZH-CN">如果是真值,</font>Up<fontface="宋体" lang="ZH-CN">和</font>Down<font face="宋体" lang="ZH-CN">按钮将变灰,用户不能进行选取;如果是</font></p><p><font face="宋体" lang="ZH-CN">假值,用户可以选择其中之一。</font></p><p>frFindNext <font face="宋体" lang="ZH-CN">如果是真值,应用程序查找在</font>FindNext<fontface="宋体" lang="ZH-CN">属性中的字符串。</font></p><p>frMatchCase <font face="宋体" lang="ZH-CN">如果是真值,匹配检查框被选中。设计、运行时均可设置。</font></p><p>frWholeWord <font face="宋体" lang="ZH-CN">如果是真值,整字匹配检查框被选中,设计、运行时均可设置。</font></p><p><font face="宋体" lang="ZH-CN">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ </p><p> 在</font>OnFind<font face="宋体" lang="ZH-CN">事件中可使用</font>Options<fontface="宋体" lang="ZH-CN">属性来决定以何种方式查找。</font>Find<fontface="宋体" lang="ZH-CN">方法响应查找对话框的</font>OnFind<font face="宋体"lang="ZH-CN">事件。 </p><p> </font> procedure TEditform.Find(Sender: TObject);</p><p>begin</p><p>with Sender as TFindDialog do</p><p>if not SearchMemo(Memo1, FindText, Options) then</p><p>ShowMessage('Cannot find "' + FindText + '".');</p><p>end;</p><p><font face="宋体" lang="ZH-CN"> 其中</font>SearchMemo<fontface="宋体" lang="ZH-CN">函数是</font>Search<font face="宋体" lang="ZH-CN">单元中定义的,</font>SearchMemo<fontface="宋体" lang="ZH-CN">可在</font>TEdit,TMemo<font face="宋体" lang="ZH-CN">,以及其它</font>TCustomEdit<fontface="宋体" lang="ZH-CN">派生类中查找指定的字符串。查找从控件的脱字号</font>(^)<fontface="宋体" lang="ZH-CN">开始,</font> <font face="宋体" lang="ZH-CN">查找方式由</font>Options<fontface="宋体" lang="ZH-CN">决定。如果向后查找从控件的</font>StlStart<fontface="宋体" lang="ZH-CN">处开始,如果向前查找则从控件的</font>SelEnd<fontface="宋体" lang="ZH-CN">处查找。</p><p> 如果在控件中找到相匹配的字符串,则字符串被选中,函数返回真值。如无匹配的字符串,函数返回假值。</p><p> 特别注意的是</font>TEdit,TMemo<font face="宋体" lang="ZH-CN">中有一个</font>HideSeletion<fontface="宋体" lang="ZH-CN">属性,它决定当焦点从该控制转移至其它控制时,被选中的字符是否保持被选中的状态。如果是真值,则只有获得焦点才能保持被选中状态。查找时,焦点在查找对话框上,因此要想了解查找情况,必须将</font>HideSeletion<fontface="宋体" lang="ZH-CN">设成假值。控制的缺省值为真值。</p><p> </font>SearchMemo<font face="宋体" lang="ZH-CN">代码如下: </p></font><p>unit Search;</p><p>interface</p><p>uses WinProcs, SysUtils, StdCtrls, Dialogs;</p><p>const</p><p>WordDelimiters: set of Char = [#0..#255] - ['a'..'z','A'..'Z','1'..'9','0']; </p><p>function SearchMemo(Memo: TCustomEdit;</p><p>const SearchString: String;</p><p>Options: TFindOptions): Boolean; </p><p>function SearchBuf(Buf: PChar; BufLen: Integer;</p><p>SelStart, SelLength: Integer;</p><p>SearchString: String;</p><p>Options: TFindOptions): PChar; </p><p>implementation </p><p>function SearchMemo(Memo: TCustomEdit;</p><p>const SearchString: String;</p><p>Options: TFindOptions): Boolean;</p><p>var</p><p>Buffer, P: PChar;</p><p>Size: Word;</p><p>begin</p><p>Result := False;</p><p>if (Length(SearchString) = 0) then Exit;</p><p>Size := Memo.GetTextLen;</p><p>if (Size = 0) then Exit;</p><p>Buffer := StrAlloc(Size + 1);</p><p>try</p><p>Memo.GetTextBuf(Buffer, Size + 1);</p><p>P := SearchBuf(Buffer, Size, Memo.SelStart,</p><p>Memo.SelLength,SearchString, Options);</p><p>if P <> nil then</p><p>begin</p><p>Memo.SelStart := P - Buffer;</p><p>Memo.SelLength := Length(SearchString);</p><p>Result := True;</p><p>end;</p><p>finally</p><p>StrDispose(Buffer);</p><p>end;</p><p>end; </p><p>function SearchBuf(Buf: PChar; BufLen: Integer;</p><p>SelStart, SelLength: Integer;</p><p>SearchString: String;</p><p>Options: TFindOptions): PChar;</p><p>var</p><p>SearchCount, I: Integer;</p><p>C: Char;</p><p>Direction: Shortint;</p><p>CharMap: array [Char] of Char; </p><p>function FindNextWordStart(var BufPtr: PChar): Boolean;</p><p>begin { (True XOR N) is equivalent to</p><p>(not N) }</p><p>Result := False; { (False XOR N) is equivalent</p><p>to (N) }</p><p>{ When Direction is forward (1), skip non</p><p>delimiters, then skip delimiters. }</p><p>{ When Direction is backward (-1), skip delims, then</p><p>skip non delims }</p><p>while (SearchCount > 0) and</p><p>((Direction = 1) xor (BufPtr^ in</p><p>WordDelimiters)) do</p><p>begin</p><p>Inc(BufPtr, Direction);</p><p>Dec(SearchCount);</p><p>end;</p><p>while (SearchCount > 0) and</p><p>((Direction = -1) xor (BufPtr^ in</p><p>WordDelimiters)) do</p><p>begin</p><p>Inc(BufPtr, Direction);</p><p>Dec(SearchCount);</p><p>end;</p><p>Result := SearchCount > 0;</p><p>if Direction = -1 then</p><p>begin { back up one char, to leave ptr on first non</p><p>delim }</p><p>Dec(BufPtr, Direction);</p><p>Inc(SearchCount);</p><p>end;</p><p>end; </p><p>begin</p><p>Result := nil;</p><p>if BufLen <= 0 then Exit;</p><p>if frDown in Options then</p><p>begin</p><p>Direction := 1;</p><p>Inc(SelStart, SelLength); { start search past end of</p><p>selection }</p><p>SearchCount := BufLen - SelStart - Length(SearchString);</p><p>if SearchCount < 0 then Exit;</p><p>if Longint(SelStart) + SearchCount > BufLen then</p><p>Exit;</p><p>end</p><p>else</p><p>begin</p><p>Direction := -1;</p><p>Dec(SelStart, Length(SearchString));</p><p>SearchCount := SelStart;</p><p>end;</p><p>if (SelStart < 0) or (SelStart > BufLen) then Exit;</p><p>Result := @Buf[SelStart]; </p><p>{ Using a Char map array is faster than calling</p><p>AnsiUpper on every character }</p><p>for C := Low(CharMap) to High(CharMap) do</p><p>CharMap[C] := C; </p><p>if not (frMatchCase in Options) then</p><p>begin</p><p>AnsiUpperBuff(PChar(@CharMap), sizeof(CharMap));</p><p>AnsiUpperBuff(@SearchString[1],</p><p>Length(SearchString));</p><p>end; </p><p>while SearchCount > 0 do</p><p>begin</p><p>if frWholeWord in Options then</p><p>if not FindNextWordStart(Result) then Break;</p><p>I := 0;</p><p>while (CharMap[Result[I]] = SearchString[I+1]) do</p><p>begin</p><p>Inc(I);</p><p>if I >= Length(SearchString) then</p><p>begin</p><p>if (not (frWholeWord in Options)) or</p><p>(SearchCount = 0) or</p><p>(Result[I] in WordDelimiters) then</p><p>Exit;</p><p>Break;</p><p>end;</p><p>end;</p><p>Inc(Result, Direction);</p><p>Dec(SearchCount);</p><p>end;</p><p>Result := nil;</p><p>end; </p><p>end.</p><p><font face="宋体" lang="ZH-CN"> </font>4.4.3 <font face="宋体" lang="ZH-CN">替换对话框部件 </font></p><font face="宋体" lang="ZH-CN"><p> 替换对话框部件为应用程序提供替换对话框。如图</font>4.9<fontface="宋体" lang="ZH-CN">。它包括查找对话框的所有功能,此外还允许使用者更换被选中的字符串。</font>FindText<font face="宋体" lang="ZH-CN">属性是应用程序需查找的字符串。</font>ReplaceText<fontface="宋体" lang="ZH-CN">属性是被选中字符的替换字符串。</font>Options <fontface="宋体" lang="ZH-CN">属性决定对话框的显示方式。其值如表</font>4.3<fontface="宋体" lang="ZH-CN">所示。</font></p><font face="宋体" lang="ZH-CN"><p>与查找对话框一样,替换对话框亦有</font>OnFind<font face="宋体" lang="ZH-CN">事件。用户输入查找字符串并按</font>FindNext<fontface="宋体" lang="ZH-CN">按钮时,发生</font>OnFind <font face="宋体"lang="ZH-CN">事件。用户选择</font>Replace <font face="宋体" lang="ZH-CN">或</font>ReplacAll<font face="宋体" lang="ZH-CN">时,</font> <font face="宋体" lang="ZH-CN">对话框发生</font>OnRelpace<fontface="宋体" lang="ZH-CN">事件,要替换的字符串存入</font>ReplaceText<fontface="宋体" lang="ZH-CN">属性中,要编写相应的代码以支持替换功能。 </p></font><p><font face="宋体" lang="ZH-CN"> 表</font>4.3 <font face="宋体"lang="ZH-CN">替换对话框的</font>Options<font face="宋体" lang="ZH-CN">属性的取值及含义</font></p><p><font face="宋体" lang="ZH-CN">━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━</font></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -