Delphi: TRichEdit Text in non-default non-Unicode system language as String (ANSI) -
so here's setup:
- make new delphi 7 application trichedit control on it. talking non-unicode applications here.
- install new input language in windows' regional , language options, that has different encoding encoding of default language non-unicode programs - example greek.
- add button in application, , in
onclick
handler addbutton1.caption := richedit1.text;
, , setfont.charset
charset of input language installed (greek_charset
if stick example). run application, switch new (greek) input language, type few letters in richedit , press button - button's caption has
????
symbols instead of greek characters.now, if set default language non-unicode programs greek (windows restart required), problem disappear - greek characters appear properly. set default language non-unicode programs before , problem there again.
so guess trichedit works unicode internally, changing font.charset value never changes - richedit accepts installed input language properly, , if have installed 2 different non-latin languages use different character sets (greek /greek_charset
/ , russian /russian_charset
/ example) accept them both without changing font.charset.
i guess when .text
(or .lines[i]
) value of trichedit, converts internal unicode text ansi, based on windws' default language non-unicode programs.
further more, assigning .text
value widestring or unicodestring doesn't work (the text once again in ????
instead of proper characters), it's not when assign string (ansistring).
so here's question:
i want able convert text of richedit string (ansi) properly, based on character set of choosing instead of system's default language non-unicode programs. how can that? prefer solution doesn't involve third party components, but, of course, if not possible - do.
thanks!
p.s.: switching delphi 2009 or later not acceptable solution.
send underlying rich edit window em_gettextex
message. pass gettextex
struct specifies code page.
so, pull text out 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 raise exception.create('em_gettextlengthex failed'); setlength(result, len); if len=0 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 convert utf-16 string whatever code page like. if you'd rather pull out in specific code page directly, 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 raise exception.create('em_gettextlengthex failed'); setlength(result, len); if len=0 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;
Comments
Post a Comment