随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

EnumEnhMetaFile(
  DC: HDC;                       {目标设备句柄; 如果回调函数不需要回放, 该参数为 0}
  Emf: HENHMETAFILE;             {图元文件句柄}
  EnhMetaFunc: TFNEnhMFEnumProc; {回调函数指针; 回调函数格式见下}
  Data: Pointer;                 {自定义数据指针}
  const Rect: TRect              {要输出的尺寸; 如果 DC=0 该参数会被忽略}
): BOOL;

//EnumEnhMetaFile 使用的回调函数格式及参数, PlayEnhMetaFileRecord 基本就是使用这些参数:
function EnhMetaFunc(
  DisplaySurface: HDC;                {目标设备句柄; 如果回调函数不需要回放, 该参数为 0}
  var MetafileTable: THandleTable;    {句柄表数组指针, 数组包含 HGDIOBJ 对象(画笔、画刷等); 元素1是文件自身句柄}
  var MetafileRecord: TEnhMetaRecord; {图元文件记录 TEnhMetaRecord 指针, TEnhMetaRecord 结构见下表}
  ObjectCount: Integer;               {对象句柄数目, 即句柄表 MetafileTable 中图形对象的数目}
  var Data: Longint                   {自定义数据指针}
): Integer; stdcall;

//TEnhMetaRecord 结构:
tagENHMETARECORD = packed record
  iType: DWORD;                {类型}
  nSize: DWORD;                {尺寸}
  dParm: array[0..0] of DWORD; {参数数组}
end;

//TEnhMetaRecord.iType 的可能值:
EMR_HEADER                  = 1;
EMR_POLYBEZIER              = 2;
EMR_POLYGON                 = 3;
EMR_POLYLINE                = 4;
EMR_POLYBEZIERTO            = 5;
EMR_POLYLINETO              = 6;
EMR_POLYPOLYLINE            = 7;
EMR_POLYPOLYGON             = 8;
EMR_SETWINDOWEXTEX          = 9;
EMR_SETWINDOWORGEX          = 10;
EMR_SETVIEWPORTEXTEX        = 11;
EMR_SETVIEWPORTORGEX        = 12;
EMR_SETBRUSHORGEX           = 13;
EMR_EOF                     = 14;
EMR_SETPIXELV               = 15;
EMR_SETMAPPERFLAGS          = $10;
EMR_SETMAPMODE              = 17;
EMR_SETBKMODE               = 18;
EMR_SETPOLYFILLMODE         = 19;
EMR_SETROP2                 = 20;
EMR_SETSTRETCHBLTMODE       = 21;
EMR_SETTEXTALIGN            = 22;
EMR_SETCOLORADJUSTMENT      = 23;
EMR_SETTEXTCOLOR            = 24;
EMR_SETBKCOLOR              = 25;
EMR_OFFSETCLIPRGN           = 26;
EMR_MOVETOEX                = 27;
EMR_SETMETARGN              = 28;
EMR_EXCLUDECLIPRECT         = 29;
EMR_INTERSECTCLIPRECT       = 30;
EMR_SCALEVIEWPORTEXTEX      = 31;
EMR_SCALEWINDOWEXTEX        = 32;
EMR_SAVEDC                  = 33;
EMR_RESTOREDC               = 34;
EMR_SETWORLDTRANSFORM       = 35;
EMR_MODIFYWORLDTRANSFORM    = 36;
EMR_SELECTOBJECT            = 37;
EMR_CREATEPEN               = 38;
EMR_CREATEBRUSHINDIRECT     = 39;
EMR_DELETEOBJECT            = 40;
EMR_ANGLEARC                = 41;
EMR_ELLIPSE                 = 42;
EMR_RECTANGLE               = 43;
EMR_ROUNDRECT               = 44;
EMR_ARC                     = 45;
EMR_CHORD                   = 46;
EMR_PIE                     = 47;
EMR_SELECTPALETTE           = 48;
EMR_CREATEPALETTE           = 49;
EMR_SETPALETTEENTRIES       = 50;
EMR_RESIZEPALETTE           = 51;
EMR_REALIZEPALETTE          = 52;
EMR_EXTFLOODFILL            = 53;
EMR_LINETO                  = 54;
EMR_ARCTO                   = 55;
EMR_POLYDRAW                = 56;
EMR_SETARCDIRECTION         = 57;
EMR_SETMITERLIMIT           = 58;
EMR_BEGINPATH               = 59;
EMR_ENDPATH                 = 60;
EMR_CLOSEFIGURE             = 61;
EMR_FILLPATH                = 62;
EMR_STROKEANDFILLPATH       = 63;
EMR_STROKEPATH              = $40;
EMR_FLATTENPATH             = 65;
EMR_WIDENPATH               = 66;
EMR_SELECTCLIPPATH          = 67;
EMR_ABORTPATH               = 68;
EMR_GDICOMMENT              = 70;
EMR_FILLRGN                 = 71;
EMR_FRAMERGN                = 72;
EMR_INVERTRGN               = 73;
EMR_PAINTRGN                = 74;
EMR_EXTSELECTCLIPRGN        = 75;
EMR_BITBLT                  = 76;
EMR_STRETCHBLT              = 77;
EMR_MASKBLT                 = 78;
EMR_PLGBLT                  = 79;
EMR_SETDIBITSTODEVICE       = 80;
EMR_STRETCHDIBITS           = 81;
EMR_EXTCREATEFONTINDIRECTW  = 82;
EMR_EXTTEXTOUTA             = 83;
EMR_EXTTEXTOUTW             = 84;
EMR_POLYBEZIER16            = 85;
EMR_POLYGON16               = 86;
EMR_POLYLINE16              = 87;
EMR_POLYBEZIERTO16          = 88;
EMR_POLYLINETO16            = 89;
EMR_POLYPOLYLINE16          = 90;
EMR_POLYPOLYGON16           = 91;
EMR_POLYDRAW16              = 92;
EMR_CREATEMONOBRUSH         = 93;
EMR_CREATEDIBPATTERNBRUSHPT = 94;
EMR_EXTCREATEPEN            = 95;
EMR_POLYTEXTOUTA            = 96;
EMR_POLYTEXTOUTW            = 97;
EMR_SETICMMODE              = 98;
EMR_CREATECOLORSPACE        = 99;
EMR_SETCOLORSPACE           = 100;
EMR_DELETECOLORSPACE        = 101;
EMR_GLSRECORD               = 102;
EMR_GLSBOUNDEDRECORD        = 103;
EMR_PIXELFORMAT             = 104;
EMR_DRAWESCAPE              = 105;
EMR_EXTESCAPE               = 106;
EMR_STARTDOC                = 107;
EMR_SMALLTEXTOUT            = 108;
EMR_FORCEUFIMAPPING         = 109;
EMR_NAMEDESCAPE             = 110;
EMR_COLORCORRECTPALETTE     = 111;
EMR_SETICMPROFILEA          = 112;
EMR_SETICMPROFILEW          = 113;
EMR_ALPHABLEND              = 114;
EMR_ALPHADIBBLEND           = 115;
EMR_TRANSPARENTBLT          = 116;
EMR_TRANSPARENTDIB          = 117;
EMR_GRADIENTFILL            = 118;
EMR_SETLINKEDUFIS           = 119;
EMR_SETTEXTJUSTIFICATION    = 120;

在下面的例子中, 先在内存中建立了一个图元文件, 再其中绘制了直线、矩形和两个椭圆;

然后控制显示了图元文件的部分及全部.

运行效果图:



代码文件:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  HMetaFile: HENHMETAFILE; {EMF 文件句柄}
  RadioGroupItemIndex: Integer = -1;

//EnumEnhMetaFile 的回调函数
function MyEnhMetaFunc(DisplaySurface: HDC; var MetafileTable: THandleTable;
  var MetafileRecord: TEnhMetaRecord; ObjectCount: Integer; var Data: Longint
  ): Integer; stdcall;
var
  itype: Integer;
begin
  case RadioGroupItemIndex of
    1: itype := EMR_RECTANGLE;
    2: itype := EMR_ELLIPSE;
  end;
  if MetafileRecord.iType = itype then
  begin
    PlayEnhMetaFileRecord(DisplaySurface, MetafileTable, MetafileRecord, ObjectCount);
    Result := 1;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  MetaFileDC: HDC;
begin
  {文件建立在内存}
  MetaFileDC := CreateEnhMetaFile(0, nil, nil, nil);
  MoveToEx(MetaFileDC, 0, 0, nil);
  LineTo(MetaFileDC, 100, 100);
  {矩形}
  Rectangle(MetaFileDC, 10, 10, 30, 30);
  {椭圆}
  Ellipse(MetaFileDC, 30, 30, 50, 50);
  Ellipse(MetaFileDC, 45, 45, 65, 65);

  HMetaFile := CloseEnhMetaFile(MetaFileDC);
  DeleteObject(MetaFileDC);

  RadioGroup1.Items.CommaText := '整个图元文件,其中的矩形,其中的椭圆';
end;

procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
  Repaint;
  RadioGroupItemIndex := RadioGroup1.ItemIndex;
  if RadioGroupItemIndex = 0 then
    PlayEnhMetaFile(Canvas.Handle, HMetaFile, ClientRect)
  else
    EnumEnhMetaFile(Canvas.Handle, HMetaFile, @MyEnhMetaFunc, nil, ClientRect);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DeleteEnhMetaFile(HMetaFile);
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 197
  ClientWidth = 325
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object RadioGroup1: TRadioGroup
    Left = 216
    Top = 8
    Width = 101
    Height = 113
    Caption = 'RadioGroup1'
    TabOrder = 0
    OnClick = RadioGroup1Click
  end
end

posted on 2008-06-05 13:20  万一  阅读(...)  评论(...编辑  收藏