如何在TCustomControl上绘制Unicode文本?是否有其他选项可以在不使用Canvas的情况下实现?
是的,你说得对。不过,我建议你升级到 Delphi 2009 或更高版本,其中 VCL 具有全面的 Unicode 支持,一切都更加容易。
无论如何,你可以这样做
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;
在旧版本的 Delphi 中(我想是这样。代码可以编译在我的虚拟 Windows 95 机器上的 Delphi 7 中,但我看不到任何文本。这是因为 Windows 95 太老了,我想。)
更新
如果你想要支持像 Windows 95 和 Windows 98 这样的非常老的操作系统,你需要使用 TextOutW
而不是 DrawTextW
,因为后者没有实现 (来源)。 TextOut
比 DrawText
功能更弱,所以如果你想在一个矩形中居中文本,你需要手动计算位置。
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;
DT_SINGLELINE
、DT_CENTER
(水平居中)和DT_VCENTER
(垂直居中),并且r
矩形是整个客户端矩形(ClientRect
),那么DrawText
会为你完成。因此,我的第一个代码片段将在水平和垂直方向上居中绘制文本。然而,使用TextOut
,你需要手动计算文本的左上角。左坐标是(Width-TextWidth) div 2
,顶部坐标是(Height-TextHeight) div 2
。Width
和Height
是控件的宽度和高度。你需要想办法弄清楚... - Andreas RejbrandPaint
过程中时,画布应该被清除。但如果您想手动清除它,请执行Canvas.Brush.Color := clBtnFace; Canvas.Brush.Style := bsSolid; Canvas.FillRect(ClientRect);
。 - Andreas RejbrandTextOut
等时,在这种情况下,我指的是TextOutW
等。) - Andreas Rejbrand