home *** CD-ROM | disk | FTP | other *** search
/ PC World Plus! (NZ) 2001 June / HDC50.iso / Runimage / Delphi50 / Doc / GRAPHICS.INT < prev    next >
Text File  |  1999-08-11  |  33KB  |  814 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1995,99 Inprise Corporation       }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit Graphics;
  11.  
  12. {$P+,S-,W-,R-,T-,X+,H+}
  13. {$C PRELOAD}
  14.  
  15. interface
  16.  
  17. uses Windows, SysUtils, Classes;
  18.  
  19. { Graphics Objects }
  20.  
  21. type
  22.   PColor = ^TColor;
  23.   TColor = -$7FFFFFFF-1..$7FFFFFFF;
  24.   {$NODEFINE TColor}
  25.  
  26.   (*$HPPEMIT 'namespace Graphics'*)
  27.   (*$HPPEMIT '{'*)
  28.   (*$HPPEMIT '  enum TColor {clMin=-0x7fffffff-1, clMax=0x7fffffff};'*)
  29.   (*$HPPEMIT '}'*)
  30.  
  31.  
  32. const
  33.   clScrollBar = TColor(COLOR_SCROLLBAR or $80000000);
  34.   clBackground = TColor(COLOR_BACKGROUND or $80000000);
  35.   clActiveCaption = TColor(COLOR_ACTIVECAPTION or $80000000);
  36.   clInactiveCaption = TColor(COLOR_INACTIVECAPTION or $80000000);
  37.   clMenu = TColor(COLOR_MENU or $80000000);
  38.   clWindow = TColor(COLOR_WINDOW or $80000000);
  39.   clWindowFrame = TColor(COLOR_WINDOWFRAME or $80000000);
  40.   clMenuText = TColor(COLOR_MENUTEXT or $80000000);
  41.   clWindowText = TColor(COLOR_WINDOWTEXT or $80000000);
  42.   clCaptionText = TColor(COLOR_CAPTIONTEXT or $80000000);
  43.   clActiveBorder = TColor(COLOR_ACTIVEBORDER or $80000000);
  44.   clInactiveBorder = TColor(COLOR_INACTIVEBORDER or $80000000);
  45.   clAppWorkSpace = TColor(COLOR_APPWORKSPACE or $80000000);
  46.   clHighlight = TColor(COLOR_HIGHLIGHT or $80000000);
  47.   clHighlightText = TColor(COLOR_HIGHLIGHTTEXT or $80000000);
  48.   clBtnFace = TColor(COLOR_BTNFACE or $80000000);
  49.   clBtnShadow = TColor(COLOR_BTNSHADOW or $80000000);
  50.   clGrayText = TColor(COLOR_GRAYTEXT or $80000000);
  51.   clBtnText = TColor(COLOR_BTNTEXT or $80000000);
  52.   clInactiveCaptionText = TColor(COLOR_INACTIVECAPTIONTEXT or $80000000);
  53.   clBtnHighlight = TColor(COLOR_BTNHIGHLIGHT or $80000000);
  54.   cl3DDkShadow = TColor(COLOR_3DDKSHADOW or $80000000);
  55.   cl3DLight = TColor(COLOR_3DLIGHT or $80000000);
  56.   clInfoText = TColor(COLOR_INFOTEXT or $80000000);
  57.   clInfoBk = TColor(COLOR_INFOBK or $80000000);
  58.  
  59.   clBlack = TColor($000000);
  60.   clMaroon = TColor($000080);
  61.   clGreen = TColor($008000);
  62.   clOlive = TColor($008080);
  63.   clNavy = TColor($800000);
  64.   clPurple = TColor($800080);
  65.   clTeal = TColor($808000);
  66.   clGray = TColor($808080);
  67.   clSilver = TColor($C0C0C0);
  68.   clRed = TColor($0000FF);
  69.   clLime = TColor($00FF00);
  70.   clYellow = TColor($00FFFF);
  71.   clBlue = TColor($FF0000);
  72.   clFuchsia = TColor($FF00FF);
  73.   clAqua = TColor($FFFF00);
  74.   clLtGray = TColor($C0C0C0);
  75.   clDkGray = TColor($808080);
  76.   clWhite = TColor($FFFFFF);
  77.   clNone = TColor($1FFFFFFF);
  78.   clDefault = TColor($20000000);
  79.  
  80. const
  81.   cmBlackness = BLACKNESS;
  82.   cmDstInvert = DSTINVERT;
  83.   cmMergeCopy = MERGECOPY;
  84.   cmMergePaint = MERGEPAINT;
  85.   cmNotSrcCopy = NOTSRCCOPY;
  86.   cmNotSrcErase = NOTSRCERASE;
  87.   cmPatCopy = PATCOPY;
  88.   cmPatInvert = PATINVERT;
  89.   cmPatPaint = PATPAINT;
  90.   cmSrcAnd = SRCAND;
  91.   cmSrcCopy = SRCCOPY;
  92.   cmSrcErase = SRCERASE;
  93.   cmSrcInvert = SRCINVERT;
  94.   cmSrcPaint = SRCPAINT;
  95.   cmWhiteness = WHITENESS;
  96.  
  97. type
  98.   {$EXTERNALSYM HMETAFILE}
  99.   HMETAFILE = THandle;
  100.   {$EXTERNALSYM HENHMETAFILE}
  101.   HENHMETAFILE = THandle;
  102.  
  103.   EInvalidGraphic = class(Exception);
  104.   EInvalidGraphicOperation = class(Exception);
  105.  
  106.   TGraphic = class;
  107.   TBitmap = class;
  108.   TIcon = class;
  109.   TMetafile = class;
  110.  
  111.   TResData = record
  112.     Handle: THandle;
  113.   end;
  114.  
  115.   TFontPitch = (fpDefault, fpVariable, fpFixed);
  116.   TFontName = type string;
  117.   TFontCharset = 0..255;
  118.  
  119.   { Changes to the following types should be reflected in the $HPPEMIT directives. }
  120.  
  121.   TFontDataName = string[LF_FACESIZE - 1];
  122.   {$NODEFINE TFontDataName}
  123.   TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
  124.   {$NODEFINE TFontStyle}
  125.   TFontStyles = set of TFontStyle;
  126.   TFontStylesBase = set of TFontStyle;
  127.   {$NODEFINE TFontStylesBase}
  128.  
  129.   (*$HPPEMIT 'namespace Graphics'*)
  130.   (*$HPPEMIT '{'*)
  131.   (*$HPPEMIT '  enum TFontStyle { fsBold, fsItalic, fsUnderline, fsStrikeOut };'*)
  132.   (*$HPPEMIT '  typedef SmallStringBase<31> TFontDataName;'*)
  133.   (*$HPPEMIT '  typedef SetBase<TFontStyle, fsBold, fsStrikeOut> TFontStylesBase;'*)
  134.   (*$HPPEMIT '}'*)
  135.  
  136.   TFontData = record
  137.     Handle: HFont;
  138.     Height: Integer;
  139.     Pitch: TFontPitch;
  140.     Style: TFontStylesBase;
  141.     Charset: TFontCharset;
  142.     Name: TFontDataName;
  143.   end;
  144.  
  145.   TPenStyle = (psSolid, psDash, psDot, psDashDot, psDashDotDot, psClear,
  146.     psInsideFrame);
  147.   TPenMode = (pmBlack, pmWhite, pmNop, pmNot, pmCopy, pmNotCopy,
  148.     pmMergePenNot, pmMaskPenNot, pmMergeNotPen, pmMaskNotPen, pmMerge,
  149.     pmNotMerge, pmMask, pmNotMask, pmXor, pmNotXor);
  150.  
  151.   TPenData = record
  152.     Handle: HPen;
  153.     Color: TColor;
  154.     Width: Integer;
  155.     Style: TPenStyle;
  156.   end;
  157.  
  158.   TBrushStyle = (bsSolid, bsClear, bsHorizontal, bsVertical,
  159.     bsFDiagonal, bsBDiagonal, bsCross, bsDiagCross);
  160.  
  161.   TBrushData = record
  162.     Handle: HBrush;
  163.     Color: TColor;
  164.     Bitmap: TBitmap;
  165.     Style: TBrushStyle;
  166.   end;
  167.  
  168.   PResource = ^TResource;
  169.   TResource = record
  170.     Next: PResource;
  171.     RefCount: Integer;
  172.     Handle: THandle;
  173.     HashCode: Word;
  174.     case Integer of
  175.       0: (Data: TResData);
  176.       1: (Font: TFontData);
  177.       2: (Pen: TPenData);
  178.       3: (Brush: TBrushData);
  179.   end;
  180.  
  181.   TGraphicsObject = class(TPersistent)
  182.   protected
  183.     procedure Changed; dynamic;
  184.     procedure Lock;
  185.     procedure Unlock;
  186.   public
  187.     property OnChange: TNotifyEvent;
  188.     property OwnerCriticalSection: PRTLCriticalSection;
  189.   end;
  190.  
  191.   IChangeNotifier = interface
  192.     ['{1FB62321-44A7-11D0-9E93-0020AF3D82DA}']
  193.     procedure Changed;
  194.   end;
  195.  
  196.   TFont = class(TGraphicsObject)
  197.   protected
  198.     procedure Changed; override;
  199.     function GetHandle: HFont;
  200.     function GetHeight: Integer;
  201.     function GetName: TFontName;
  202.     function GetPitch: TFontPitch;
  203.     function GetSize: Integer;
  204.     function GetStyle: TFontStyles;
  205.     function GetCharset: TFontCharset;
  206.     procedure SetColor(Value: TColor);
  207.     procedure SetHandle(Value: HFont);
  208.     procedure SetHeight(Value: Integer);
  209.     procedure SetName(const Value: TFontName);
  210.     procedure SetPitch(Value: TFontPitch);
  211.     procedure SetSize(Value: Integer);
  212.     procedure SetStyle(Value: TFontStyles);
  213.     procedure SetCharset(Value: TFontCharset);
  214.   public
  215.     constructor Create;
  216.     destructor Destroy; override;
  217.     procedure Assign(Source: TPersistent); override;
  218.     property FontAdapter: IChangeNotifier;
  219.     property Handle: HFont;
  220.     property PixelsPerInch: Integer;
  221.   published
  222.     property Charset: TFontCharset;
  223.     property Color: TColor;
  224.     property Height: Integer;
  225.     property Name: TFontName;
  226.     property Pitch: TFontPitch default fpDefault;
  227.     property Size: Integer;
  228.     property Style: TFontStyles;
  229.   end;
  230.  
  231.   TPen = class(TGraphicsObject)
  232.   protected
  233.     function GetColor: TColor;
  234.     procedure SetColor(Value: TColor);
  235.     function GetHandle: HPen;
  236.     procedure SetHandle(Value: HPen);
  237.     procedure SetMode(Value: TPenMode);
  238.     function GetStyle: TPenStyle;
  239.     procedure SetStyle(Value: TPenStyle);
  240.     function GetWidth: Integer;
  241.     procedure SetWidth(Value: Integer);
  242.   public
  243.     constructor Create;
  244.     destructor Destroy; override;
  245.     procedure Assign(Source: TPersistent); override;
  246.     property Handle: HPen;
  247.   published
  248.     property Color: TColor default clBlack;
  249.     property Mode: TPenMode default pmCopy;
  250.     property Style: TPenStyle default psSolid;
  251.     property Width: Integer default 1;
  252.   end;
  253.  
  254.   TBrush = class(TGraphicsObject)
  255.   protected
  256.     function GetBitmap: TBitmap;
  257.     procedure SetBitmap(Value: TBitmap);
  258.     function GetColor: TColor;
  259.     procedure SetColor(Value: TColor);
  260.     function GetHandle: HBrush;
  261.     procedure SetHandle(Value: HBrush);
  262.     function GetStyle: TBrushStyle;
  263.     procedure SetStyle(Value: TBrushStyle);
  264.   public
  265.     constructor Create;
  266.     destructor Destroy; override;
  267.     procedure Assign(Source: TPersistent); override;
  268.     property Bitmap: TBitmap;
  269.     property Handle: HBrush;
  270.   published
  271.     property Color: TColor default clWhite;
  272.     property Style: TBrushStyle default bsSolid;
  273.   end;
  274.  
  275.   TFillStyle = (fsSurface, fsBorder);
  276.   TFillMode = (fmAlternate, fmWinding);
  277.  
  278.   TCopyMode = Longint;
  279.  
  280.   TCanvasStates = (csHandleValid, csFontValid, csPenValid, csBrushValid);
  281.   TCanvasState = set of TCanvasStates;
  282.   TCanvasOrientation = (coLeftToRight, coRightToLeft);
  283.  
  284.   TCanvas = class(TPersistent)
  285.   protected
  286.     procedure Changed; virtual;
  287.     procedure Changing; virtual;
  288.     procedure CreateHandle; virtual;
  289.     procedure RequiredState(ReqState: TCanvasState);
  290.   public
  291.     constructor Create;
  292.     destructor Destroy; override;
  293.     procedure Arc(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
  294.     procedure BrushCopy(const Dest: TRect; Bitmap: TBitmap;
  295.       const Source: TRect; Color: TColor);
  296.     procedure Chord(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
  297.     procedure CopyRect(const Dest: TRect; Canvas: TCanvas;
  298.       const Source: TRect);
  299.     procedure Draw(X, Y: Integer; Graphic: TGraphic);
  300.     procedure DrawFocusRect(const Rect: TRect);
  301.     procedure Ellipse(X1, Y1, X2, Y2: Integer); overload;
  302.     procedure Ellipse(const Rect: TRect); overload;
  303.     procedure FillRect(const Rect: TRect);
  304.     procedure FloodFill(X, Y: Integer; Color: TColor; FillStyle: TFillStyle);
  305.     procedure FrameRect(const Rect: TRect);
  306.     procedure LineTo(X, Y: Integer);
  307.     procedure Lock;
  308.     procedure MoveTo(X, Y: Integer);
  309.     procedure Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
  310.     procedure Polygon(const Points: array of TPoint);
  311.     procedure Polyline(const Points: array of TPoint);
  312.     procedure PolyBezier(const Points: array of TPoint);
  313.     procedure PolyBezierTo(const Points: array of TPoint);
  314.     procedure Rectangle(X1, Y1, X2, Y2: Integer); overload;
  315.     procedure Rectangle(const Rect: TRect); overload;
  316.     procedure Refresh;
  317.     procedure RoundRect(X1, Y1, X2, Y2, X3, Y3: Integer);
  318.     procedure StretchDraw(const Rect: TRect; Graphic: TGraphic);
  319.     function TextExtent(const Text: string): TSize;
  320.     function TextHeight(const Text: string): Integer;
  321.     procedure TextOut(X, Y: Integer; const Text: string);
  322.     procedure TextRect(Rect: TRect; X, Y: Integer; const Text: string);
  323.     function TextWidth(const Text: string): Integer;
  324.     function TryLock: Boolean;
  325.     procedure Unlock;
  326.     property ClipRect: TRect;
  327.     property Handle: HDC;
  328.     property LockCount: Integer;
  329.     property CanvasOrientation: TCanvasOrientation;
  330.     property PenPos: TPoint;
  331.     property Pixels[X, Y: Integer]: TColor;
  332.     property TextFlags: Longint;
  333.     property OnChange: TNotifyEvent;
  334.     property OnChanging: TNotifyEvent;
  335.   published
  336.     property Brush: TBrush;
  337.     property CopyMode: TCopyMode default cmSrcCopy;
  338.     property Font: TFont;
  339.     property Pen: TPen;
  340.   end;
  341.  
  342.   { TProgressEvent is a generic progress notification event which may be
  343.         used by TGraphic classes with computationally intensive (slow)
  344.         operations, such as loading, storing, or transforming image data.
  345.     Event params:
  346.       Stage - Indicates whether this call to the OnProgress event is to
  347.         prepare for, process, or clean up after a graphic operation.  If
  348.         OnProgress is called at all, the first call for a graphic operation
  349.         will be with Stage = psStarting, to allow the OnProgress event handler
  350.         to allocate whatever resources it needs to process subsequent progress
  351.         notifications.  After Stage = psStarting, you are guaranteed that
  352.         OnProgress will be called again with Stage = psEnding to allow you
  353.         to free those resources, even if the graphic operation is aborted by
  354.         an exception.  Zero or more calls to OnProgress with Stage = psRunning
  355.         may occur between the psStarting and psEnding calls.
  356.       PercentDone - The ratio of work done to work remaining, on a scale of
  357.         0 to 100.  Values may repeat or even regress (get smaller) in
  358.         successive calls.  PercentDone is usually only a guess, and the
  359.         guess may be dramatically altered as new information is discovered
  360.         in decoding the image.
  361.       RedrawNow - Indicates whether the graphic can be/should be redrawn
  362.         immediately.  Useful for showing successive approximations of
  363.         an image as data is available instead of waiting for all the data
  364.         to arrive before drawing anything.  Since there is no message loop
  365.         activity during graphic operations, you should call Update to force
  366.         a control to be redrawn immediately in the OnProgress event handler.
  367.         Redrawing a graphic when RedrawNow = False could corrupt the image
  368.         and/or cause exceptions.
  369.       Rect - Area of image that has changed and needs to be redrawn.
  370.       Msg - Optional text describing in one or two words what the graphic
  371.         class is currently working on.  Ex:  "Loading" "Storing"
  372.         "Reducing colors".  The Msg string can also be empty.
  373.         Msg strings should be resourced for translation,  should not
  374.         contain trailing periods, and should be used only for
  375.         display purposes.  (do not: if Msg = 'Loading' then...)
  376.   }
  377.  
  378.   TProgressStage = (psStarting, psRunning, psEnding);
  379.   TProgressEvent = procedure (Sender: TObject; Stage: TProgressStage;
  380.     PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string) of object;
  381.  
  382.   { The TGraphic class is a abstract base class for dealing with graphic images
  383.     such as metafile, bitmaps, icons, and other image formats.
  384.       LoadFromFile - Read the graphic from the file system.  The old contents of
  385.         the graphic are lost.  If the file is not of the right format, an
  386.         exception will be generated.
  387.       SaveToFile - Writes the graphic to disk in the file provided.
  388.       LoadFromStream - Like LoadFromFile except source is a stream (e.g.
  389.         TBlobStream).
  390.       SaveToStream - stream analogue of SaveToFile.
  391.       LoadFromClipboardFormat - Replaces the current image with the data
  392.         provided.  If the TGraphic does not support that format it will generate
  393.         an exception.
  394.       SaveToClipboardFormats - Converts the image to a clipboard format.  If the
  395.         image does not support being translated into a clipboard format it
  396.         will generate an exception.
  397.       Height - The native, unstretched, height of the graphic.
  398.       Palette - Color palette of image.  Zero if graphic doesn't need/use palettes.
  399.       Transparent - Image does not completely cover its rectangular area
  400.       Width - The native, unstretched, width of the graphic.
  401.       OnChange - Called whenever the graphic changes
  402.       PaletteModified - Indicates in OnChange whether color palette has changed.
  403.         Stays true until whoever's responsible for realizing this new palette
  404.         (ex: TImage) sets it to False.
  405.       OnProgress - Generic progress indicator event. Propagates out to TPicture
  406.         and TImage OnProgress events.}
  407.  
  408.   TGraphic = class(TPersistent)
  409.   protected
  410.     constructor Create; virtual;
  411.     procedure Changed(Sender: TObject); virtual;
  412.     procedure DefineProperties(Filer: TFiler); override;
  413.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); virtual; abstract;
  414.     function Equals(Graphic: TGraphic): Boolean; virtual;
  415.     function GetEmpty: Boolean; virtual; abstract;
  416.     function GetHeight: Integer; virtual; abstract;
  417.     function GetPalette: HPALETTE; virtual;
  418.     function GetTransparent: Boolean; virtual;
  419.     function GetWidth: Integer; virtual; abstract;
  420.     procedure Progress(Sender: TObject; Stage: TProgressStage;
  421.       PercentDone: Byte;  RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic;
  422.     procedure ReadData(Stream: TStream); virtual;
  423.     procedure SetHeight(Value: Integer); virtual; abstract;
  424.     procedure SetPalette(Value: HPALETTE); virtual;
  425.     procedure SetTransparent(Value: Boolean); virtual;
  426.     procedure SetWidth(Value: Integer); virtual; abstract;
  427.     procedure WriteData(Stream: TStream); virtual;
  428.   public
  429.     procedure LoadFromFile(const Filename: string); virtual;
  430.     procedure SaveToFile(const Filename: string); virtual;
  431.     procedure LoadFromStream(Stream: TStream); virtual; abstract;
  432.     procedure SaveToStream(Stream: TStream); virtual; abstract;
  433.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  434.       APalette: HPALETTE); virtual; abstract;
  435.     procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
  436.       var APalette: HPALETTE); virtual; abstract;
  437.     property Empty: Boolean;
  438.     property Height: Integer;
  439.     property Modified: Boolean;
  440.     property Palette: HPALETTE;
  441.     property PaletteModified: Boolean;
  442.     property Transparent: Boolean;
  443.     property Width: Integer;
  444.     property OnChange: TNotifyEvent;
  445.     property OnProgress: TProgressEvent;
  446.   end;
  447.  
  448.   TGraphicClass = class of TGraphic;
  449.  
  450.   { TPicture }
  451.   { TPicture is a TGraphic container.  It is used in place of a TGraphic if the
  452.     graphic can be of any TGraphic class.  LoadFromFile and SaveToFile are
  453.     polymorphic. For example, if the TPicture is holding an Icon, you can
  454.     LoadFromFile a bitmap file, where if the class was TIcon you could only read
  455.     .ICO files.
  456.       LoadFromFile - Reads a picture from disk.  The TGraphic class created
  457.         determined by the file extension of the file.  If the file extension is
  458.         not recognized an exception is generated.
  459.       SaveToFile - Writes the picture to disk.
  460.       LoadFromClipboardFormat - Reads the picture from the handle provided in
  461.         the given clipboard format.  If the format is not supported, an
  462.         exception is generated.
  463.       SaveToClipboardFormats - Allocates a global handle and writes the picture
  464.         in its native clipboard format (CF_BITMAP for bitmaps, CF_METAFILE
  465.         for metafiles, etc.).  Formats will contain the formats written.
  466.         Returns the number of clipboard items written to the array pointed to
  467.         by Formats and Datas or would be written if either Formats or Datas are
  468.         nil.
  469.       SupportsClipboardFormat - Returns true if the given clipboard format
  470.         is supported by LoadFromClipboardFormat.
  471.       Assign - Copys the contents of the given TPicture.  Used most often in
  472.         the implementation of TPicture properties.
  473.       RegisterFileFormat - Register a new TGraphic class for use in
  474.         LoadFromFile.
  475.       RegisterClipboardFormat - Registers a new TGraphic class for use in
  476.         LoadFromClipboardFormat.
  477.       UnRegisterGraphicClass - Removes all references to the specified TGraphic
  478.         class and all its descendents from the file format and clipboard format
  479.         internal lists.
  480.       Height - The native, unstretched, height of the picture.
  481.       Width - The native, unstretched, width of the picture.
  482.       Graphic - The TGraphic object contained by the TPicture
  483.       Bitmap - Returns a bitmap.  If the contents is not already a bitmap, the
  484.         contents are thrown away and a blank bitmap is returned.
  485.       Icon - Returns an icon.  If the contents is not already an icon, the
  486.         contents are thrown away and a blank icon is returned.
  487.       Metafile - Returns a metafile.  If the contents is not already a metafile,
  488.         the contents are thrown away and a blank metafile is returned. }
  489.   TPicture = class(TPersistent)
  490.   protected
  491.     procedure AssignTo(Dest: TPersistent); override;
  492.     procedure Changed(Sender: TObject); dynamic;
  493.     procedure Progress(Sender: TObject; Stage: TProgressStage;
  494.       PercentDone: Byte;  RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic;
  495.     procedure DefineProperties(Filer: TFiler); override;
  496.   public
  497.     constructor Create;
  498.     destructor Destroy; override;
  499.     procedure LoadFromFile(const Filename: string);
  500.     procedure SaveToFile(const Filename: string);
  501.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  502.       APalette: HPALETTE);
  503.     procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
  504.       var APalette: HPALETTE);
  505.     class function SupportsClipboardFormat(AFormat: Word): Boolean;
  506.     procedure Assign(Source: TPersistent); override;
  507.     class procedure RegisterFileFormat(const AExtension, ADescription: string;
  508.       AGraphicClass: TGraphicClass);
  509.     class procedure RegisterFileFormatRes(const AExtension: String;
  510.       ADescriptionResID: Integer; AGraphicClass: TGraphicClass);
  511.     class procedure RegisterClipboardFormat(AFormat: Word;
  512.       AGraphicClass: TGraphicClass);
  513.     class procedure UnregisterGraphicClass(AClass: TGraphicClass);
  514.     property Bitmap: TBitmap;
  515.     property Graphic: TGraphic;
  516.     property PictureAdapter: IChangeNotifier;
  517.     property Height: Integer;
  518.     property Icon: TIcon;
  519.     property Metafile: TMetafile;
  520.     property Width: Integer;
  521.     property OnChange: TNotifyEvent;
  522.     property OnProgress: TProgressEvent;
  523.   end;
  524.  
  525.   { TMetafile }
  526.   { TMetafile is an encapsulation of the Win32 Enhanced metafile.
  527.       Handle - The metafile handle.
  528.       Enhanced - determines how the metafile will be stored on disk.
  529.         Enhanced = True (default) stores as EMF (Win32 Enhanced Metafile),
  530.         Enhanced = False stores as WMF (Windows 3.1 Metafile, with Aldus header).
  531.         The in-memory format is always EMF.  WMF has very limited capabilities;
  532.         storing as WMF will lose information that would be retained by EMF.
  533.         This property is set to match the metafile type when loaded from a
  534.         stream or file.  This maintains form file compatibility with 16 bit
  535.         Delphi (If loaded as WMF, then save as WMF).
  536.       Inch - The units per inch assumed by a WMF metafile.  Used to alter
  537.         scale when writing as WMF, but otherwise this property is obsolete.
  538.         Enhanced metafiles maintain complete scale information internally.
  539.       MMWidth,
  540.       MMHeight: Width and Height in 0.01 millimeter units, the native
  541.         scale used by enhanced metafiles.  The Width and Height properties
  542.         are always in screen device pixel units; you can avoid loss of
  543.         precision in converting between device pixels and mm by setting
  544.         or reading the dimentions in mm with these two properties.
  545.       CreatedBy - Optional name of the author or application used to create
  546.         the metafile.
  547.       Description - Optional text description of the metafile.
  548.       You can set the CreatedBy and Description of a new metafile by calling
  549.       TMetafileCanvas.CreateWithComment.
  550.  
  551.     TMetafileCanvas
  552.       To create a metafile image from scratch, you must draw the image in
  553.       a metafile canvas.  When the canvas is destroyed, it transfers the
  554.       image into the metafile object provided to the canvas constructor.
  555.       After the image is drawn on the canvas and the canvas is destroyed,
  556.       the image is 'playable' in the metafile object.  Like this:
  557.  
  558.       MyMetafile := TMetafile.Create;
  559.       MyMetafile.Width := 200;
  560.       MyMetafile.Height := 200;
  561.       with TMetafileCanvas.Create(MyMetafile, 0) do
  562.       try
  563.         Brush.Color := clRed;
  564.         Ellipse(0,0,100,100);
  565.         ...
  566.       finally
  567.         Free;
  568.       end;
  569.       Form1.Canvas.Draw(0,0,MyMetafile);  (* 1 red circle  *)
  570.  
  571.       To add to an existing metafile image, create a metafile canvas
  572.       and play the source metafile into the metafile canvas.  Like this:
  573.  
  574.       (* continued from previous example, so MyMetafile contains an image *)
  575.       with TMetafileCanvas.Create(MyMetafile, 0) do
  576.       try
  577.         Draw(0,0,MyMetafile);
  578.         Brush.Color := clBlue;
  579.         Ellipse(100,100,200,200);
  580.         ...
  581.       finally
  582.         Free;
  583.       end;
  584.       Form1.Canvas.Draw(0,0,MyMetafile);  (* 1 red circle and 1 blue circle *)
  585.   }
  586.  
  587.   TMetafileCanvas = class(TCanvas)
  588.   public
  589.     constructor Create(AMetafile: TMetafile; ReferenceDevice: HDC);
  590.     constructor CreateWithComment(AMetafile: TMetafile; ReferenceDevice: HDC;
  591.       const CreatedBy, Description: String);
  592.     destructor Destroy; override;
  593.   end;
  594.  
  595.   TSharedImage = class
  596.   protected
  597.     procedure Reference;
  598.     procedure Release;
  599.     procedure FreeHandle; virtual; abstract;
  600.     property RefCount: Integer;
  601.   end;
  602.  
  603.   TMetafileImage = class(TSharedImage)
  604.   protected
  605.     procedure FreeHandle; override;
  606.   public
  607.     destructor Destroy; override;
  608.   end;
  609.  
  610.   TMetafile = class(TGraphic)
  611.   protected
  612.     function GetEmpty: Boolean; override;
  613.     function GetHeight: Integer; override;
  614.     function GetPalette: HPALETTE; override;
  615.     function GetWidth: Integer; override;
  616.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
  617.     procedure ReadData(Stream: TStream); override;
  618.     procedure ReadEMFStream(Stream: TStream);
  619.     procedure ReadWMFStream(Stream: TStream; Length: Longint);
  620.     procedure SetHeight(Value: Integer); override;
  621.     procedure SetTransparent(Value: Boolean); override;
  622.     procedure SetWidth(Value: Integer); override;
  623.     function  TestEMF(Stream: TStream): Boolean;
  624.     procedure WriteData(Stream: TStream); override;
  625.     procedure WriteEMFStream(Stream: TStream);
  626.     procedure WriteWMFStream(Stream: TStream);
  627.   public
  628.     constructor Create; override;
  629.     destructor Destroy; override;
  630.     procedure Clear;
  631.     procedure LoadFromStream(Stream: TStream); override;
  632.     procedure SaveToFile(const Filename: String); override;
  633.     procedure SaveToStream(Stream: TStream); override;
  634.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  635.       APalette: HPALETTE); override;
  636.     procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
  637.       var APalette: HPALETTE); override;
  638.     procedure Assign(Source: TPersistent); override;
  639.     function ReleaseHandle: HENHMETAFILE;
  640.     property CreatedBy: String;
  641.     property Description: String;
  642.     property Enhanced: Boolean default True;
  643.     property Handle: HENHMETAFILE;
  644.     property MMWidth: Integer;
  645.     property MMHeight: Integer;
  646.     property Inch: Word;
  647.   end;
  648.  
  649.   { TBitmap }
  650.   { TBitmap is an encapsulation of a Windows HBITMAP and HPALETTE.  It manages
  651.     the palette realizing automatically as well as having a Canvas to allow
  652.     modifications to the image.  Creating copies of a TBitmap is very fast
  653.     since the handle is copied not the image.  If the image is modified, and
  654.     the handle is shared by more than one TBitmap object, the image is copied
  655.     before the modification is performed (i.e. copy on write).
  656.       Canvas - Allows drawing on the bitmap.
  657.       Handle - The HBITMAP encapsulated by the TBitmap.  Grabbing the handle
  658.         directly should be avoided since it causes the HBITMAP to be copied if
  659.         more than one TBitmap share the handle.
  660.       Palette - The HPALETTE realized by the TBitmap.  Grabbing this handle
  661.         directly should be avoided since it causes the HPALETTE to be copied if
  662.         more than one TBitmap share the handle.
  663.       Monochrome - True if the bitmap is a monochrome bitmap }
  664.  
  665.   TBitmapImage = class(TSharedImage)
  666.   protected
  667.     procedure FreeHandle; override;
  668.   public
  669.     destructor Destroy; override;
  670.   end;
  671.  
  672.   TBitmapHandleType = (bmDIB, bmDDB);
  673.   TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);
  674.   TTransparentMode = (tmAuto, tmFixed);
  675.  
  676.   TBitmap = class(TGraphic)
  677.   protected
  678.     procedure Changed(Sender: TObject); override;
  679.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
  680.     function GetEmpty: Boolean; override;
  681.     function GetHeight: Integer; override;
  682.     function GetPalette: HPALETTE; override;
  683.     function GetWidth: Integer; override;
  684.     procedure HandleNeeded;
  685.     procedure MaskHandleNeeded;
  686.     procedure PaletteNeeded;
  687.     procedure ReadData(Stream: TStream); override;
  688.     procedure SetHeight(Value: Integer); override;
  689.     procedure SetPalette(Value: HPALETTE); override;
  690.     procedure SetWidth(Value: Integer); override;
  691.     procedure WriteData(Stream: TStream); override;
  692.   public
  693.     constructor Create; override;
  694.     destructor Destroy; override;
  695.     procedure Assign(Source: TPersistent); override;
  696.     procedure Dormant;
  697.     procedure FreeImage;
  698.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  699.       APalette: HPALETTE); override;
  700.     procedure LoadFromStream(Stream: TStream); override;
  701.     procedure LoadFromResourceName(Instance: THandle; const ResName: String);
  702.     procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
  703.     procedure Mask(TransparentColor: TColor);
  704.     function ReleaseHandle: HBITMAP;
  705.     function ReleaseMaskHandle: HBITMAP;
  706.     function ReleasePalette: HPALETTE;
  707.     procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
  708.       var APalette: HPALETTE); override;
  709.     procedure SaveToStream(Stream: TStream); override;
  710.     property Canvas: TCanvas;
  711.     property Handle: HBITMAP;
  712.     property HandleType: TBitmapHandleType;
  713.     property IgnorePalette: Boolean;
  714.     property MaskHandle: HBITMAP;
  715.     property Monochrome: Boolean;
  716.     property PixelFormat: TPixelFormat;
  717.     property ScanLine[Row: Integer]: Pointer;
  718.     property TransparentColor: TColor;
  719.     property TransparentMode: TTransparentMode default tmAuto;
  720.   end;
  721.  
  722.   { TIcon }
  723.   { TIcon encapsulates window HICON handle. Drawing of an icon does not stretch
  724.     so calling stretch draw is not meaningful.
  725.       Handle - The HICON used by the TIcon. }
  726.  
  727.   TIconImage = class(TSharedImage)
  728.   protected
  729.     procedure FreeHandle; override;
  730.   public
  731.     destructor Destroy; override;
  732.   end;
  733.  
  734.   TIcon = class(TGraphic)
  735.   protected
  736.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
  737.     function GetEmpty: Boolean; override;
  738.     function GetHeight: Integer; override;
  739.     function GetWidth: Integer; override;
  740.     procedure SetHeight(Value: Integer); override;
  741.     procedure SetTransparent(Value: Boolean); override;
  742.     procedure SetWidth(Value: Integer); override;
  743.   public
  744.     constructor Create; override;
  745.     destructor Destroy; override;
  746.     procedure Assign(Source: TPersistent); override;
  747.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  748.       APalette: HPALETTE); override;
  749.     procedure LoadFromStream(Stream: TStream); override;
  750.     function ReleaseHandle: HICON;
  751.     procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
  752.       var APalette: HPALETTE); override;
  753.     procedure SaveToStream(Stream: TStream); override;
  754.     property Handle: HICON;
  755.   end;
  756.  
  757. var    // New TFont instances are intialized with the values in this structure:
  758.   DefFontData: TFontData = (
  759.     Handle: 0;
  760.     Height: 0;
  761.     Pitch: fpDefault;
  762.     Style: [];
  763.     Charset : DEFAULT_CHARSET;
  764.     Name: 'MS Sans Serif');
  765.  
  766.  
  767. var
  768.   SystemPalette16: HPalette; // 16 color palette that maps to the system palette
  769.  
  770. var
  771.   DDBsOnly: Boolean = False; // True = Load all BMPs as device bitmaps.
  772.                              // Not recommended.
  773.  
  774. function GraphicFilter(GraphicClass: TGraphicClass): string;
  775. function GraphicExtension(GraphicClass: TGraphicClass): string;
  776. function GraphicFileMask(GraphicClass: TGraphicClass): string;
  777.  
  778. function ColorToRGB(Color: TColor): Longint;
  779. function ColorToString(Color: TColor): string;
  780. function StringToColor(const S: string): TColor;
  781. procedure GetColorValues(Proc: TGetStrProc);
  782. function ColorToIdent(Color: Longint; var Ident: string): Boolean;
  783. function IdentToColor(const Ident: string; var Color: Longint): Boolean;
  784. procedure GetCharsetValues(Proc: TGetStrProc);
  785. function CharsetToIdent(Charset: Longint; var Ident: string): Boolean;
  786. function IdentToCharset(const Ident: string; var Charset: Longint): Boolean;
  787.  
  788. procedure GetDIBSizes(Bitmap: HBITMAP; var InfoHeaderSize: DWORD;
  789.   var ImageSize: DWORD);
  790. function GetDIB(Bitmap: HBITMAP; Palette: HPALETTE; var BitmapInfo; var Bits): Boolean;
  791.  
  792. function CopyPalette(Palette: HPALETTE): HPALETTE;
  793.  
  794. procedure PaletteChanged;
  795. procedure FreeMemoryContexts;
  796.  
  797. function GetDefFontCharSet: TFontCharSet;
  798.  
  799. function TransparentStretchBlt(DstDC: HDC; DstX, DstY, DstW, DstH: Integer;
  800.   SrcDC: HDC; SrcX, SrcY, SrcW, SrcH: Integer; MaskDC: HDC; MaskX,
  801.   MaskY: Integer): Boolean;
  802.  
  803. function CreateMappedBmp(Handle: HBITMAP; const OldColors, NewColors: array of TColor): HBITMAP;
  804. function CreateMappedRes(Instance: THandle; ResName: PChar; const OldColors, NewColors: array of TColor): HBITMAP;
  805. function CreateGrayMappedBmp(Handle: HBITMAP): HBITMAP;
  806. function CreateGrayMappedRes(Instance: THandle; ResName: PChar): HBITMAP;
  807.  
  808. function AllocPatternBitmap(BkColor, FgColor: TColor): TBitmap;
  809.  
  810. // Alignment must be a power of 2.  Color BMPs require DWORD alignment (32).
  811. function BytesPerScanline(PixelsPerScanline, BitsPerPixel, Alignment: Longint): Longint;
  812.  
  813. implementation
  814.