Little Helper
Little Helper

Reputation: 2456

How do I draw Unicode text?

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

Upvotes: 7

Views: 3685

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109168

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;

Upvotes: 11

Related Questions