Little Helper
Little Helper

Reputation: 2456

How to get pixel width and height of String?

How do you to use GetTextExtentPoint32W in Delphi 7 to get the pixel width and height of a wide string before it is output?

Upvotes: 1

Views: 3322

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

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.

Upvotes: 9

Related Questions