Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw Unicode text?

How to draw Unicode text on TCustomControl? Are there other options to make it without the Canvas?

like image 499
Little Helper Avatar asked Apr 30 '11 09:04

Little Helper


People also ask

How do I type Unicode text?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

How do I get Unicode characters?

Inserting Unicode CharactersType the character code where you want to insert the Unicode symbol. Press ALT+X to convert the code to the symbol. If you're placing your Unicode character immediately after another character, select just the code before pressing ALT+X.

What is a Unicode example?

Unicode supports more than a million code points, which are written with a "U" followed by a plus sign and the number in hex; for example, the word "Hello" is written U+0048 U+0065 U+006C U+006C U+006F (see hex chart).


1 Answers

Yes, you are right on spot. Still, I would recommend you to upgrade to Delphi 2009 or later in which the VCL has full Unicode support and everything is much easier.

Anyhow, you can do

procedure TMyControl.Paint;
var
  S: WideString;
  r: TRect;
begin
  inherited;
  r := ClientRect;
  S := 'This is the integral sign: '#$222b;
  DrawTextW(Canvas.Handle, PWideChar(S), length(S), r, DT_SINGLELINE or
    DT_CENTER or DT_VCENTER or DT_END_ELLIPSIS);
end;

in old versions of Delphi (I think. The code compiles in Delphi 7 in my virtual Windows 95 machine, but I see no text. That is because Windows 95 is too old, I think.)

Update

If you want to support very old operating systems, like Windows 95 and Windows 98, you need to use TextOutW instead of DrawTextW, since the latter isn't implemented (source). TextOut is less powerful then DrawText, so you need to compute the position manually if you want to center the text inside a rectangle, for instance.

procedure TMyControl.Paint;
var
  S: WideString;
begin
  inherited;
  S := 'This is the integral sign: '#$222b;
  TextOutW(Canvas.Handle, 0, 0, PWideChar(S), length(S));
end;
like image 69
Andreas Rejbrand Avatar answered Oct 01 '22 12:10

Andreas Rejbrand