function DrawEllipsisText(WinHandle: HWND;
DC: Hdc; Text: WideString; TextRect: TRect;
ColrARGB: COLORREF; UseFont: WideString; UseSize: Single; FontStyle: TFontStyle;
ShadowOffset: Single; UseStrFormat: integer; WordWrap: BOOL): GPSTATUS;
var
Width: integer;
Fam: GpFontFamily;
TempFont: GpFont;
Graphics: LONG_PTR;
rectF: TGPRectF;
rc: TRect;
strFormat: Pointer;
boundingBox, layoutRect: TGPRectF;
begin
Result := GenericError;
Graphics := 0;
strFormat :=
nil;
TempFont :=
nil;
Fam :=
nil;
try
GdipCheck(GdipCreateFromHDC(
DC, Graphics));
GdipCheck(GdipCreateFontFamilyFromName(PWideChar(UseFont),
nil, Fam));
if assigned(Fam)
then
begin
GdipCheck(GdipCreateFont(Fam, UseSize, FontStyle, 2, TempFont));
if assigned(TempFont)
then
begin
GdipCheck(GdipCreateStringFormat(0, 0, strFormat));
FillChar(boundingBox, SizeOf(boundingBox), 0);
FillChar(layoutRect, SizeOf(layoutRect), 0);
GdipCheck(GdipMeasureString(Graphics, PWideChar(Text), length(Text), TempFont, @layoutRect,
strFormat, @boundingBox,
nil,
nil));
Width := (TextRect.Right - TextRect.Left);
if boundingBox.Width > Width
then
begin
rectF := MakeRect(TextRect.Left, TextRect.Top, Width, TextRect.Bottom);
rc.Left := round(rectF.x);
rc.Top := round(rectF.y);
rc.Bottom := round(rectF.Height);
rc.Right := round(rectF.Width);
UseStrFormat := GD_Ellipsis;
Result := GdipCheck(DrawTextToDC(
DC, Text, rc, ColrARGB, UseFont, UseSize, FontStyle,
ShadowOffset, UseStrFormat,
nil, False, 0, True));
end
else
Result := GdipCheck(DrawTextToDC(
DC, Text, TextRect, ColrARGB, UseFont, UseSize, FontStyle,
ShadowOffset, UseStrFormat,
nil, False, 0, WordWrap));
end;
end;
finally
if Graphics <> 0
then
GdipCheck(GdipDeleteGraphics(Graphics));
if assigned(TempFont)
then
GdipCheck(GdipDeleteFont(TempFont));
if assigned(Fam)
then
GdipCheck(GdipDeleteFontFamily(Fam));
if assigned(strFormat)
then
GdipCheck(GdipDeleteStringFormat(strFormat));
end;
end;