Subversion Repositories decoder

Rev

Blame | Last modification | View Log | RSS feed

  1. unit DropBMPTarget;
  2.  
  3. // -----------------------------------------------------------------------------
  4. // Project:         Drag and Drop Component Suite
  5. // Component Names: TDropBMPSource
  6. // Module:          DropBMPSource
  7. // Description:     Implements Dragging & Dropping of Bitmaps
  8. //                  FROM your application to another.
  9. // Version:         3.7
  10. // Date:            22-JUL-1999
  11. // Target:          Win32, Delphi 3 - Delphi 5, C++ Builder 3, C++ Builder 4
  12. // Authors:         Angus Johnson,   ajohnson@rpi.net.au
  13. //                  Anders Melander, anders@melander.dk
  14. //                                   http://www.melander.dk
  15. // Copyright        © 1997-99 Angus Johnson & Anders Melander
  16. // -----------------------------------------------------------------------------
  17.  
  18. interface
  19.  
  20. uses
  21.   DropSource,
  22.   DropTarget,
  23.   Windows, Classes, Graphics, ActiveX;
  24.  
  25. {$include DragDrop.inc}
  26.  
  27. type
  28.   TDropBMPTarget = class(TDropTarget)
  29.   private
  30.     fBitmap: TBitmap;
  31.   protected
  32.     procedure ClearData; override;
  33.     function DoGetData: boolean; override;
  34.     function HasValidFormats: boolean; override;
  35.   public
  36.     constructor Create(AOwner: TComponent); override;
  37.     destructor Destroy; override;
  38.     property Bitmap: TBitmap Read fBitmap;
  39.   end;
  40.  
  41. procedure Register;
  42. procedure CopyDIBToBitmap(Bitmap: TBitmap; BitmapInfo: PBitmapInfo; DIBSize: integer);
  43.  
  44. implementation
  45.  
  46. const
  47.   DIBFormatEtc: TFormatEtc = (cfFormat: CF_DIB;
  48.     ptd: nil; dwAspect: DVASPECT_CONTENT; lindex: -1; tymed: TYMED_HGLOBAL);
  49.   BMPFormatEtc: TFormatEtc = (cfFormat: CF_BITMAP;
  50.     ptd: nil; dwAspect: DVASPECT_CONTENT; lindex: -1; tymed: TYMED_GDI);
  51.   PalFormatEtc: TFormatEtc = (cfFormat: CF_PALETTE;
  52.     ptd: nil; dwAspect: DVASPECT_CONTENT; lindex: -1; tymed: TYMED_GDI);
  53.  
  54. procedure Register;
  55. begin
  56.   RegisterComponents('DragDrop', [TDropBMPTarget]);
  57. end;
  58.  
  59. // -----------------------------------------------------------------------------
  60. //      Miscellaneous DIB Function
  61. // -----------------------------------------------------------------------------
  62.  
  63. procedure CopyDIBToBitmap(Bitmap: TBitmap; BitmapInfo: PBitmapInfo; DIBSize: integer);
  64. var
  65.   BitmapFileHeader      : TBitmapFileHeader;
  66.   FileSize              : integer;
  67.   InfoSize              : integer;
  68.   Stream                : TMemoryStream;
  69. begin
  70.   // Write DIB to a stream in the BMP file format
  71.   Stream := TMemoryStream.Create;
  72.   try
  73.     FileSize := sizeof(TBitmapFileHeader) + DIBSize;
  74.     InfoSize := sizeof(TBitmapInfoHeader);
  75.     if (BitmapInfo^.bmiHeader.biBitCount > 8) then
  76.     begin
  77.       if ((BitmapInfo^.bmiHeader.biCompression and BI_BITFIELDS) <> 0) then
  78.         Inc(InfoSize, 12);
  79.     end else
  80.       Inc(InfoSize, sizeof(TRGBQuad) * (1 shl BitmapInfo^.bmiHeader.biBitCount));
  81.     Stream.SetSize(FileSize);
  82.     // Initialize file header
  83.     FillChar(BitmapFileHeader, sizeof(TBitmapFileHeader), 0);
  84.     with BitmapFileHeader do
  85.     begin
  86.       bfType := $4D42; // 'BM' = Windows BMP signature
  87.       bfSize := FileSize; // File size (not needed)
  88.       bfOffBits := sizeof(TBitmapFileHeader) + InfoSize; // Offset of pixel data
  89.     end;
  90.     // Save file header
  91.     Stream.Write(BitmapFileHeader, sizeof(TBitmapFileHeader));
  92.     // Save TBitmapInfo structure and pixel data
  93.     Stream.Write(BitmapInfo^, DIBSize);
  94.  
  95.     // Rewind and load bitmap from stream
  96.     Stream.Position := 0;
  97.     Bitmap.LoadFromStream(Stream);
  98.   finally
  99.     Stream.Free;
  100.   end;
  101. end;
  102.  
  103. // -----------------------------------------------------------------------------
  104. //                      TDropBMPTarget
  105. // -----------------------------------------------------------------------------
  106.  
  107. constructor TDropBMPTarget.Create( AOwner: TComponent );
  108. begin
  109.    inherited Create( AOwner );
  110.    fBitmap := TBitmap.Create;
  111. end;
  112. // -----------------------------------------------------------------------------
  113.  
  114. destructor TDropBMPTarget.Destroy;
  115. begin
  116.   fBitmap.Free;
  117.   inherited Destroy;
  118. end;
  119. // -----------------------------------------------------------------------------
  120.  
  121. function TDropBMPTarget.HasValidFormats: boolean;
  122. begin
  123.   result := (DataObject.QueryGetData(DIBFormatEtc) = S_OK) or
  124.                 (DataObject.QueryGetData(BMPFormatEtc) = S_OK);
  125. end;
  126. // -----------------------------------------------------------------------------
  127.  
  128. procedure TDropBMPTarget.ClearData;
  129. begin
  130.   fBitmap.handle := 0;
  131. end;
  132. // -----------------------------------------------------------------------------
  133.  
  134. function TDropBMPTarget.DoGetData: boolean;
  135. var
  136.   medium, medium2: TStgMedium;
  137.   DIBData: pointer;
  138. begin
  139.   result := false;
  140.   //--------------------------------------------------------------------------
  141.   if (DataObject.GetData(DIBFormatEtc, medium) = S_OK) then
  142.   begin
  143.     if (medium.tymed = TYMED_HGLOBAL) then
  144.     begin
  145.       DIBData := GlobalLock(medium.HGlobal);
  146.       try
  147.         CopyDIBToBitmap(fBitmap, DIBData, GlobalSize(Medium.HGlobal));
  148.         result := true;
  149.       finally
  150.         GlobalUnlock(medium.HGlobal);
  151.       end;
  152.     end;
  153.     ReleaseStgMedium(medium);
  154.   end
  155.   //--------------------------------------------------------------------------
  156.   else if (DataObject.GetData(BMPFormatEtc, medium) = S_OK) then
  157.   begin
  158.     try
  159.       if (medium.tymed <> TYMED_GDI) then exit;
  160.       if (DataObject.GetData(PalFormatEtc, medium2) = S_OK) then
  161.       begin
  162.         try
  163.           fBitmap.LoadFromClipboardFormat(CF_BITMAP, Medium.hBitmap, Medium2.hBitmap);
  164.         finally
  165.           ReleaseStgMedium(medium2);
  166.         end;
  167.       end
  168.       else
  169.         fBitmap.LoadFromClipboardFormat(CF_BITMAP, Medium.hBitmap, 0);
  170.       result := true;
  171.     finally
  172.       ReleaseStgMedium(medium);
  173.     end;
  174.   end
  175.   //--------------------------------------------------------------------------
  176.   else
  177.     result := false;
  178. end;
  179. // -----------------------------------------------------------------------------
  180. // -----------------------------------------------------------------------------
  181.  
  182. end.
  183.