home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 August / VPR9708A.ISO / D3TRIAL / INSTALL / DATA.Z / GRAPHICS.INT < prev    next >
Text File  |  1997-03-21  |  32KB  |  773 lines

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