//增强图元文件的头文件结构 TEnhMetaHeader: tagENHMETAHEADER = packed record iType: DWORD; {记录类型} nSize: DWORD; {结构大小} rclBounds: TRect; {外接矩形(单位是像素)} rclFrame: TRect; {图片矩形(单位是 0.1 毫米)} dSignature: DWORD; {文件签名} nVersion: DWORD; {文件版本} nBytes: DWORD; {文件尺寸} nRecords: DWORD; {记录数} nHandles: Word; {句柄数} sReserved: Word; {保留} nDescription: DWORD; {说明文本的长度} offDescription: DWORD; {说明文本的偏移量} nPalEntries: DWORD; {调色板的元素数} szlDevice: TSize; {分辨率(像素)} szlMillimeters: TSize; {分辨率(毫米)} cbPixelFormat: DWORD; {} offPixelFormat: DWORD; {} bOpenGL: DWORD; {} end;本例效果图:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} var HMetaFile: HENHMETAFILE; {EMF 文件句柄} {建立带说明的 EMF 文件} procedure TForm1.FormCreate(Sender: TObject); const Path = 'C:\Temp\MyMeateFile.emf'; Description = 'ABCDE'; {当作 EMF 文件的说明} var MetaFileDC: HDC; begin {建立并写入说明} MetaFileDC := CreateEnhMetaFile(0, PChar(Path), nil, PChar(Description)); {绘图} Rectangle(MetaFileDC, 11, 11, 111, 111); {结束编辑、保存文件, 并返回文件句柄} HMetaFile := CloseEnhMetaFile(MetaFileDC); {删除 DC} DeleteObject(MetaFileDC); end; {读取 EMF 文件头} procedure TForm1.Button1Click(Sender: TObject); const n = #13#10; var MetaHeaderSize: Integer; {文件头大小} MetaHeader: PEnhMetaHeader; {文件头结构的指针} str: string; buf: array[Word] of Char; begin MetaHeaderSize := GetEnhMetaFileHeader(HMetaFile, 0, nil); GetMem(MetaHeader, MetaHeaderSize); GetEnhMetaFileHeader(HMetaFile, MetaHeaderSize, MetaHeader); str := 'iType: %d' + n + 'nSize: %d' + n + 'rclBounds: %d,%d,%d,%d' + n + 'rclFrame: %d,%d,%d,%d' + n + 'dSignature: %d' + n + 'nVersion: %d' + n + 'nBytes: %d' + n + 'nRecords: %d' + n + 'nHandles: %d' + n + //'sReserved: %d' + n + 'nDescription: %d' + n + 'offDescription: %d' + n + 'nPalEntries: %d' + n + 'szlDevice: %d,%d' + n + 'szlMillimeters: %d,%d' + n + 'cbPixelFormat: %d' + n + 'offPixelFormat: %d' + n + 'bOpenGL: %d'; wvsprintf(buf, PChar(str), PChar(MetaHeader)); ShowMessage(buf); FreeMem(MetaHeader); end; {绘制在窗体} procedure TForm1.Button2Click(Sender: TObject); begin PlayEnhMetaFile(Canvas.Handle, HMetaFile, Rect(11,11,111,111)); end; {删除 EMF 文件句柄} procedure TForm1.FormDestroy(Sender: TObject); begin DeleteEnhMetaFile(HMetaFile); end; end.