Send the base edit window EM_GETTEXTEX . You pass a GETTEXTEX struct that indicates the code page.
So, something like this will pull the text into a UTF-16 WideString :
function GetRichEditText(RichEdit: TRichEdit): WideString; var GetTextLengthEx: TGetTextLengthEx; GetTextEx: TGetTextEx; Len: Integer; begin GetTextLengthEx.flags := GTL_DEFAULT; GetTextLengthEx.codepage := 1200; Len := SendMessage(RichEdit.Handle, EM_GETTEXTLENGTHEX, WPARAM(@GetTextLengthEx), 0); if Len=E_INVALIDARG then raise Exception.Create('EM_GETTEXTLENGTHEX failed'); SetLength(Result, Len); if Len=0 then exit; GetTextEx.cb := (Length(Result)+1)*SizeOf(WideChar); GetTextEx.flags := GTL_DEFAULT; GetTextEx.codepage := 1200; GetTextEx.lpDefaultChar := nil; GetTextEx.lpUsedDefChar := nil; SendMessage(RichEdit.Handle, EM_GETTEXTEX, WPARAM(@GetTextEx), LPARAM(PWideChar(Result))); end;
Then you can convert this UTF-16 string to any code page that you like. If you prefer to pull it out on a specific code page directly, do the following:
function GetRichEditText(RichEdit: TRichEdit; AnsiCodePage: UINT): AnsiString; var GetTextLengthEx: TGetTextLengthEx; GetTextEx: TGetTextEx; Len: Integer; begin GetTextLengthEx.flags := GTL_DEFAULT; GetTextLengthEx.codepage := AnsiCodePage; Len := SendMessage(RichEdit.Handle, EM_GETTEXTLENGTHEX, WPARAM(@GetTextLengthEx), 0); if Len=E_INVALIDARG then raise Exception.Create('EM_GETTEXTLENGTHEX failed'); SetLength(Result, Len); if Len=0 then exit; GetTextEx.cb := (Length(Result)+1)*SizeOf(AnsiChar); GetTextEx.flags := GTL_DEFAULT; GetTextEx.codepage := AnsiCodePage; GetTextEx.lpDefaultChar := nil; GetTextEx.lpUsedDefChar := nil; SendMessage(RichEdit.Handle, EM_GETTEXTEX, WPARAM(@GetTextEx), LPARAM(PWideChar(Result))); end;
source share