⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getenhmetafileu.pas

📁 DelphiWin32核心API参考光盘内容.是学习书籍中的源码,便于学习.
💻 PAS
字号:
unit GetEnhMetaFileU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, FileCtrl, StdCtrls;

type
  TForm1 = class(TForm)
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    Panel1: TPanel;
    Image1: TImage;
    Panel2: TPanel;
    Image2: TImage;
    ListBox1: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure FileListBox1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FileListBox1DblClick(Sender: TObject);
var
   TheMetafile: HENHMETAFILE;           // a handle to the original metafile
   CopyMetafile: HENHMETAFILE;          // a handle to the copied metafile
   MetafileInfo: TEnhMetaHeader;        // the metafile header structure
   MetafileDescription: PChar;          // holds the metafile description
   DescriptionSize: UINT;               // holds the size of the description
   CorrectedRect: TRect;                // an aspect ratio corrected rectangle
   ScaleVert,                           // these are used to compute the
   ScaleHorz,                           // corrected aspect ratio
   ScaleLeast: Real;
begin                                  
   {open and retrieve a handle to the selected metafile}
   TheMetafile:=GetEnhMetaFile(PChar(FileListBox1.FileName));

   {retrieve the size of the description string}
   DescriptionSize:=GetEnhMetaFileDescription(TheMetaFile, 0, nil);

   {dynamically allocate a buffer large enough to hold the description}
   MetafileDescription:=StrAlloc(DescriptionSize+1);

   {retrieve the metafile description string, if one exists}
   GetEnhMetaFileDescription(TheMetaFile, DescriptionSize, MetafileDescription);

   {retrieve the metafile header info}
   GetEnhMetaFileHeader(TheMetafile, SizeOf(MetafileInfo), @MetafileInfo);

   {find the smallest ratio between the size of the metafile bounding rectangle
    and the TImage rectangle}
   ScaleVert:=Image1.Height / (MetafileInfo.rclBounds.Bottom-
                               MetafileInfo.rclBounds.Top);
   ScaleHorz:=Image1.Width  / (MetafileInfo.rclBounds.Right-
                               MetafileInfo.rclBounds.Left);

   {find the smallest ratio}
   if ScaleVert<ScaleHorz then
      ScaleLeast:=ScaleVert
   else
      ScaleLeast:=ScaleHorz;

   {determine the new bounding rectangle using this scaling factor}
   CorrectedRect.Left  :=Trunc(MetafileInfo.rclBounds.Left*ScaleLeast);
   CorrectedRect.Top   :=Trunc(MetafileInfo.rclBounds.Top*ScaleLeast);
   CorrectedRect.Right :=Trunc(MetafileInfo.rclBounds.Right*ScaleLeast);
   CorrectedRect.Bottom:=Trunc(MetafileInfo.rclBounds.Bottom*ScaleLeast);

   {adjust the new bounding rectangle so it starts in the
    upper left hand corner}
   CorrectedRect.Left:=0;
   CorrectedRect.Top:=0;
   CorrectedRect.Right:=CorrectedRect.Right-CorrectedRect.Left;
   CorrectedRect.Bottom:=CorrectedRect.Bottom-CorrectedRect.Top;

   {start displaying the metafile information}
   with ListBox1.Items do
   begin
      Clear;
      Add('Description -');
      if DescriptionSize>0 then
      begin
         {the description is a string in the form of the program name used
          to create the metafile followed by a null terminator, followed
          by the name of the metafile followed by two null terminators. this
          line will display the first part of the description (the name of the
          program used to create the metafile)}
         Add(string(MetafileDescription));

         {by advancing the address of the string one past the first null
          terminator, we gain access the second half containing the
          name of the metafile}
         Add(string(PChar(MetafileDescription+StrLen(MetafileDescription)+1)));
      end
      else
         Add('No description found.');
      Add('Type: '+IntToStr(MetafileInfo.iType));
      Add('Size: '+IntToStr(MetafileInfo.nSize));
      Add('Bounding Rectangle -');
      Add('         Left: '+IntToStr(MetafileInfo.rclBounds.Left));
      Add('          Top: '+IntToStr(MetafileInfo.rclBounds.Top));
      Add('        Right: '+IntToStr(MetafileInfo.rclBounds.Right));
      Add('       Bottom: '+IntToStr(MetafileInfo.rclBounds.Bottom));
      Add('Frame Rectangle - (1 = .01 millimeters)');
      Add('         Left: '+IntToStr(MetafileInfo.rclFrame.Left));
      Add('          Top: '+IntToStr(MetafileInfo.rclFrame.Top));
      Add('        Right: '+IntToStr(MetafileInfo.rclFrame.Right));
      Add('       Bottom: '+IntToStr(MetafileInfo.rclFrame.Bottom));
      Add('Signature: '+IntToStr(MetafileInfo.dSignature));
      Add('Version: '+IntToStr(MetafileInfo.nVersion));
      Add('Bytes: '+IntToStr(MetafileInfo.nBytes));
      Add('Records: '+IntToStr(MetafileInfo.nRecords));
      Add('Handles: '+IntToStr(MetafileInfo.nHandles));
      Add('Reserved: '+IntToStr(MetafileInfo.sReserved));
      Add('Description Size: '+IntToStr(MetafileInfo.nDescription));
      Add('Description Offset: '+IntToStr(MetafileInfo.offDescription));
      Add('Palette Entries: '+IntToStr(MetafileInfo.nPalEntries));
      Add('Reference Resolution, Pixels - ');
      Add('          Horizontal: '+IntToStr(MetafileInfo.szlDevice.cx));
      Add('            Vertical: '+IntToStr(MetafileInfo.szlDevice.cy));
   end;

   {erase any previous images}
   Image1.Canvas.Fillrect(Image1.Canvas.Cliprect);
   Image2.Canvas.Fillrect(Image2.Canvas.Cliprect);

   {display the metafile as it originally appears}
   PlayEnhMetaFile(Image1.Canvas.Handle, TheMetafile, CorrectedRect);

   {make a copy of the original metafile in memory}
   CopyMetafile:=CopyEnhMetaFile(TheMetafile, nil);

   {display this copied metafile}
   PlayEnhMetaFile(Image2.Canvas.Handle, CopyMetafile, Image1.Canvas.Cliprect);

   {delete the handles to both metafiles, as they are no longer needed}
   DeleteEnhMetaFile(TheMetafile);
   DeleteEnhMetaFile(CopyMetafile);

   {return the memory allocated for the description string}
   StrDispose(MetafileDescription);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -