簡體   English   中英

如何滾動到用戶在finddialog框中輸入的“找到”單詞

[英]How to scroll to the 'found' word that the user has entered in the finddialog box

我在delphi表單上有一個豐富的編輯,其中包含一個finddialog框。 用戶輸入要找到的單詞,程序正確地突出顯示“找到的”單詞 - 這很好用。 然而,我想要的是程序滾動,以便找到的單詞的第一個出現在richedit框的第一行。 如果用戶然后單擊“下一步”(查找對話框),則滾動繼續,以便下一次出現的單詞出現在第一行,依此類推。 有人可以幫我從這里出去嗎?

procedure tform3.FindDialog1Find(Sender: TObject);
var
  sText: string;
  StartFrom, FoundPos: integer;
begin
  { If saved position is 0, this must be a "Find First" operation. }
  if PreviousFoundPos = 0 then
    { Reset the frFindNext flag of the FindDialog }
    findDialog1.Options := findDialog1.Options - [frFindNext];

  if not (frFindNext in FindDialog1.Options) then begin // is it a Find "First" ?
    sText := Richedit1.Text;
    StartFrom := 1;
  end
  else begin // it's a Find "Next"
    { Calculate from where to start searching:
      start AFTER the end of the last found position. }
    StartFrom := PreviousFoundPos + Length(FindDialog1.Findtext);
    { Get text from the RichEdit, starting from StartFrom }
    sText := Copy(Richedit1.Text, StartFrom, Length(Richedit1.Text) - StartFrom + 1);
  end;

  if frMatchCase in FindDialog1.Options then   // case-sensitive search?
    { Search position of FindText in sText.
      Note that function Pos() is case-sensitive. }
    FoundPos := Pos(FindDialog1.FindText, sText)
  else
    { Search position of FindText, converted to uppercase,
      in sText, also converted to uppercase        }
    FoundPos := Pos(UpperCase(FindDialog1.FindText), UpperCase(sText));

  if FoundPos > 0 then begin
    { If found, calculate position of FindText in the RichEdit }
    PreviousFoundPos := FoundPos + StartFrom - 1;
    { Highlight the text that was found }
    RichEdit1.SelStart := PreviousFoundPos - 1;
    RichEdit1.SelLength := Length(FindDialog1.FindText);
    RichEdit1.SetFocus;
  end
  else
    ShowMessage('Could not find "' + FindDialog1.FindText + '"');
end;

您可以使用以下代碼滾動到TRichEdit控件中的光標位置:

RichEdit1.SelStart := PreviousFoundPos - 1;
RichEdit1.SelLength := Length(FindDialog1.FindText);
RichEdit1.SetFocus;   

// Now scroll to the current cursor position 
RichEdit1.Perform(EM_SCROLLCARET, 0, 0);

這將Windows消息EM_SCROLLCARET發送到控件,該控件將使控件滾動到當前光標位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM