2004. október 31., vasárnap

How to display both Latin and Greek characters in a TRichEdit


Problem/Question/Abstract:

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));

Answer:

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

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

with richedit1 do
begin
  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. }
end;

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.

Nincsenek megjegyzések:

Megjegyzés küldése