home *** CD-ROM | disk | FTP | other *** search
- {*******************************************************}
- { }
- { Delphi Visual Component Library }
- { }
- { Copyright (c) 1995 Borland International }
- { }
- {*******************************************************}
-
- unit Graphics;
-
- {$P+,S-,W-,R-}
- {$C PRELOAD}
-
- interface
-
- uses WinTypes, WinProcs, SysUtils, Classes;
-
- { Graphics Objects }
-
- type
- TColor = -(COLOR_ENDCOLORS + 1)..$2FFFFFF;
-
- const
- clScrollBar = TColor(-COLOR_SCROLLBAR - 1);
- clBackground = TColor(-COLOR_BACKGROUND - 1);
- clActiveCaption = TColor(-COLOR_ACTIVECAPTION - 1);
- clInactiveCaption = TColor(-COLOR_INACTIVECAPTION - 1);
- clMenu = TColor(-COLOR_MENU - 1);
- clWindow = TColor(-COLOR_WINDOW - 1);
- clWindowFrame = TColor(-COLOR_WINDOWFRAME - 1);
- clMenuText = TColor(-COLOR_MENUTEXT - 1);
- clWindowText = TColor(-COLOR_WINDOWTEXT - 1);
- clCaptionText = TColor(-COLOR_CAPTIONTEXT - 1);
- clActiveBorder = TColor(-COLOR_ACTIVEBORDER - 1);
- clInactiveBorder = TColor(-COLOR_INACTIVEBORDER - 1);
- clAppWorkSpace = TColor(-COLOR_APPWORKSPACE - 1);
- clHighlight = TColor(-COLOR_HIGHLIGHT - 1);
- clHighlightText = TColor(-COLOR_HIGHLIGHTTEXT - 1);
- clBtnFace = TColor(-COLOR_BTNFACE - 1);
- clBtnShadow = TColor(-COLOR_BTNSHADOW - 1);
- clGrayText = TColor(-COLOR_GRAYTEXT - 1);
- clBtnText = TColor(-COLOR_BTNTEXT - 1);
- clInactiveCaptionText = TColor(-COLOR_INACTIVECAPTIONTEXT - 1);
- clBtnHighlight = TColor(-COLOR_BTNHIGHLIGHT - 1);
-
- clBlack = TColor($000000);
- clMaroon = TColor($000080);
- clGreen = TColor($008000);
- clOlive = TColor($008080);
- clNavy = TColor($800000);
- clPurple = TColor($800080);
- clTeal = TColor($808000);
- clGray = TColor($808080);
- clSilver = TColor($C0C0C0);
- clRed = TColor($0000FF);
- clLime = TColor($00FF00);
- clYellow = TColor($00FFFF);
- clBlue = TColor($FF0000);
- clFuchsia = TColor($FF00FF);
- clAqua = TColor($FFFF00);
- clLtGray = TColor($C0C0C0);
- clDkGray = TColor($808080);
- clWhite = TColor($FFFFFF);
-
- const
- cmBlackness = BLACKNESS;
- cmDstInvert = DSTINVERT;
- cmMergeCopy = MERGECOPY;
- cmMergePaint = MERGEPAINT;
- cmNotSrcCopy = NOTSRCCOPY;
- cmNotSrcErase = NOTSRCERASE;
- cmPatCopy = PATCOPY;
- cmPatInvert = PATINVERT;
- cmPatPaint = PATPAINT;
- cmSrcAnd = SRCAND;
- cmSrcCopy = SRCCOPY;
- cmSrcErase = SRCERASE;
- cmSrcInvert = SRCINVERT;
- cmSrcPaint = SRCPAINT;
- cmWhiteness = WHITENESS;
-
- type
- HMETAFILE = THandle;
- TExtension = string[3];
-
- EInvalidGraphic = class(Exception);
- EInvalidGraphicOperation = class(Exception);
-
- TGraphic = class;
- TBitmap = class;
- TIcon = class;
- TMetafile = class;
-
- TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
- TFontStyles = set of TFontStyle;
- TFontPitch = (fpDefault, fpVariable, fpFixed);
- TFontName = string[LF_FACESIZE - 1];
-
- TPenStyle = (psSolid, psDash, psDot, psDashDot, psDashDotDot, psClear,
- psInsideFrame);
- TPenMode = (pmBlack, pmWhite, pmNop, pmNot, pmCopy, pmNotCopy,
- pmMergePenNot, pmMaskPenNot, pmMergeNotPen, pmMaskNotPen, pmMerge,
- pmNotMerge, pmMask, pmNotMask, pmXor, pmNotXor);
-
- TBrushStyle = (bsSolid, bsClear, bsHorizontal, bsVertical,
- bsFDiagonal, bsBDiagonal, bsCross, bsDiagCross);
-
- TBrushData = record
- Handle: HBrush;
- Color: TColor;
- Bitmap: TBitmap;
- Style: TBrushStyle;
- end;
-
- TGraphicsObject = class(TPersistent)
- protected
- procedure Changed; dynamic;
- public
- property OnChange: TNotifyEvent;
- end;
-
- TFont = class(TGraphicsObject)
- protected
- function GetHandle: HFont;
- function GetHeight: Integer;
- function GetName: TFontName;
- function GetPitch: TFontPitch;
- function GetSize: Integer;
- function GetStyle: TFontStyles;
- procedure SetColor(Value: TColor);
- procedure SetHandle(Value: HFont);
- procedure SetHeight(Value: Integer);
- procedure SetName(const Value: TFontName);
- procedure SetPitch(Value: TFontPitch);
- procedure SetSize(Value: Integer);
- procedure SetStyle(Value: TFontStyles);
- public
- constructor Create;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- property Handle: HFont;
- property PixelsPerInch: Integer;
- published
- property Color: TColor;
- property Height: Integer;
- property Name: TFontName;
- property Pitch: TFontPitch default fpDefault;
- property Size: Integer;
- property Style: TFontStyles;
- end;
-
- TPen = class(TGraphicsObject)
- protected
- function GetColor: TColor;
- procedure SetColor(Value: TColor);
- function GetHandle: HPen;
- procedure SetHandle(Value: HPen);
- procedure SetMode(Value: TPenMode);
- function GetStyle: TPenStyle;
- procedure SetStyle(Value: TPenStyle);
- function GetWidth: Integer;
- procedure SetWidth(Value: Integer);
- public
- constructor Create;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- property Handle: HPen;
- published
- property Color: TColor default clBlack;
- property Mode: TPenMode default pmCopy;
- property Style: TPenStyle default psSolid;
- property Width: Integer default 1;
- end;
-
- TBrush = class(TGraphicsObject)
- protected
- function GetBitmap: TBitmap;
- procedure SetBitmap(Value: TBitmap);
- function GetColor: TColor;
- procedure SetColor(Value: TColor);
- function GetHandle: HBrush;
- procedure SetHandle(Value: HBrush);
- function GetStyle: TBrushStyle;
- procedure SetStyle(Value: TBrushStyle);
- public
- constructor Create;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- property Bitmap: TBitmap;
- property Handle: HBrush;
- published
- property Color: TColor default clWhite;
- property Style: TBrushStyle default bsSolid;
- end;
-
- TFillStyle = (fsSurface, fsBorder);
-
- TCopyMode = Longint;
-
- TCanvasStates = (csHandleValid, csFontValid, csPenValid, csBrushValid);
- TCanvasState = set of TCanvasStates;
-
- TCanvas = class(TPersistent)
- protected
- procedure Changed; virtual;
- procedure Changing; virtual;
- procedure CreateHandle; virtual;
- public
- constructor Create;
- destructor Destroy; override;
- procedure Arc(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
- procedure BrushCopy(const Dest: TRect; Bitmap: TBitmap;
- const Source: TRect; Color: TColor);
- procedure Chord(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
- procedure CopyRect(const Dest: TRect; Canvas: TCanvas;
- const Source: TRect);
- procedure Draw(X, Y: Integer; Graphic: TGraphic);
- procedure DrawFocusRect(const Rect: TRect);
- procedure Ellipse(X1, Y1, X2, Y2: Integer);
- procedure FillRect(const Rect: TRect);
- procedure FloodFill(X, Y: Integer; Color: TColor; FillStyle: TFillStyle);
- procedure FrameRect(const Rect: TRect);
- procedure LineTo(X, Y: Integer);
- procedure MoveTo(X, Y: Integer);
- procedure Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
- procedure Polygon(const Points: array of TPoint);
- procedure Polyline(const Points: array of TPoint);
- procedure Rectangle(X1, Y1, X2, Y2: Integer);
- procedure Refresh;
- procedure RoundRect(X1, Y1, X2, Y2, X3, Y3: Integer);
- procedure StretchDraw(const Rect: TRect; Graphic: TGraphic);
- function TextHeight(const Text: string): Integer;
- procedure TextOut(X, Y: Integer; const Text: string);
- procedure TextRect(Rect: TRect; X, Y: Integer; const Text: string);
- function TextWidth(const Text: string): Integer;
- property ClipRect: TRect;
- property Handle: HDC;
- property PenPos: TPoint;
- property Pixels[X, Y: Integer]: TColor;
- property OnChange: TNotifyEvent;
- property OnChanging: TNotifyEvent;
- published
- property Brush: TBrush;
- property CopyMode: TCopyMode default cmSrcCopy;
- property Font: TFont;
- property Pen: TPen;
- end;
-
- { The TGraphic class is a abstract base class for dealing with graphic images
- such as metafile, bitmaps and icons; but is not limited to such.
- LoadFromFile - Read the graphic from the file system. The old contents of
- the graphic are lost. If the file is not of the right format, an
- exception will be generated.
- SaveToFile - Writes the graphic to disk in the file provided.
- LoadFromStream - Like LoadFromFile except source is a stream (e.g.
- TBlobStream).
- SaveToStream - stream analogue of SaveToFile.
- LoadFromClipboardFormat - Replaces the current image with the data
- provided. If the TGraphic does not support that format it will generate
- an exception.
- SaveToClipboardFormats - Converts the image to a clipboard format. If the
- image does not support being translated into a clipboard format it
- will generate an exception.
- Height - The native, unstretched, height of the graphic.
- Width - The native, unstretched, width of the graphic.
- OnChange - Called whenever the graphic changes }
-
- TGraphic = class(TPersistent)
- protected
- constructor Create; virtual;
- procedure Changed(Sender: TObject);
- procedure DefineProperties(Filer: TFiler); override;
- procedure Draw(ACanvas: TCanvas; const Rect: TRect); virtual; abstract;
- function GetEmpty: Boolean; virtual; abstract;
- function GetHeight: Integer; virtual; abstract;
- function GetWidth: Integer; virtual; abstract;
- procedure ReadData(Stream: TStream); virtual;
- procedure SetHeight(Value: Integer); virtual; abstract;
- procedure SetWidth(Value: Integer); virtual; abstract;
- procedure WriteData(Stream: TStream); virtual;
- public
- procedure Assign(Source: TPersistent); override;
- procedure LoadFromFile(const Filename: string); virtual;
- procedure SaveToFile(const Filename: string); virtual;
- procedure LoadFromStream(Stream: TStream); virtual; abstract;
- procedure SaveToStream(Stream: TStream); virtual; abstract;
- procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
- APalette: HPALETTE); virtual; abstract;
- procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
- var APalette: HPALETTE); virtual; abstract;
- property Empty: Boolean;
- property Height: Integer;
- property Modified: Boolean;
- property Width: Integer;
- property OnChange: TNotifyEvent;
- end;
-
- TGraphicClass = class of TGraphic;
-
- { TPicture }
- { TPicture is a TGraphic container. It is used in place of a TGraphic if the
- graphic can be of any TGraphic class. LoadFromFile and SaveToFile are
- polymorphic. For example, if the TPicture is holding an Icon, you can
- LoadFromFile a bitmap file, where if the class was TIcon you could only read
- .ICO files.
- LoadFromFile - Reads a picture from disk. The TGraphic class created
- determined by the file extension of the file. If the file extension is
- not recognized an exception is generated.
- SaveToFile - Writes the picture to disk.
- LoadFromClipboardFormat - Reads the picture from the handle provided in
- the given clipboard format. If the format is not supported, an
- exception is generated.
- SaveToClipboardFormats - Allocates a global handle and writes the picture
- in its native clipboard format (CF_BITMAP for bitmaps, CF_METAFILE
- for metafiles, etc.). Formats will contain the formats written.
- Returns the number of clipboard items written to the array pointed to
- by Formats and Datas or would be written if either Formats or Datas are
- nil.
- SupportsClipboardFormat - Returns true if the given clipboard format
- is supported by LoadFromClipboardFormat.
- Assign - Copys the contents of the given TPicture. Used most often in
- the implementation of TPicture properties.
- RegisterFileFormat - Register a new TGraphic class for use in
- LoadFromFile.
- RegisterClipboardFormat - Registers a new TGraphic class for use in
- LoadFromClipboardFormat.
- Height - The native, unstretched, height of the picture.
- Width - The native, unstretched, width of the picture.
- Graphic - The TGraphic object contained by the TPicture
- Bitmap - Returns a bitmap. If the contents is not already a bitmap, the
- contents are thrown away and a blank bitmap is returned.
- Icon - Returns an icon. If the contents is not already an icon, the
- contents are thrown away and a blank icon is returned.
- Metafile - Returns a metafile. If the contents is not already a bitmap,
- the contents are thrown away and a blank metafile is returned. }
- TPicture = class(TPersistent)
- protected
- procedure Changed(Sender: TObject);
- procedure DefineProperties(Filer: TFiler); override;
- public
- destructor Destroy; override;
- procedure LoadFromFile(const Filename: string);
- procedure SaveToFile(const Filename: string);
- procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
- APalette: HPALETTE);
- procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
- var APalette: HPALETTE);
- class function SupportsClipboardFormat(AFormat: Word): Boolean;
- procedure Assign(Source: TPersistent); override;
- class procedure RegisterFileFormat(const AExtension, ADescription: string;
- AGraphicClass: TGraphicClass);
- class procedure RegisterClipboardFormat(AFormat: Word;
- AGraphicClass: TGraphicClass);
- property Bitmap: TBitmap;
- property Graphic: TGraphic;
- property Height: Integer;
- property Icon: TIcon;
- property Metafile: TMetafile;
- property Width: Integer;
- property OnChange: TNotifyEvent;
- end;
-
- { TMetafile }
- { TMetafile is an encapsulation of Windows metafile rendering.
- Handle - The metafile handle.
- Inch - The units per inch assumed by the metafile. Changing this
- value changes the coordinate system and, therefore, the width
- and height of the metafile. New metafiles default to the device
- LOGPIXELSPERINCH value given by Windows GDI. }
-
- TMetafile = class(TGraphic)
- protected
- function GetEmpty: Boolean; override;
- function GetHeight: Integer; override;
- function GetWidth: Integer; override;
- procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
- procedure ReadData(Stream: TStream); override;
- procedure SetHeight(Value: Integer); override;
- procedure SetWidth(Value: Integer); override;
- procedure WriteData(Stream: TStream); override;
- public
- constructor Create;
- destructor Destroy; override;
- procedure LoadFromStream(Stream: TStream); override;
- procedure SaveToStream(Stream: TStream); override;
- procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
- APalette: HPALETTE); override;
- procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
- var APalette: HPALETTE); override;
- procedure Assign(Source: TPersistent); override;
- property Handle: HMETAFILE;
- property Inch: Word;
- end;
-
- { TBitmap }
- { TBitmap is an encapuslation of a Windows HBITMAP and HPALETTE. It manages
- the palette realizing automatically as well as having a Canvas to allow
- modifications to the palette. Creating copies of a TBitmap is very fast
- since the handles is copied not the image. If the image is modified, and
- the handle is shared by more than one TBitmap object, the image is copied
- before the modification is performed (i.e. copy on write).
- Canvas - Allows drawing on the bitmap.
- Handle - The HBITMAP encapsulated by the TBitmap. Grabbing the handle
- directly should be avoided since it causes the HBITMAP to be copied if
- more than one TBitmap share the handle.
- Palette - The HPALETTE realized by the TBitmap. Grabbing this handle
- directly should be avoided since it causes the HPALETTE to be copied if
- more than one TBitmap share the handle.
- Monochrome - True if the bitmap is a monochrome bitmap }
-
- TBitmap = class(TGraphic)
- protected
- procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
- function GetEmpty: Boolean; override;
- function GetHeight: Integer; override;
- function GetWidth: Integer; override;
- procedure ReadData(Stream: TStream); override;
- procedure SetWidth(Value: Integer); override;
- procedure SetHeight(Value: Integer); override;
- procedure WriteData(Stream: TStream); override;
- public
- constructor Create; override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure Dormant;
- procedure FreeImage;
- procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
- APalette: HPALETTE); override;
- procedure LoadFromStream(Stream: TStream); override;
- function ReleaseHandle: HBITMAP;
- function ReleasePalette: HPALETTE;
- procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
- var APalette: HPALETTE); override;
- procedure SaveToStream(Stream: TStream); override;
- property Canvas: TCanvas;
- property Handle: HBITMAP;
- property Monochrome: Boolean;
- property Palette: HPALETTE;
- property TransparentColor: TColor;
- end;
-
- { TIcon }
- { TIcon encapsulates window HICON handle. Drawing of an icon does not stretch
- so calling stretch draw is not meaningful.
- Handle - The HICON used by the TIcon. }
-
- TIcon = class(TGraphic)
- protected
- procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
- function GetEmpty: Boolean; override;
- function GetHeight: Integer; override;
- function GetWidth: Integer; override;
- procedure SetHeight(Value: Integer); override;
- procedure SetWidth(Value: Integer); override;
- procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
- var APalette: HPALETTE); override;
- public
- constructor Create; override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure LoadFromStream(Stream: TStream); override;
- function ReleaseHandle: HICON;
- procedure SaveToStream(Stream: TStream); override;
- property Handle: HICON;
- end;
-
- { TImageList }
-
- TImageList = class
- public
- constructor Create(AWidth, AHeight: Integer);
- destructor Destroy; override;
- function Add(Image, Mask: TBitmap): Integer;
- function AddMasked(Image: TBitmap; MaskColor: TColor): Integer;
- procedure Replace(Index: Integer; Image, Mask: TBitmap);
- procedure ReplaceMasked(Index: Integer; Image: TBitmap; MaskColor: TColor);
- procedure Draw(Canvas: TCanvas; X, Y: Integer; Index: Integer);
- procedure Delete(Index: Integer);
- property Count: Integer;
- property Delta: Integer;
- property Width: Integer;
- property Height: Integer;
- end;
-
- function GraphicFilter(GraphicClass: TGraphicClass): string;
- function GraphicExtension(GraphicClass: TGraphicClass): string;
-
- function ColorToRGB(Color: TColor): Longint;
- function ColorToString(Color: TColor): string;
- function StringToColor(S: string): TColor;
- procedure GetColorValues(Proc: TGetStrProc);
- function ColorToIdent(Color: Longint; var Ident: string): Boolean;
- function IdentToColor(const Ident: string; var Color: Longint): Boolean;
-
- function MemAlloc(Size: Longint): Pointer;
- procedure GetDIBSizes(Bitmap: HBITMAP; var InfoHeaderSize: Integer;
- var ImageSize: Longint);
- function GetDIB(Bitmap: HBITMAP; Palette: HPALETTE; var BitmapInfo; var Bits): Boolean;
-
- procedure InitGraphics;
- procedure PaletteChanged;
- procedure FreeMemoryContexts;
-
- implementation
-