如果是全局位置:SelStart属性就可以了。
如果要返回哪行哪列:可以通过CaretPos属性。

解决方案 »

  1.   

    Delphi提供现成的查找函数,Options是个集合在,其中包含frDown则向下查找,包含frUP则向上查找 
    procedure TEditform.Find(Sender: TObject);
    begin
    with Sender as TFindDialog do
    if not SearchMemo(Memo1, FindText, Options) then
    ShowMessage('Cannot find "' + FindText + '".');
    end;
     SearchMemo函数是Search单元中定义的,SearchMemo可在TEdit,TMemo,以及其它TCustomEdit派生类中查找指定的字符串。查找从控件的脱字号(^)开始, 查找方式由Options决定。如果向后查找从控件的SelStart处开始,如果向前查找则从控件的SelEnd处查找。  
      

  2.   


    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.