Send the underlying rich edit window the EM_GETTEXTEX
message. You pass a GETTEXTEX
struct which specifies the code page.
So, something like this would pull the text out into a UTF-16 encoded 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;
You can then convert that UTF-16 string to whatever code page you like. If you'd rather pull it out in a specific code page directly, then do it like this:
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;