もう 1 つの DrawShadowText 関数で、今回は MS Win API に基づいています。Windows Vista 以降が必要です。
ソース: https://www.delphipraxis.net/66678-drawshadowtext-delphi-commctrl-h-comctl32-dll-3.html
英語版は次のとおりです。
{-------------------------------------------------------------------------------------------------------------
Unit ShadowText by Matthias G. aka turboPASCAL
Version 1.0 (VCL)
Draws text with shadow (This unit provides the function DrawShadowText from the ComCtl32.dll)
Minimum req:
Windows Vista
Comctl32.dll v6
If running under XP or without Windows XP Manifest, a substitute function is used to display the shadow.
From the user manual: To use DrawShadowText, specify Comctl32.dll version 6 in the manifest. For more information on manifests, see Enabling Visual Styles.
Src: https://www.delphipraxis.net/66678-drawshadowtext-delphi-commctrl-h-comctl32-dll-3.html
API docs: https://docs.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-drawshadowtext
Tester: c:\Myprojects\Project Testers\gr cGraphUtil.pas\
-------------------------------------------------------------------------------------------------------------}
UNIT ShadowText;
INTERFACE
USES
Windows, SysUtils, System.Classes, Graphics;
VAR
// determines whether the alternative shadow should be used if the function from ComCtl32.dll is not available
UseLQShadow : Boolean = True;
// Output of a text with shadow by specifying X and Y coordinates
function DrawShadowText(ACanvas: TCanvas; x, y: Integer; AText: string; TextColor, ShadowColor: TColor; ShadowSpaceX, ShadowSpaceY: Integer): Integer;
// Output of a text with shadow over a TRect structure with specification of the text formatting (text flags see Delphi help "DrawText")
function DrawShadowTextR(ACanvas: TCanvas; TextRect: TRect; x, y: Integer; AText: string; TextColor, ShadowColor: TColor; ShadowSpaceX, ShadowSpaceY: Integer; TextFlags: DWord): Integer;
IMPLEMENTATION
TYPE
TDrawShadowTextI = function(hdc: HDC;
pszText: LPCWSTR;
cch: UINT;
const pRect: PRect;
dwFlags: DWORD;
crText: COLORREF;
crShadow: COLORREF;
ixOffset: Integer;
iyOffset: Integer): Integer; stdcall;
VAR
hComCtl32DLL: THandle = 0;
DrawShadowTextI: TDrawShadowTextI;
OldCanvasColor: TColor;
OldBkModeColor: Integer;
// DrawShadowText as declared in ComCtl32.dll ( requires WindowsXP-Manifest! )
// function DrawShadowText_(hdc: HDC; pszText: LPCWSTR; cch: UINT; const pRect: PRect; dwFlags: DWORD; crText: COLORREF; crShadow: COLORREF; ixOffset: Integer; iyOffset: Integer): Integer; stdcall; external 'ComCtl32.dll' name 'DrawShadowText';
// Determines whether a Windows version from WinXP on is available.
function IsWindowsXPAndUp: Boolean;
begin
Result := ((Win32MajorVersion = 5) and (Win32MinorVersion >= 1)) or (Win32MajorVersion > 5);
end;
// DrawShadowTextL (L - Low Quality)
function DrawShadowTextL(ACanvas: TCanvas; TextRect: TRect; AText: string;
TextColor, ShadowColor: TColor; ShadowSpaceX,
ShadowSpaceY: Integer; TextFlags: DWORD): Integer;
begin
OldBkModeColor := SetBKMode(ACanvas.Handle, TRANSPARENT);
OldCanvasColor := ACanvas.Font.Color;
if UseLQShadow
then
begin
inc(TextRect.Left, ShadowSpaceX);
inc(TextRect.Top, ShadowSpaceY);
inc(TextRect.Right, ShadowSpaceX);
inc(TextRect.Bottom, ShadowSpaceY);
ACanvas.Font.Color := ShadowColor;
Result := DrawText(ACanvas.Handle, PChar(AText), length(AText), TextRect, TextFlags);
dec(TextRect.Left, ShadowSpaceX);
dec(TextRect.Top, ShadowSpaceY);
dec(TextRect.Right, ShadowSpaceX);
dec(TextRect.Bottom, ShadowSpaceY);
end
else
Result := 1;
ACanvas.Font.Color := TextColor;
Result := Result AND DrawText(ACanvas.Handle, PChar(AText), length(AText), TextRect, TextFlags);
ACanvas.Font.Color := OldCanvasColor;
SetBKMode(ACanvas.Handle, OldBkModeColor);
end;
// DrawShadowText: für einfache Ausgabe
function DrawShadowText(ACanvas: TCanvas; x, y: Integer; AText: string; TextColor, ShadowColor: TColor; ShadowSpaceX, ShadowSpaceY: Integer): Integer;
VAR TextRect: TRect;
begin
TextRect := RECT(x, y, x + ACanvas.TextWidth(AText), y + ACanvas.TextHeight(AText));
if @DrawShadowTextI <> nil
then Result := DrawShadowTextI(ACanvas.Handle, PWideChar(WideString(AText)), length(AText), @TextRect, 0, COLORREF(TextColor), COLORREF(ShadowColor), ShadowSpaceX, ShadowSpaceY)
else Result := DrawShadowTextL(ACanvas, TextRect, AText, TextColor, ShadowColor, ShadowSpaceX, ShadowSpaceY, 0);
end;
// DrawShadowTextR: for formatted output (R - Text[R]ect)
function DrawShadowTextR(ACanvas: TCanvas; TextRect: TRect; x, y: Integer; AText: string; TextColor, ShadowColor: TColor; ShadowSpaceX, ShadowSpaceY: Integer; TextFlags: DWord): Integer;
begin
if @DrawShadowTextI <> NIL
then Result := DrawShadowTextI(ACanvas.Handle, PWideChar(WideString(AText)), length(AText), @TextRect, TextFlags, COLORREF(TextColor), COLORREF(ShadowColor), ShadowSpaceX, ShadowSpaceY)
else Result := DrawShadowTextL(ACanvas, TextRect, AText, TextColor, ShadowColor, ShadowSpaceX, ShadowSpaceY, TextFlags);
end;
initialization
if IsWindowsXPAndUp then
begin
hComCtl32DLL := LoadLibrary('ComCtl32.dll');
@DrawShadowTextI := GetProcAddress(hComCtl32DLL, 'DrawShadowText');
end;
finalization
if hComCtl32DLL <> 0
then FreeLibrary(hComCtl32DLL);
end.