zoukankan      html  css  js  c++  java
  • Delphi TFindDialog TReplaceDialog对话框在Memo中的使用

    Delphi TFindDialog TReplaceDialog对话框的使用

     
    下面这段代码已经完全的解决Memo的查找对话框 和 替换对话框 功能 的所有功能
     

    查找对话框部件

      查找对话框部件为应用程序提供查找对话框,用户可使用查找对话框在文本文件中查找字符串。

      可用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事件。
    其中SearchMemo函数是Search单元中定义的,SearchMemo可在TEdit,TMemo,以及其它TCustomEdit派生类中查找指定的字符串。查找从控件的脱字号(^)开始,查找方式由Options决定。如果向后查找从控件的StlStart处开始,如果向前查找则从控件的SelEnd处查找。
     
      如果在控件中找到相匹配的字符串,则字符串被选中,函数返回真值。如无匹配的字符串,函数返回假值。
     
      特别注意的是TEdit,TMemo中有一个HideSeletion属性,它决定当焦点从该控制转移至其它控制时,被选中的字符是否保持被选中的状态。如果是真值,则只有获得焦点才能保持被选中状态。查找时,焦点在查找对话框上,因此要想了解查找情况,必须将HideSeletion设成假值。控制的缺省值为真值。

    替换对话框

    替换对话框部件为应用程序提供替换对话框。如图4.9。它包括查找对话框的所有功能,此外还允许使用者更换被选中的字符串。FindText 属性是应用程序需查找的字符串。ReplaceText属性是被选中字符的替换字符串。Options 属性决定对话框的显示方式。其值如表4.3所示。

      与查找对话框一样,替换对话框亦有OnFind 事件。用户输入查找字符串并按FindNext按钮时,发生OnFind 事件。用户选择Replace 或ReplacAll 时,对话框发生OnRelpace事件,要替换的字符串存入ReplaceText属性中,要编写相应的代码以支持替换功能。

     表4.3 替换对话框的Options属性的取值及含义

    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

    取值              含义

    ────────────────────────────────────────────────────

    frRelpace                 如果是真值,应用程序将ReplaceText 属性中的字符串替换

                FindText属性中的字符串。

    frReplacAll               如果是真值,应用程序将ReplaceText属性中的字符串替换,

                查找到的所有FindText属性中的字符串。

    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

     Search单元 SearchMemo

    ///////////////////////////////////////////////////////////////////////////////////////////
    //Search单元 SearchMemo
    ///////////////////////////////////////////////////////////////////////////////////////////
     
    unit Search;
     
    interface
     
    uses
      SysUtils, StdCtrls, Dialogs, StrUtils;
     
    function SearchMemo(Memo: TCustomEdit; const SearchString: string; Options: TFindOptions): Boolean; 
     
    implementation
     
    function SearchMemo(Memo: TCustomEdit; const SearchString: string; Options: TFindOptions): Boolean; 
    var
      Buffer, P: PChar;
      Size: Word;
    begin
      Result := False;
      if Length(SearchString) = 0 then
        Exit;
     
      Size := Memo.GetTextLen;
      if (Size = 0) then
        Exit;
     
      Buffer := SysUtils.StrAlloc(Size + 1);
      try
        Memo.GetTextBuf(Buffer, Size + 1);
     
        if frDown in Options then
          P := SearchBuf(Buffer, Size, Memo.SelStart, Memo.SelLength,SearchString, [soDown])
           
        else
          P := SearchBuf(Buffer, Size, Memo.SelStart, Memo.SelLength,SearchString, []);
            
        if (frMatchCase in Options) then
          P := SearchBuf(Buffer, Size, Memo.SelStart, Memo.SelLength, SearchString,[soMatchCase]);
           
        if (frWholeWord in Options) then
          P := SearchBuf(Buffer, Size, Memo.SelStart, Memo.SelLength, SearchString,[soWholeWord]);
     
        if P <> nil then
        begin
          Memo.SelStart := P - Buffer;
          Memo.SelLength := Length(SearchString);
          Result := True;
        end;
     
      finally
        SysUtils.StrDispose(Buffer);
      end;
    end;
     
    end.
    ///////////////////////////////////////////////////////////////////////////////////////////
    

     Unit1单元代码

    ///////////////////////////////////////////////////////////////////////////////////////////
    //Unit1单元
    ///////////////////////////////////////////////////////////////////////////////////////////
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Menus;
     
    type
      TForm1 = class(TForm)
        FindDialog1: TFindDialog;
        ReplaceDialog1: TReplaceDialog;
        MainMenu1: TMainMenu;
        Edit1: TMenuItem;
        Find1: TMenuItem;
        Replace1: TMenuItem;
        FindNext1: TMenuItem;
        N1: TMenuItem;
        Memo1: TMemo;
        procedure FindDialog1Find(Sender: TObject);
        procedure ReplaceDialog1Replace(Sender: TObject);
        procedure ReplaceDialog1Find(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FindNext1Click(Sender: TObject);
        procedure Find1Click(Sender: TObject);
        procedure Replace1Click(Sender: TObject);
        procedure Edit1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     
    var
      Form1: TForm1;
      FindStr: string;
     
    implementation
     
    uses Search;
     
    {$R *.dfm}
     
    { FindDialog Find }
    procedure TForm1.FindDialog1Find(Sender: TObject);
    begin
      with Sender as TFindDialog do
      begin
        FindStr := FindText;
        if not SearchMemo(Memo1, FindText, Options) then
          MessageBox(Handle, PWideChar(Concat('找不到"', FindText, '"')), '记事本',
            MB_ICONINFORMATION);
      end;
    end;
     
    { ReplaceDialog Replace }
    procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
    var
      Found: Boolean;
    begin
      with ReplaceDialog1 do
      begin
        { Replace }
        if (frReplace in Options) and (Memo1.SelText = FindText) then
          Memo1.SelText := ReplaceText;
        Found := SearchMemo(Memo1, FindText, Options);
     
        { Replace All }
        if (frReplaceAll in Options) then
        begin
          Memo1.SelStart := 0;
          while Found do
          begin
            if (Memo1.SelText = FindText) then
              Memo1.SelText := ReplaceText;
            Found := SearchMemo(Memo1, FindText, Options);
          end;
          if not found then
             SendMessage(form1.Memo1.Handle,WM_VSCROLL,SB_TOP,0);
        end;
     
        if (not Found) and (frReplace in Options) then
          MessageBox(Handle, PWideChar(Concat('找不到"', FindText, '"')), '记事本',
            MB_ICONINFORMATION);
      end;
     
    end;
     
    { ReplaceDialog Find }
    procedure TForm1.ReplaceDialog1Find(Sender: TObject);
    begin
      with Sender as TReplaceDialog do
        if not SearchMemo(Memo1, FindText, Options) then
          MessageBox(Handle, PWideChar(Concat('找不到"', FindText, '"')), '记事本',
            MB_ICONINFORMATION);
    end;
     
    { Find Next }
    procedure TForm1.FindNext1Click(Sender: TObject);
    begin
      if not SearchMemo(Memo1, FindStr, FindDialog1.Options) then
        MessageBox(Handle, PWideChar(Concat('找不到"', FindStr, '"')), '记事本',
          MB_ICONINFORMATION);
    end;
     
    { FindDialog1.Execute }
    procedure TForm1.Find1Click(Sender: TObject);
    begin
      with FindDialog1 do
      begin
        Left := Self.Left + 100;
        Top := Self.Top + 150;
        FindText := Memo1.SelText;
        Execute;
      end;
    end;
     
    { ReplaceDialog1.Execute }
    procedure TForm1.Replace1Click(Sender: TObject);
    begin
      with ReplaceDialog1 do
      begin
        Left := Self.Left + 100;
        Top := Self.Top + 150;
        FindText := Memo1.SelText;
        Execute;
      end;
    end;
     
    { MainMenu Enable }
    procedure TForm1.Edit1Click(Sender: TObject);
    begin
      Find1.Enabled := (Memo1.Text <> '');
      FindNext1.Enabled := (Memo1.Text <> '') or (FindStr <> '');
      Replace1.Enabled := (Memo1.Text <> '');
    end;
     
    { Initial }
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Position := poDesktopCenter;
    //  FindDialog1.Options := [frDown, frHideWholeWord];
    //  ReplaceDialog1.Options := [frDown, frHideWholeWord];
      with Memo1 do
      begin
        HideSelection := False;
        ScrollBars := ssVertical;
        Align := alClient;
      end;
    end;
     
    end.
    ///////////////////////////////////////////////////////////////////////////////////////////
    

     

    PS:
        SearchMemo这个函数在我看Delphi TipSender中有本书 《Delphi 2.0 高级程序设计指南》里面找到的,不过我对它进行删减,保留了最重要的部分,并增强了它。   
      用Delphi的ActionList可以完全的实现FindDialog和ReplaceDialog的全部功能并且不用一句代码
    但如果你要用它写一个记事本的话那个 F3查找下一个 并不能实现
     
  • 相关阅读:
    swift--使用URLSession异步加载图片
    swift--浮点数转换成整数(四舍五入/直接截断)
    swift--环形进度条(UIActivityIndicatorView)的用法
    swift--Timer实现定时器功能,每个一段时间执行具体函数,可以重复,也可以只执行一次
    HTML节点树
    网页的结构
    网页的组成
    HTTP 请求过程
    HTTP 基础术语
    《投资最重要的事》
  • 原文地址:https://www.cnblogs.com/xe2011/p/3370865.html
Copyright © 2011-2022 走看看