TAdvSmoothButton width accoding to caption length

Good afternoon to all,
i have a TAdvSmoothButton like this

image

with width:=120

When i change the caption to "Automatico" i try to set the button width via bitmap canvas like (assuming bmp is bitmap and assigned, TWD is an integer and btn1 is TAdvSmoothButton )

    Bmp.Canvas.Font.Assign(Btn1.Appearance.Font);
    TWD:=Bmp.Canvas.TextWidth(Btn1.Caption);
    // try to set button length
    if Btn1.Width - Btn1.HorizontalSpacing<TWD  then  Btn1.Width:=TWD + 15;

But.... (1)
Bmp.Canvas.TextWidth(Btn1.Caption) returns 105 that is < 120 and the size is not changed .....
But (2) what i get is this
image
In order to get the exact button width i have to set it to 121 to obtain
image
So how i can do in order to calculate the exact text width according to used font to adjust the button width ? (due the fact that Appearance property is very complex one and the painted text, caption, is not "simple text").

Thank you for attention
Best regards
Daniele

TAdvSmoothButton text is drawn via GDI+ and you are here measuring the text size with GDI. This can result in subtle differences.
The TGPGraphics class in AdvGDIP.pas has the TextWidth() based on GDI+

Good morning Bruno,
thank you for your fast reply.
I tried to use TGPGraphics.TextWidth() without any result ...

I use this code... with btn1 is TAdvSmoothButton and TG is TGPGraphics

TG:=TGPGraphics.Create(Btn1.Handle);
TWD:=TG.TextWidth(Btn1.Caption,Btn1.Appearance.Font);
....
FreeandNil(TG);

Is This code correct?? (According to constructor TGPGraphics.Create(hdc: HDC);

Where i miss ??

Thank you
Good week end

Daniele

You need to create TGPGraphics on a Canvas handle.

Example:

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
  gp: TGPGraphics;
  w: integer;
begin
  s := 'pneumonoultramicroscopicsilicovolcanoconiosis';

  AdvSmoothButton1.Caption := s;

  gp := TGPGraphics.Create(Canvas.Handle);
  w := gp.TextWidth(s, AdvSmoothButton1.Appearance.Font);
  gp.Free;

  AdvSmoothButton1.Width := w + advsmoothbutton1.Appearance.Spacing * 2;
end;

smoothbutton

Good morning Bruno,
thank you for example.
I did not understand that handle is Canvas's handle .....

Thank you again

Regards
Daniele