Delphi richedit获取选中文字

时间:2021-11-27 19:22:54
 
function TForm1.GetSendText(RichEdit: TExRichEdit): string;
var
  MsgListInfo: TStrings;
  i, m, n: integer;
  x, y: LongInt;
  WideStr: WideString;
begin
  Result := '';
  MsgListInfo := RichEdit.Lines;
  Y := SendMessage(RichEdit.Handle, EM_LINEFROMCHAR, MsgList.SelStart, 0); //得到rich光标所在行与列
  X := RichEdit.SelStart - SendMessage(RichEdit.Handle, EM_LINEINDEX, Y, 0);
  if RichEdit.SelLength = 0 then
  begin
    for i := y downto 0 do
    begin
      m := Pos('[', MsgListInfo.Strings[i]);
      n := Pos(']', MsgListInfo.Strings[i]);
      if (m = 1) and (n = 21) then
      begin
        Result := Copy(MsgListInfo.Strings[i], m + 1, n - 2);
        Break;
      end;
    end;
  end else
  begin
    WideStr := MsgListInfo.Strings[y];
    Result :=Copy(WideStr, x + 1, RichEdit.SelLength);
  end;
end;