How to get pixel width and height of String?
How do you to use GetTextExtentPoint32W in 开发者_如何学JAVADelphi 7 to get the pixel width and height of a wide string before it is output?
You can do
procedure TForm1.FormPaint(Sender: TObject);
var
extent: TSize;
S: WideString;
begin
S := 'This is the integral sign: '#$222b;
if not GetTextExtentPoint32W(Canvas.Handle, PWideChar(S), length(S), extent) then
RaiseLastOSError;
TextOutW(Canvas.Handle, (Width - extent.cx) div 2, (Height - extent.cy) div 2,
PWideChar(S), length(S));
end;
The GetTextExtentPoint32W
will place the width and height in extent.cx
and extent.cy
, respectively.
The last line will then use TextOutW
to draw the string centered both horizontally and vertically on the client area.
精彩评论