How to display both Latin and Greek characters in a TRichEdit (Views: 46)


I am trying to display some results in a TRichEdit control. The text is a mixture of Latin letters and Greek (science) letters. Obviously, I have to change the font to 'Symbol' when I want to display the greek letter, otherwise it will be the default font. So, how do I do that? For example how do I display in RichEdit box the following:
RichEdit1.Lines.Add('Standard deviation /* here I want to insert the Greek letter 'sigma' */ is '+FloatToStr(sigma));


The key is to not use Lines.Add to add the text, the Lines property is not "formatting-aware". You do it this way:

{ ... }
  norm_font = 'Times New Roman';
  norm_charset = DEFAULT_CHARSET;
  symb_font = 'Symbol';
  symb_charset = SYMBOL_CHARSET;
  { ... }

with richedit1 do
  selstart := gettextlen; {set caret to end}
  selattributes.Name := norm_font;
  selattributes.charset := norm_charset;
  seltext := 'Standard deviation ';
  selattributes.Name := symb_font;
  selattributes.charset := symb_charset;
  seltext := 'S';
  selattributes.Name := norm_font;
  selattributes.charset := norm_charset;
  seltext := ' is ' + FloatToStr(sigma);
  { etc. }

As you see this is quite cumbersome but the alternative is even more so: constructing a *complete* rich text file (with font table!) for the text to insert and use EM_STREAMIN to get it into the text.

<< Back to main page