function GetTextBound(UseText: WideString; UseFont: WideString; UseSize: single;
var bW: integer;
var bH: integer; FontCollection: Pointer; UseStrFormat: integer): GPSTATUS;
stdcall;
var
Graphics: LONG_PTR;
Fam: GpFontFamily;
TempFont: GpFont;
DC: HDC;
strFormat: GPSTRINGFORMAT;
boundingBox, layoutRect: TGPRectF;
begin
Result := GenericError;
strFormat :=
nil;
Fam :=
nil;
TempFont :=
nil;
Graphics := 0;
// Create matching font
try
GdipCheck(GdipCreateFontFamilyFromName(PWideChar(UseFont), FontCollection, Fam));
if Assigned(Fam)
then
begin
GdipCheck(GdipCreateFont(Fam, UseSize, 0, 2, TempFont));
if Assigned(TempFont)
then
begin
DC := GetDC(GetDesktopWindow);
GdipCheck(GdipCreateStringFormat(0, 0, strFormat));
GdipCheck(GdipCreateFromHDC(
DC, Graphics));
FillChar(boundingBox, SizeOf(boundingBox), 0);
FillChar(layoutRect, SizeOf(layoutRect), 0);
GdipCheck(GdipMeasureString(Graphics, PWideChar(UseText), Length(UseText), TempFont,
@layoutRect, strFormat, @boundingBox,
nil,
nil));
if Assigned(strFormat)
then
GdipCheck(GdipDeleteStringFormat(strFormat));
bW := round(boundingBox.Width + 0.5);
bH := round(boundingBox.Height + 0.5);
if UseStrFormat <> 0
then
Swap(bW, bH);
if (bW <> 0)
or (bH <> 0)
then
Result := OK;
ReleaseDc(GetDesktopWindow,
DC);
end;
end;
finally
if Graphics <> 0
then
GdipCheck(GdipDeleteGraphics(Graphics));
if Assigned(TempFont)
then
GdipCheck(GdipDeleteFont(TempFont));
if Assigned(Fam)
then
GdipCheck(GdipDeleteFontFamily(Fam));
end;
end;