📄 dxgdiplusapi.pas
字号:
TGdiplusStartupInput = GdiplusStartupInput;
PGdiplusStartupInput = ^TGdiplusStartupInput;
// Output structure for GdiplusStartup()
GdiplusStartupOutput = packed record
NotificationHook : NotificationHookProc;
NotificationUnhook: NotificationUnhookProc;
end;
TGdiplusStartupOutput = GdiplusStartupOutput;
PGdiplusStartupOutput = ^TGdiplusStartupOutput;
// Private GDI+ classes for internal type checking
GpGraphics = Pointer;
ARGB = DWORD;
PARGB = ^ARGB;
GpBrush = Pointer;
GpTexture = Pointer;
GpSolidFill = Pointer;
GpLineGradient = Pointer;
GpPathGradient = Pointer;
GpHatch = Pointer;
GpPen = Pointer;
GpImage = Pointer;
GpBitmap = Pointer;
GpCachedBitmap = Pointer;
GPStatus = TdxGPStatus;
GpFillMode = TdxGPFillMode;
GpWrapMode = TdxGPWrapMode;
GpUnit = TdxGPUnit;
GpPointF = PdxGPPointF;
GpPoint = PdxGPPoint;
GpRectF = PdxGPRectF;
GpRect = PdxGPRect;
GpSizeF = PdxGPSizeF;
GpHatchStyle = TdxGPHatchStyle;
GpDashStyle = TdxGPDashStyle;
GpPenAlignment = TdxGPPenAlignment;
GpPenType = TdxGPPenType;
GpBrushType = TdxGPBrushType;
BitmapData = packed record
Width : UINT;
Height : UINT;
Stride : Integer;
PixelFormat : Integer;
Scan0 : Pointer;
Reserved : UINT;
end;
TBitmapData = BitmapData;
PBitmapData = ^TBitmapData;
type
TdxProc = procedure;
TdxUnitsLoader = class
protected
FinalizeList: TList;
InitializeList: TList;
public
constructor Create();
destructor Destroy; override;
procedure AddUnit(const AInitializeProc, AFinalizeProc: Pointer);
procedure RemoveUnit(const AFinalizeProc: Pointer);
procedure Finalize;
procedure Initialize;
end;
var
// codecs
PNGCodec: TGUID;
// GDI+ Memory managment methods
GdipAlloc: function(size: ULONG): pointer; stdcall;
GdipFree: procedure(ptr: pointer); stdcall;
// GDI+ initialization/finalization methods
GdiplusStartup: function(out token: DWORD; const input: GdiplusStartupInput; output: PGdiplusStartupOutput): Status; stdcall;
GdiplusShutdown: procedure(token: DWORD); stdcall;
// GDI+ Brush methods
GdipCloneBrush: function(brush :GpBrush; var clonebrush :GpBrush) :GPStatus; stdcall;
GdipDeleteBrush: function(brush :GpBrush) :GPStatus; stdcall;
GdipGetBrushType: function(brush :GpBrush; var bt :GpBrushType) :GPStatus; stdcall;
// GDI+ Solid Brush methods
GdipCreateSolidFill: function(color :ARGB; var brush :GpSolidFill) :GPStatus; stdcall;
GdipSetSolidFillColor: function(brush :GpSolidFill; color :ARGB) :GPStatus; stdcall;
GdipGetSolidFillColor: function(brush :GpSolidFill; var color :ARGB) :GPStatus; stdcall;
// GDI+ Gradient Brush methods
GdipCreateLineBrushFromRectI: function(const rect :GpRect; color1,color2 :ARGB; mode :LinearGradientMode;
wrapmode :GpWrapMode; var lineGradient :GpLineGradient) :GPStatus; stdcall;
GdipGetLineRectI: function(brush :GpLineGradient; var rect :GpRect) :GPStatus; stdcall;
GdipSetLineColors: function(brush :GpLineGradient; color1,color2 :ARGB) :GPStatus; stdcall;
GdipGetLineColors: function(brush :GpLineGradient; colors :PARGB) :GPStatus; stdcall;
GdipSetLineWrapMode: function(brush :GpLineGradient; wrapmode :GpWrapMode) :GPStatus; stdcall;
GdipGetLineWrapMode: function(brush :GpLineGradient; wrapmode :GpWrapMode) :GPStatus; stdcall;
// GDI+ Hatch Brush methods
GdipCreateHatchBrush: function(hatchstyle :GpHatchStyle; forecol,backcol :ARGB; var brush :GpHatch) :GPStatus; stdcall;
GdipGetHatchStyle: function(brush :GpHatch; var hatchstyle :GpHatchStyle) :GPStatus; stdcall;
GdipGetHatchForegroundColor: function(brush :GpHatch; var forecol :ARGB) :GPStatus; stdcall;
GdipGetHatchBackgroundColor: function(brush :GpHatch; var backcol :ARGB) :GPStatus; stdcall;
// GDI+ Pen methods
GdipCreatePen1: function(color :ARGB; width :single; u :GpUnit; var pen :GpPen) :GPStatus; stdcall;
GdipCreatePen2: function(brush :GpBrush; width :single; u :GpUnit; var pen :GpPen) :GPStatus; stdcall;
GdipClonePen: function(pen :GpPen; var clonepen :GpPen) :GPStatus; stdcall;
GdipDeletePen: function(pen :GpPen) :GPStatus; stdcall;
GdipGetPenFillType: function(pen :GpPen; var penType :GpPenType) :GPStatus; stdcall;
GdipSetPenBrushFill: function(pen :GpPen; brush :GpBrush) :GPStatus; stdcall;
GdipGetPenBrushFill: function(pen :GpPen; var brush :GpBrush) :GPStatus; stdcall;
GdipSetPenColor: function(pen :GpPen; color :ARGB) :GPStatus; stdcall;
GdipGetPenColor: function(pen :GpPen; var color :ARGB) :GPStatus; stdcall;
GdipSetPenMode: function(pen :GpPen; penMode :GpPenAlignment) :GPStatus; stdcall;
GdipGetPenMode: function(pen :GpPen; var penMode :GpPenAlignment) :GPStatus; stdcall;
GdipSetPenWidth: function(pen :GpPen; width :single) :GPStatus; stdcall;
GdipGetPenWidth: function(pen :GpPen; var width :single) :GPStatus; stdcall;
// GDI+ Graphis methods
GdipCreateFromHDC: function(hdc :HDC; var graphics :GpGraphics) :GPStatus; stdcall;
GdipDeleteGraphics: function(graphics :GpGraphics) :GPStatus; stdcall;
GdipGetDC: function(graphics :GpGraphics; var hdc :HDC) :GPStatus; stdcall;
GdipReleaseDC: function(graphics :GpGraphics; hdc :HDC) :GPStatus; stdcall;
GdipGraphicsClear: function(graphics :GpGraphics; color :ARGB) :GPStatus; stdcall;
GdipDrawLineI: function(graphics :GpGraphics; pen :GpPen; x1,y1,x2,y2 :integer) :GPStatus; stdcall;
GdipFillRectangleI: function(graphics :GpGraphics; brush :GpBrush; x,y,width,height :integer) :GPStatus; stdcall;
GdipDrawArcI: function(graphics :GpGraphics; pen :GpPen; x,y,width,height :integer;
startAngle,sweepAngle :single) :GPStatus; stdcall;
GdipDrawBezierI: function(graphics :GpGraphics; pen :GpPen; x1,y1,x2,y2,x3,y3,x4,y4 :integer) :GPStatus; stdcall;
GdipDrawRectangleI: function(graphics :GpGraphics; pen :GpPen; x,y,width,height :integer) :GPStatus; stdcall;
GdipDrawEllipseI: function(graphics :GpGraphics; pen :GpPen; x,y,width,height :integer) :GPStatus; stdcall;
GdipDrawPieI: function(graphics :GpGraphics; pen :GpPen; x,y,width,height :integer;
startAngle,sweepAngle :single) :GPStatus; stdcall;
GdipDrawPolygonI: function(graphics :GpGraphics; pen :GpPen; const points :GpPoint;
count :integer) :GPStatus; stdcall;
GdipDrawCurve2I: function(graphics :GpGraphics; pen :GpPen; const points :GpPoint;
count :integer; tension :single) :GPStatus; stdcall;
GdipDrawClosedCurve2I: function(graphics :GpGraphics; pen :GpPen; const points :GpPoint;
count :integer; tension :single) :GPStatus; stdcall;
GdipFillPolygonI: function(graphics :GpGraphics; brush :GpBrush; const points :GpPoint;
count :integer; fillmode :GpFillMode) :GPStatus; stdcall;
GdipFillEllipseI: function(graphics :GpGraphics; brush :GpBrush; x,y,width,height :integer) :GPStatus; stdcall;
GdipFillPieI: function(graphics :GpGraphics; brush :GpBrush; x,y,width,height :integer;
startAngle,sweepAngle :single) :GPStatus; stdcall;
GdipFillClosedCurveI: function(graphics :GpGraphics; brush :GpBrush; const points :GpPoint;
count :integer) :GPStatus; stdcall;
// added from MSN
GdipLoadImageFromStream: function(stream: IStream; var image: GpImage): GPStatus; stdcall;
GdipCreateHBITMAPFromBitmap: function(image: GpBitmap; var bitmap: HBitmap; color :ARGB): GPStatus; stdcall;
GdipCreateBitmapFromFile: function(filename: PWideChar; var bitmap: GpBitmap): GPStatus; stdcall;
GdipCreateBitmapFromStream: function(stream: IStream; var bitmap: GpBitmap): GPStatus; stdcall;
GdipCreateBitmapFromStreamICM: function (stream: ISTREAM; out bitmap: GpBitmap): GPStatus; stdcall;
GdipLoadImageFromFile: function(filename: PWideChar; var image: GpImage): GPStatus; stdcall;
GdipGetImageDimension: function(image: GpImage; var weidth, height: Single): GPStatus; stdcall;
GdipDrawImageRectI: function(graphics: GpGraphics; image: GpImage;
x, y: Integer; width, height: Integer): GPStatus; stdcall;
GdipDisposeImage: function(image: GpImage): GPStatus; stdcall;
GdipGetImageEncodersSize: function(out numEncoders: Integer; out size: Integer): GPStatus; stdcall;
GdipGetImageEncoders: function(numEncoders: Integer; size: Integer; encoders: PdxGPImageCodecInfo): GPStatus; stdcall;
GdipGetEncoderParameterListSize: function(image: GpImage; clsidEncoder: PGUID; out size: UINT): GPStatus; stdcall;
GdipGetEncoderParameterList: function(image: GpImage; clsidEncoder: PGUID;
size: UINT; buffer: PENCODERPARAMETERS): GPStatus; stdcall;
GdipCreateBitmapFromGdiDib: function (gdiBitmapInfo: PBitmapInfo;
gdiBitmapData: Pointer; out bitmap: GpBitmap): GPStatus; stdcall;
GdipCreateBitmapFromScan0: function (width: Integer; height: Integer;
stride: Integer; format: integer; scan0: PBYTE;
out bitmap: GpImage): GPStatus; stdcall;
GdipBitmapLockBits: function (bitmap: GpBitmap; rect: GPRECT; flags: UINT;
format: Integer; lockedBitmapData: PBITMAPDATA): GPStatus; stdcall;
GdipBitmapUnlockBits: function(bitmap: GpBitmap;
lockedBitmapData: PBITMAPDATA): GPStatus; stdcall;
GdipDrawImageRectRectI: function(graphics: GpGraphics; image: GpImage;
dstx, dsty, dstwidth, dstheight: Integer; srcx, srcy, srcwidth, srcheight: Integer;
srcUnit: GPUNIT; imageAttributes: Pointer; callback: TgpImageAbort; callbackData: Pointer): GPStatus; stdcall;
GdipDrawImageRectRect: function(graphics: GPGRAPHICS; image: GPIMAGE;
dstx: Single; dsty: Single; dstwidth: Single; dstheight: Single;
srcx: Single; srcy: Single; srcwidth: Single; srcheight: Single;
srcUnit: GPUNIT; imageAttributes: Pointer; callback: TgpImageAbort; callbackData: Pointer): GPSTATUS; stdcall;
GdipDrawImagePointRect: function(graphics: GPGRAPHICS; image: GPIMAGE;
x: Single; y: Single; srcx: Single; srcy: Single; srcwidth: Single;
srcheight: Single; srcUnit: GPUNIT): GPSTATUS; stdcall;
GdipCloneImage: function(image: GPIMAGE; out cloneImage: GPIMAGE): GPSTATUS; stdcall;
//lcm
GdipSetInterpolationMode: function(graphics: GpGraphics; imode: Integer): GpStatus; stdcall;
GdipGetInterpolationMode: function(graphics: GpGraphics; out imode: Integer): GpStatus; stdcall;
GdipCreateCachedBitmap: function(bitmap: GpBitmap; graphics: GpGraphics; out cachedBitmap: GpCachedBitmap): GpStatus; stdcall;
GdipDeleteCachedBitmap: function(cachedBitmap: GpCachedBitmap): GpStatus; stdcall;
GdipDrawCachedBitmap: function(graphics: GpGraphics; cachedBitmap: GpCachedBitmap; x, y: Integer): GpStatus; stdcall;
GdipCreateBitmapFromGraphics: function(width, height: Integer; target: GpGraphics; var bitmap: GpBitmap): GpStatus; stdcall;
GdipGetImageGraphicsContext: function(image: GpImage; out graphics: GpGraphics): GpStatus; stdcall;
GdipGetImageWidth: function(image: GpImage; out width: Integer): GpStatus; stdcall;
GdipGetImageHeight: function(image: GpImage; out height: Integer): GpStatus; stdcall;
GdipSetCompositingMode: function(graphics: GpGraphics; cmode: Integer): GpStatus; stdcall;
GdipGetCompositingMode: function(graphics: GpGraphics; out cmode: Integer): GpStatus; stdcall;
GdipSetCompositingQuality: function(graphics: GpGraphics; cq: Integer): GpStatus; stdcall;
GdipGetCompositingQuality: function(graphics: GpGraphics; out cq: Integer): GpStatus; stdcall;
GdipSetSmoothingMode: function(graphics: GpGraphics; sm: Integer): GpStatus; stdcall;
GdipGetSmoothingMode: function(graphics: GpGraphics; out sm: Integer): GpStatus; stdcall;
GdipCloneBitmapAreaI: function(x: Integer; y: Integer; width: Integer;
height: Integer; format: Cardinal; srcBitmap: GPBITMAP; out dstBitmap: GPBITMAP): GPSTATUS; stdcall;
{$EXTERNALSYM GdipBitmapUnlockBits}
GdipSaveImageToStream: function(image: GpImage; stream: ISTREAM; clsidEncoder: PGUID; encoderParams: pointer): GPStatus; stdcall;
GdipCreateBitmapFromHBITMAP: function(hbm: HBITMAP; hpal: HPALETTE; out bitmap: GpImage): GPStatus; stdcall;
function MakePoint(X, Y: Integer): TdxGPPoint; overload;
function MakePoint(X, Y: Single): TdxGPPointF; overload;
function MakeRect(x, y, width, height: Integer): TdxGPRect; overload;
function MakeRect(location: TdxGPPoint; size: TdxGPSize): TdxGPRect; overload;
function MakeRect(Rect: TRect): TdxGPRect; overload;
function MakeRect(x, y, width, height: Single): TdxGPRectF; overload;
function MakeRect(location: TdxGPPointF; size: TdxGPSizeF): TdxGPRectF; overload;
function MakeSize(Width, Height: Single): TdxGPSizeF; overload;
function MakeSize(Width, Height: Integer): TdxGPSize; overload;
// image conversion
function DifferentImage2Bitmap(AStream: TStream): TBitmap; overload;
function DifferentImage2Bitmap(const AFileName: string): TBitmap; overload;
procedure Bitmap2PNG(ABitmap: TBitmap; AStream: TStream); overload;
procedure Bitmap2PNG(ABitmap: TBitmap; const AFileName: string); overload;
// codecs
function GetCodecID(const CodecName: string; out Clsid: TGUID): GPStatus;
procedure CheckPngCodec;
// check errors
procedure GdipCheck(AStatus: Boolean); overload;
procedure GdipCheck(AStatus: GPStatus); overload;
// CLR
procedure dxInitializeGDIPlus; stdcall;
procedure dxFinalizeGDIPlus; stdcall;
function dxUnitsLoader: TdxUnitsLoader;
function CheckGdiPlus: Boolean;
resourcestring
scxGdipInvalidOperation = 'Invalid operation in GDI+';
implementation
const
GDIPlusLibraryName = 'gdiplus.dll';
var
FGDIPlusLibrary: Integer;
FGDIPlusToken: DWORD;
FGdiPlusHook: TGdiplusStartupOutput;
FGDIPresent: Boolean;
FGDIInitialized: Boolean;
UnitsLoader: TdxUnitsLoader;
function IsDLL: Boolean;
begin
Result := ModuleIsLib and not ModuleIsPackage;
end;
{ TdxLoader }
constructor TdxUnitsLoader.Create();
begin
FinalizeList := TList.Create;
InitializeList := TList.Create;
end;
destructor TdxUnitsLoader.Destroy;
begin
Finalize;
FinalizeList.Free;
InitializeList.Free;
inherited Destroy;
end;
procedure TdxUnitsLoader.AddUnit(const AInitializeProc, AFinalizeProc: Pointer);
var
AProc: TdxProc;
begin
if AInitializeProc <> nil then
begin
AProc := AInitializeProc;
if not IsDLL then
begin
FGDIInitialized := True;
AProc;
end
else
InitializeList.Add(AInitializeProc);
end;
if AFinalizeProc <> nil then
FinalizeList.Add(AFinalizeProc);
end;
procedure TdxUnitsLoader.RemoveUnit(const AFinalizeProc: Pointer);
var
AProc: TdxProc;
begin
AProc := AFinalizeProc;
if (FinalizeList.Remove(AFinalizeProc) >=0) and (AFinalizeProc <> nil) then
AProc;
end;
procedure TdxUnitsLoader.Finalize;
var
I: Integer;
AProc: TdxProc;
begin
if FGDIInitialized then
for I := FinalizeList.Count - 1 downto 0 do
begin
AProc := TdxProc(FinalizeList[I]);
if Assigned(AProc) then AProc;
end;
FGDIInitialized := False;
FinalizeList.Clear;
end;
procedure TdxUnitsLoader.Initialize;
var
I: Integer;
AProc: TdxProc;
begin
for I := 0 to InitializeList.Count - 1 do
begin
AProc := TdxProc(InitializeList[I]);
if Assigned(AProc) then AProc;
end;
InitializeList.Clear;
FGDIInitialized := True;
end;
{ GDI+ loading }
procedure GdiPlusLoad;
const
DefaultStartup: GdiplusStartupInput =
(GdiplusVersion: 1;
DebugEventCallback: nil;
SuppressBackgroundThread: True;
SuppressExternalCodecs: False);
function LoadGdiPlusMethod(const ProcName: string): Pointer;
begin
Result := nil;
if FGDIPresent then
Result := GetProcAddress(FGDIPlusLibrary, PChar(ProcName));
FGDIPresent := Result <> nil;
end;
begin
if FGDIPresent then
Exit;
FGDIPlusLibrary := SafeLoadLibrary(GDIPlusLibraryName);
FGDIPresent := FGDIPlusLibrary <> 0;
if FGDIPresent then
begin
// GDI+ Memory managment methods loading
GdipAlloc := LoadGdiPlusMethod('GdipAlloc');
GdipFree := LoadGdiPlusMethod('GdipFree');
// GDI+ initialization/finalization methods loading
GdiplusStartup := LoadGdiPlusMethod('GdiplusStartup');
GdiplusShutdown := LoadGdiPlusMethod('GdiplusShutdown');
// GDI+ Brush methods loading
GdipCloneBrush := LoadGdiPlusMethod('GdipCloneBrush');
GdipDeleteBrush := LoadGdiPlusMethod('GdipDeleteBrush');
GdipGetBrushType := LoadGdiPlusMethod('GdipGetBrushType');
// GDI+ Solid Brush methods loading
GdipCreateSolidFill := LoadGdiPlusMethod('GdipCreateSolidFill');
GdipSetSolidFillColor := LoadGdiPlusMethod('GdipSetSolidFillColor');
GdipGetSolidFillColor := LoadGdiPlusMethod('GdipGetSolidFillColor');
// GDI+ Gradient Brush methods loading
GdipCreateLineBrushFromRectI := LoadGdiPlusMethod('GdipCreateLineBrushFromRectI');
GdipGetLineRectI := LoadGdiPlusMethod('GdipGetLineRectI');
GdipSetLineColors := LoadGdiPlusMethod('GdipSetLineColors');
GdipGetLineColors := LoadGdiPlusMethod('GdipGetLineColors');
GdipSetLineWrapMode := LoadGdiPlusMethod('GdipSetLineWrapMode');
GdipGetLineWrapMode := LoadGdiPlusMethod('GdipGetLineWrapMode');
// GDI+ Hatch Brush methods loading
GdipCreateHatchBrush := LoadGdiPlusMethod('GdipCreateHatchBrush');
GdipGetHatchStyle := LoadGdiPlusMethod('GdipGetHatchStyle');
GdipGetHatchForegroundColor := LoadGdiPlusMethod('GdipGetHatchForegroundColor');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -