首页 / 软件开发 / Delphi / 第四章-文本编辑器的设计(二)(1)
第四章-文本编辑器的设计(二)(1)2007-05-084.4.2查找对话框部件 查找对话框部件为应用程序提供查找对话框, 用户可使用查找对话框在文本文件中查找字符串。可用Execult方法显示查找对话框,如图4.8。应用程序要查找的字符放到FindText属性中。Options 属性可决定查找对话框中有哪些选项。例如, 用户可选择是否显示匹配检查框。Options的常用选项如表4.2所示。如果用户在对话框中输入字符并选择FindNext按钮,对话框将发生OnFind事件。 表4.2 查找对话框的Options属性的取值及含义━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━取值 含义───────────────────────────────────────frDown 如果是真值,对话框中出现Down按钮,查找方向向下。如果是假值,Up按钮将被选中,查找方向向上,frDown 值可在设计或运行时设置。frDisableUpDown 如果是真值,Up和Down按钮将变灰,用户不能进行选取;如果是假值,用户可以选择其中之一。frFindNext 如果是真值,应用程序查找在FindNext属性中的字符串。frMatchCase 如果是真值,匹配检查框被选中。设计、运行时均可设置。frWholeWord 如果是真值,整字匹配检查框被选中,设计、运行时均可设置。━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 在OnFind事件中可使用Options属性来决定以何种方式查找。Find方法响应查找对话框的OnFind事件。 procedure TEditform.Find(Sender: TObject);beginwith Sender as TFindDialog doif not SearchMemo(Memo1, FindText, Options) thenShowMessage("Cannot find "" + FindText + "".");end;其中SearchMemo函数是Search单元中定义的,SearchMemo可在TEdit,TMemo,以及其它TCustomEdit派生类中查找指定的字符串。查找从控件的脱字号(^)开始, 查找方式由Options决定。如果向后查找从控件的StlStart处开始,如果向前查找则从控件的SelEnd处查找。如果在控件中找到相匹配的字符串,则字符串被选中,函数返回真值。如无匹配的字符串,函数返回假值。特别注意的是TEdit,TMemo中有一个HideSeletion属性,它决定当焦点从该控制转移至其它控制时,被选中的字符是否保持被选中的状态。如果是真值,则只有获得焦点才能保持被选中状态。查找时,焦点在查找对话框上,因此要想了解查找情况,必须将HideSeletion设成假值。控制的缺省值为真值。SearchMemo代码如下: unit Search;interfaceuses WinProcs, SysUtils, StdCtrls, Dialogs;constWordDelimiters: set of Char = [#0..#255] - ["a".."z","A".."Z","1".."9","0"]; function SearchMemo(Memo: TCustomEdit;const SearchString: String;Options: TFindOptions): Boolean; function SearchBuf(Buf: PChar; BufLen: Integer;SelStart, SelLength: Integer;SearchString: String;Options: TFindOptions): PChar; implementation function SearchMemo(Memo: TCustomEdit;const SearchString: String;Options: TFindOptions): Boolean;varBuffer, P: PChar;Size: Word;beginResult := False;if (Length(SearchString) = 0) then Exit;Size := Memo.GetTextLen;if (Size = 0) then Exit;Buffer := StrAlloc(Size + 1);tryMemo.GetTextBuf(Buffer, Size + 1);P := SearchBuf(Buffer, Size, Memo.SelStart,Memo.SelLength,SearchString, Options);if P <> nil thenbeginMemo.SelStart := P - Buffer;Memo.SelLength := Length(SearchString);Result := True;end;finallyStrDispose(Buffer);end;end; function SearchBuf(Buf: PChar; BufLen: Integer;SelStart, SelLength: Integer;SearchString: String;Options: TFindOptions): PChar;varSearchCount, I: Integer;C: Char;Direction: Shortint;CharMap: array [Char] of Char; function FindNextWordStart(var BufPtr: PChar): Boolean;begin { (True XOR N) is equivalent to(not N) }Result := False; { (False XOR N) is equivalentto (N) }{ When Direction is forward (1), skip nondelimiters, then skip delimiters. }{ When Direction is backward (-1), skip delims, thenskip non delims }while (SearchCount > 0) and((Direction = 1) xor (BufPtr^ inWordDelimiters)) dobeginInc(BufPtr, Direction);Dec(SearchCount);end;while (SearchCount > 0) and((Direction = -1) xor (BufPtr^ inWordDelimiters)) dobeginInc(BufPtr, Direction);Dec(SearchCount);end;Result := SearchCount > 0;if Direction = -1 thenbegin { back up one char, to leave ptr on first nondelim }Dec(BufPtr, Direction);Inc(SearchCount);end;end; beginResult := nil;if BufLen <= 0 then Exit;if frDown in Options thenbeginDirection := 1;Inc(SelStart, SelLength); { start search past end ofselection }SearchCount := BufLen - SelStart - Length(SearchString);if SearchCount < 0 then Exit;if Longint(SelStart) + SearchCount > BufLen thenExit;endelsebeginDirection := -1;Dec(SelStart, Length(SearchString));SearchCount := SelStart;end;if (SelStart < 0) or (SelStart > BufLen) then Exit;Result := @Buf[SelStart]; { Using a Char map array is faster than callingAnsiUpper on every character }for C := Low(CharMap) to High(CharMap) doCharMap[C] := C; if not (frMatchCase in Options) thenbeginAnsiUpperBuff(PChar(@CharMap), sizeof(CharMap));AnsiUpperBuff(@SearchString[1],Length(SearchString));end; while SearchCount > 0 dobeginif frWholeWord in Options thenif not FindNextWordStart(Result) then Break;I := 0;while (CharMap[Result[I]] = SearchString[I+1]) dobeginInc(I);if I >= Length(SearchString) thenbeginif (not (frWholeWord in Options)) or(SearchCount = 0) or(Result[I] in WordDelimiters) thenExit;Break;end;end;Inc(Result, Direction);Dec(SearchCount);end;Result := nil;end; end. 4.4.3 替换对话框部件 替换对话框部件为应用程序提供替换对话框。如图4.9。它包括查找对话框的所有功能,此外还允许使用者更换被选中的字符串。FindText 属性是应用程序需查找的字符串。ReplaceText属性是被选中字符的替换字符串。Options 属性决定对话框的显示方式。其值如表4.3所示。与查找对话框一样,替换对话框亦有OnFind 事件。用户输入查找字符串并按FindNext按钮时,发生OnFind 事件。用户选择Replace 或ReplacAll 时, 对话框发生OnRelpace事件,要替换的字符串存入ReplaceText属性中,要编写相应的代码以支持替换功能。 表4.3 替换对话框的Options属性的取值及含义━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 取值 含义────────────────────────────────────────frRelpace 如果是真值, 应用程序将ReplaceText 属性中的字符串替换 FindText属性中的字符串。frReplacAll 如果是真值,应用程序将ReplaceText属性中的字符串替换, 查找到的所有FindText属性中的字符串。━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━